Skip to content

Forum in maintenance, we will back soon 🙂

Covert JSON to GeoJ...
 
Notifications
Clear all

Covert JSON to GeoJSON

2 Posts
2 Users
2 Reactions
30 Views
(@etienne-araya)
Posts: 1
New Member Customer
Topic starter
 

Prompt: I want to convert using a python script a JSON to GEOJSON of locations where latitude and longitude have the coordinates of each point. here is the sample JSON to convert ===> "{data": { "licensePlate": "ABCDEFG", "locations": [ { "latitude": -33.381247, "longitude": -70.542402, "date": "2023-04-23 18:36:53", "speed": 5.0, "heading": 101, "altitude": 0 } //you can add more locations here. ]} }<=== 

 

Output: 

import json

# JSON original

json_data = {  "data": { "licensePlate": "RHGBA18", "locations": [ { "latitude": -33.381247, "longitude": -70.542402,  "date": "2023-04-23 18:36:53", "speed": 5.0, "heading": 101, "altitude": 0 }, { "latitude": -33.381275, "longitude": -70.542338, "date": "2023-04-23 18:36:54", "speed": 12.0, "heading": 129, "altitude": 0 }  ] } }

# new object that has GeoJSON format

geojson = { "type": "FeatureCollection", "features": [] }

# for each point, we built a Feature of GeoJSON

for pos in json_data["data"]["positions"]:

feature = { "type": "Feature", "geometry": { "type": "Point", "coordinates": [pos["longitude"], pos["latitude"]] }, "properties": { "date": pos["date"], "speed": pos["speed"], "heading": pos["heading"], "altitude": pos["altitude"],  "licensePlate": json_data["data"]["licensePlate"] } }

geojson["features"].append(feature)

# Finally, convert  GeoJSON to string 

geojson_str = json.dumps(geojson)

print(geojson_str)

 

 

 

 
Posted : 07/08/2023 11:41 pm
SSAdvisor reacted
Hasan Aboul Hasan
(@admin)
Posts: 1276
Member Admin
 

Thanks for sharing. Did you try it? What were the results?

 
Posted : 07/09/2023 6:47 am
SSAdvisor reacted
Share:
[the_ad_group id="312"]