简体   繁体   中英

Extract specific keys from list of dict in python. Sentinelhub

I seem to be stuck on very simple task. I'm still dipping my toes into Python.

I'm trying to download Sentinel 2 Images with SentinelHub API: SentinelHub

The result of data that my code returns is like this:

{'geometry': {'coordinates': [[[[35.895906644, 31.602691754],
     [36.264307655, 31.593801516],
     [36.230618703, 30.604681346],
     [35.642363693, 30.617971909],
     [35.678587829, 30.757888786],
     [35.715700562, 30.905919341],
     [35.754290061, 31.053632806],
     [35.793289298, 31.206946419],
     [35.895906644, 31.602691754]]]],
  'type': 'MultiPolygon'},
 'id': 'ee923fac-0097-58a8-b861-b07d89b99310',
 'properties': {'**productType**': '**S2MSI1C**',
  'centroid': {'coordinates': [18.1321538275, 31.10368655], 'type': 'Point'},
  'cloudCover': 10.68,
  'collection': 'Sentinel2',
  'completionDate': '2017-06-07T08:15:54Z',
  'description': None,
  'instrument': 'MSI',
  'keywords': [],
  'license': {'description': {'shortName': 'No license'},
   'grantedCountries': None,
   'grantedFlags': None,
   'grantedOrganizationCountries': None,
   'hasToBeSigned': 'never',
   'licenseId': 'unlicensed',
   'signatureQuota': -1,
   'viewService': 'public'},
  'links': [{'href': 'http://opensearch.sentinel-hub.com/resto/collections/Sentinel2/ee923fac-0097-58a8-b861-b07d89b99310.json?&lang=en',
    'rel': 'self',
    'title': 'GeoJSON link for ee923fac-0097-58a8-b861-b07d89b99310',
    'type': 'application/json'}],
  'orbitNumber': 10228,
  'organisationName': None,
  'parentIdentifier': None,
  'platform': 'Sentinel-2',
  'processingLevel': '1C',
  'productIdentifier': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
  'published': '2017-07-26T13:09:17.405352Z',
  'quicklook': None,
  'resolution': 10,
  's3Path': 'tiles/36/R/YV/2017/6/7/0',
  's3URI': 's3://sentinel-s2-l1c/tiles/36/R/YV/2017/6/7/0/',
  'sensorMode': None,
  'services': {'download': {'mimeType': 'text/html',
    'url': 'http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com#tiles/36/R/YV/2017/6/7/0/'}},
  'sgsId': 2168915,
  'snowCover': 0,
  'spacecraft': 'S2A',
  'startDate': '2017-06-07T08:15:54Z',
  'thumbnail': None,
  'title': 'S2A_OPER_MSI_L1C_TL_SGS__20170607T120016_A010228_T36RYV_N02.05',
  'updated': '2017-07-26T13:09:17.405352Z'},
 'type': 'Feature'}

Can you explain how can I iterate through this set of data and extract only 'productType'? For example, if there are several similar data sets it would return only different product types.

My code is :

 import matplotlib.pyplot as plt
    import numpy as np
    from sentinelhub import AwsProductRequest, AwsTileRequest, AwsTile, BBox, CRS
    betsiboka_coords_wgs84 = [31.245117,33.897777,34.936523,36.129002]
    bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
    date= '2017-06-05',('2017-06-08')
    data=sentinelhub.opensearch.get_area_info(bbox, date_interval=date, maxcc=None)
for i in data:
    print(i)

Based on what you have provided, replace your bottom for loop:

for i in data:
  print(i)

with the following:

for i in data:
  print(i['properties']['**productType**'])

If you want to access only the propertyType you can use i['properties']['productType'] in your for loop. If you want to access it any time you want without writing each time those keys, you can define a generator like this:

def property_types(data_array):
    for data in data_array
        yield data['properties']['propertyType']

So you can use it like this in a loop (your data_array is data, as returned by sentinelhub api):

for property_type in property_types(data):
    # do stuff with property_type
keys = []  
for key in d.keys():
     if key == 'properties':
         for k in d[key].keys():
            if k == '**productType**' and k not in keys:         
            keys.append(d[key][k])
print(keys)
  1. Getting only specific (nested) values: Since your request key is nested, and resides inside the parent "properties" object, you need to access it first, preferably using the get method. This can be done as follows (note the '{}' parameter in the first get, this returns an empty dictionary if the first key is not present)
 data_dictionary = json.loads(data_string) product_type = data_dictionary.get('properties', {}).get('**productType**') 
  1. You can then aggregate the different product_type objects in a set , which will automatically guarantee that no 2 objects are the same
 product_type_set = set() product_type.add(product_type) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM