在Python中存储类似json的结构进行处理的最佳方法

时间:2019-07-07 08:34:38

标签: python-3.x data-structures python-datamodel

我似乎有一个类似于JSON的结构作为API响应。看起来如下。

{
  '_links': {
    '_self': 'https://api.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183308_100c',
    'assets': 'https://api.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183308_100c/assets/',
    'thumbnail': 'https://tiles.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183308_100c/thumb'
  },
  '_permissions': ['assets.basic_analytic_rpc_nitf:download', 'assets.udm2:download', 'assets.analytic_xml:download', 'assets.basic_analytic_dn:download', 'assets.basic_analytic_dn_xml_nitf:download', 'assets.basic_analytic_dn_nitf:download', 'assets.basic_analytic_xml:download', 'assets.basic_analytic_nitf:download', 'assets.basic_analytic_rpc:download', 'assets.analytic_dn:download', 'assets.basic_udm:download', 'assets.basic_analytic_dn_rpc_nitf:download', 'assets.analytic:download', 'assets.analytic_dn_xml:download', 'assets.analytic_sr:download', 'assets.basic_analytic_dn_xml:download', 'assets.basic_udm2:download', 'assets.basic_analytic_dn_rpc:download', 'assets.basic_analytic_xml_nitf:download', 'assets.basic_analytic:download', 'assets.udm:download'],
  'geometry': {
    'coordinates': [
      [
        [-122.33099544690265, 37.50055222996912],
        [-122.32310279398551, 37.528325690277924],
        [-122.32274055258365, 37.5282619001356],
        [-122.31970046184915, 37.53940549147412],
        [-122.03482745003222, 37.489623639299495],
        [-122.0474732844629, 37.44402988056649],
        [-122.05518612558149, 37.41692373485298],
        [-122.34026781061122, 37.46710050993664],
        [-122.33099544690265, 37.50055222996912]
      ]
    ],
    'type': 'Polygon'
  },
  'id': '20190530_183308_100c',
  'properties': {
    'acquired': '2019-05-30T18:33:08.371898Z',
    'anomalous_pixels': 0.01,
    'clear_confidence_percent': 85,
    'clear_percent': 97,
    'cloud_cover': 0.03,
    'cloud_percent': 1,
    'columns': 8996,
    'epsg_code': 32610,
    'ground_control': True,
    'gsd': 3.9,
    'heavy_haze_percent': 0,
    'instrument': 'PS2',
    'item_type': 'PSScene4Band',
    'light_haze_percent': 1,
    'origin_x': 558339,
    'origin_y': 4154931,
    'pixel_resolution': 3,
    'provider': 'planetscope',
    'published': '2019-05-31T02:03:28.763Z',
    'quality_category': 'standard',
    'rows': 4463,
    'satellite_id': '100c',
    'shadow_percent': 0,
    'snow_ice_percent': 0,
    'strip_id': '2405105',
    'sun_azimuth': 120.7,
    'sun_elevation': 64.2,
    'updated': '2019-05-31T03:40:00.992Z',
    'usable_data': 0,
    'view_angle': 5,
    'visible_confidence_percent': 64,
    'visible_percent': 99
  },
  'type': 'Feature'
} {
  '_links': {
    '_self': 'https://api.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183307_100c',
    'assets': 'https://api.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183307_100c/assets/',
    'thumbnail': 'https://tiles.planet.com/data/v1/item-types/PSScene4Band/items/20190530_183307_100c/thumb'
  },
  '_permissions': ['assets.basic_analytic_rpc_nitf:download', 'assets.udm2:download', 'assets.analytic_xml:download', 'assets.basic_analytic_dn:download', 'assets.basic_analytic_dn_xml_nitf:download', 'assets.basic_analytic_dn_nitf:download', 'assets.basic_analytic_xml:download', 'assets.basic_analytic_nitf:download', 'assets.basic_analytic_rpc:download', 'assets.analytic_dn:download', 'assets.basic_udm:download', 'assets.basic_analytic_dn_rpc_nitf:download', 'assets.analytic:download', 'assets.analytic_dn_xml:download', 'assets.analytic_sr:download', 'assets.basic_analytic_dn_xml:download', 'assets.basic_udm2:download', 'assets.basic_analytic_dn_rpc:download', 'assets.basic_analytic_xml_nitf:download', 'assets.basic_analytic:download', 'assets.udm:download'],
  'geometry': {
    'coordinates': [
      [
        [-122.3006710736394, 37.605388252597656],
        [-122.01624942227079, 37.55570149014945],
        [-122.0364127147522, 37.482963540177245],
        [-122.32141233388535, 37.53313054639475],
        [-122.30471665876881, 37.594329305533236],
        [-122.30373184352291, 37.59415601927027],
        [-122.3006710736394, 37.605388252597656]
      ]
    ],
    'type': 'Polygon'
  },
  'id': '20190530_183307_100c',
  'properties': {
    'acquired': '2019-05-30T18:33:07.320176Z',
    'anomalous_pixels': 0.02,
    'clear_confidence_percent': 79,
    'clear_percent': 93,
    'cloud_cover': 0.07,
    'cloud_percent': 7,
    'columns': 8980,
    'epsg_code': 32610,
    'ground_control': True,
    'gsd': 3.9,
    'heavy_haze_percent': 0,
    'instrument': 'PS2',
    'item_type': 'PSScene4Band',
    'light_haze_percent': 0,
    'origin_x': 559953,
    'origin_y': 4162266,
    'pixel_resolution': 3,
    'provider': 'planetscope',
    'published': '2019-05-31T02:03:28.727Z',
    'quality_category': 'standard',
    'rows': 4460,
    'satellite_id': '100c',
    'shadow_percent': 0,
    'snow_ice_percent': 0,
    'strip_id': '2405105',
    'sun_azimuth': 120.8,
    'sun_elevation': 64.2,
    'updated': '2019-05-31T03:39:55.970Z',
    'usable_data': 0,
    'view_angle': 4.9,
    'visible_confidence_percent': 56,
    'visible_percent': 93
  },
  'type': 'Feature'
}

每个id是图像ID,geometry包含该特定图像的坐标。我想使用这些坐标进行一些计算并返回类似以下的内容。

img_id area
xxxx   nn
yyyy   mm

到目前为止,我正在使用类似以下的python字典。

def strip_id_geom(results):
    d = {}
    for i in results.items_iter(100):
        d['geometry_'+ i['id']] = i['geometry']

    return d

我将按照以下方式转换数据。

def transform_coordinates(img_geojson): 
    co = []
    if 'coordinates' in img_geojson:
        for i in range(len(img_geojson["coordinates"])):
            for j in zip(*img_geojson["coordinates"]):
                co.append(tuple(*j))
    else: 
        raise Exception('No coordinates exist in this geojson')

我的问题是:这是用于此目的的最佳数据结构吗?我的目标是要快速并针对内存使用进行优化。如果有帮助,则数据来自Plante Labs API。

谢谢

def strip_id_geom(results):
    d = {}
    for i in results.items_iter(100):
        d['geometry_'+ i['id']] = i['geometry']

    return d

我将按照以下方式转换数据。

def transform_coordinates(img_geojson): 
    co = []
    if 'coordinates' in img_geojson:
        for i in range(len(img_geojson["coordinates"])):
            for j in zip(*img_geojson["coordinates"]):
                co.append(tuple(*j))
    else: 
        raise Exception('No coordinates exist in this geojson')

img_id area
xxxx   nn
yyyy   mm

0 个答案:

没有答案