OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to access data in a geojson object (python)

  • Thread starter Thread starter Powknow
  • Start date Start date
P

Powknow

Guest
I have a question where there must be a straightforward solution, but I just don't get it.

I use the mapillary API to get images of different cities via their coordinates. For this I first of all make an API call to get the image ID's that I want. It uses Python and looks like this:

Code:
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 works without trouble and 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'}}]}

Now all I want is to get a list that contains all the values of the key 'id'. The rest of the data is not needed.

I thought I simply use nested dictionary logic to get these values. Also, other Stackoverflow posts show this as the correct way. I therefore tried things like:

Code:
id = gj['features'][0]['properties']['id']
features = gj['features']

And similar stuff. However, this throws 'TypeError: 'GeoJSON' object is not subscriptable'
Or I tried standard dictionary attributes like:

Code:
keys = gj.keys()
values = gj.values()

This throws 'AttributeError: 'GeoJSON' object has no attribute 'keys', 'values'... .

I'm well aware of what these errors mean, but I can't solve them since I have no experience with working with geojson objects. Apparently, a geojson object looks like a dictionary but works differently. Or am I missing something? How do I get my needed list? Thanks for your help, it is much appreciated.
<p>I have a question where there must be a straightforward solution, but I just don't get it.</p>
<p>I use the mapillary API to get images of different cities via their coordinates. For this I first of all make an API call to get the image ID's that I want. It uses Python and looks like this:</p>
<pre><code>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)
</code></pre>
<p>This works without trouble and creates a geojson object. The print statement for these example coordinates gives me this:</p>
<blockquote>
<p>{'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'}}]}</p>
</blockquote>
<p>Now all I want is to get a list that contains all the values of the key 'id'. The rest of the data is not needed.</p>
<p>I thought I simply use nested dictionary logic to get these values. Also, other Stackoverflow posts show this as the correct way. I therefore tried things like:</p>
<pre><code>id = gj['features'][0]['properties']['id']
features = gj['features']

</code></pre>
<p>And similar stuff. However, this throws 'TypeError: 'GeoJSON' object is not subscriptable'<br />
Or I tried standard dictionary attributes like:</p>
<pre><code>keys = gj.keys()
values = gj.values()
</code></pre>
<p>This throws 'AttributeError: 'GeoJSON' object has no attribute 'keys', 'values'... .</p>
<p>I'm well aware of what these errors mean, but I can't solve them since I have no experience with working with geojson objects. Apparently, a geojson object looks like a dictionary but works differently. Or am I missing something? How do I get my needed list?
Thanks for your help, it is much appreciated.</p>
 

Online statistics

Members online
0
Guests online
3
Total visitors
3
Top