I use the Mapillary API to get images of cities by coordinates. I make an API call to get the image ID’s that I want:
import mapillary as mly
mly.interface.set_access_token('here_is_my_private_token')
gj = mly.interface.get_image_close_to(longitude=11.49385, latitude=48.05053, radius=50)
print(gj)
This creates a GeoJSON object. The print statement for these example coordinates gives me this:
{'type': 'FeatureCollection', 'features': [{'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.49345874786377, 48.050743224759515]}, 'properties': {'captured_at': 1560688528671, 'compass_angle': 219.55442810059, 'creator_id': 105621578345075, 'id': 156304466376583, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.49367868900299, 48.05092969456331]}, 'properties': {'captured_at': 1560688523518, 'compass_angle': 218.94528198242, 'creator_id': 105621578345075, 'id': 4152401114851048, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493721604347229, 48.0509691400122]}, 'properties': {'captured_at': 1560688522466, 'compass_angle': 216.1079864502, 'creator_id': 105621578345075, 'id': 957272818356791, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493549942970276, 48.05081852995386]}, 'properties': {'captured_at': 1560688526608, 'compass_angle': 216.87623596191, 'creator_id': 105621578345075, 'id': 195685349071104, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493507027626038, 48.0507790843896]}, 'properties': {'captured_at': 1560688527607, 'compass_angle': 217.21737670898, 'creator_id': 105621578345075, 'id': 494493828463470, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493635773658752, 48.05089383503807]}, 'properties': {'captured_at': 1560688524562, 'compass_angle': 217.47109985352, 'creator_id': 105621578345075, 'id': 5440816592658176, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}, {'type': Feature, 'geometry': {'type': Point, 'coordinates': [11.493603587150574, 48.05086514739992]}, 'properties': {'captured_at': 1560688525562, 'compass_angle': 216.0885925293, 'creator_id': 105621578345075, 'id': 916290425872451, 'is_pano': False, 'sequence_id': 'q88pqp6h720xf4tf3x0mut'}}]}
I want a list that contains all the values of the key ‘id’. The rest of the data is not needed. I thought I’d use nested dictionary logic to get these values:
id = gj['features'][0]['properties']['id']
features = gj['features']
This throws:
TypeError: ‘GeoJSON’ object is not subscriptable
I tried standard dictionary attributes:
keys = gj.keys()
values = gj.values()
This throws:
AttributeError: ‘GeoJSON’ object has no attribute ‘keys’, ‘values’…
Apparently a GeoJSON object looks like a dictionary but works differently. How do I get my needed list?
Powknow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You can use [feature['properties']['id'] for feature in gj['features']]
:
_filter = lambda x: [feature['properties']['id'] for feature in x['features']]
gj = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.49345874786377, 48.050743224759515]
},
'properties': {
'captured_at': 1560688528671,
'compass_angle': 219.55442810059,
'creator_id': 105621578345075,
'id': 156304466376583,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.49367868900299, 48.05092969456331]
},
'properties': {
'captured_at': 1560688523518,
'compass_angle': 218.94528198242,
'creator_id': 105621578345075,
'id': 4152401114851048,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.493721604347229, 48.0509691400122]
},
'properties': {
'captured_at': 1560688522466,
'compass_angle': 216.1079864502,
'creator_id': 105621578345075,
'id': 957272818356791,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.493549942970276, 48.05081852995386]
},
'properties': {
'captured_at': 1560688526608,
'compass_angle': 216.87623596191,
'creator_id': 105621578345075,
'id': 195685349071104,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.493507027626038, 48.0507790843896]
},
'properties': {
'captured_at': 1560688527607,
'compass_angle': 217.21737670898,
'creator_id': 105621578345075,
'id': 494493828463470,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.493635773658752, 48.05089383503807]
},
'properties': {
'captured_at': 1560688524562,
'compass_angle': 217.47109985352,
'creator_id': 105621578345075,
'id': 5440816592658176,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
},
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [11.493603587150574, 48.05086514739992]
},
'properties': {
'captured_at': 1560688525562,
'compass_angle': 216.0885925293,
'creator_id': 105621578345075,
'id': 916290425872451,
'is_pano': False,
'sequence_id': 'q88pqp6h720xf4tf3x0mut'
}
}
]
}
print(_filter(gj))
Prints
[156304466376583, 4152401114851048, 957272818356791, 195685349071104, 494493828463470, 5440816592658176, 916290425872451]