Received geo data of Riyadh traffic from HERE api as shape points, how to load them into geopandas?

109 Views Asked by At

I'm new to geospatial data, and I've collected some traffic flow data of Riyadh using HERE API by using this request url:

requests.get(f"https://data.traffic.hereapi.com/v7/flow?locationReferencing=shape&in=bbox:46.278534,24.377121,47.040710,25.084355&advancedFeatures=deepCoverage&apiKey={HERE['apikey']}")

they have 3 ways of describing a location, check here: https://developer.here.com/documentation/traffic-api/dev_guide/topics/concepts/location-referencing.html, and the only one that shows coordinates in the response is Shape Points. I need the response with coordinates in order to manipulate and visualize the data using geopandas and add more data to every linestring. however, the response goes something like this:

>     {"sourceUpdated": "2022-10-06T14:28:08Z", "results": [{"location": {"description": "\u0634\u0627\u0631\u0639
> \u0627\u0644\u0634\u064a\u062e \u062e\u0644\u064a\u0641\u0629
> \u0628\u0646 \u0623\u062d\u0645\u062f \u0622\u0644
> \u062b\u0627\u0646", "length": 288.0, "shape": {"links": [{"points":
> [{"lat": 24.82835004106164, "lng": 46.88801002688706}, {"lat":
> 24.828669978305697, "lng": 46.887639965862036}], "length": 52.0}, {"points": [{"lat": 24.828669978305697, "lng": 46.887639965862036},
> {"lat": 24.828799981623888, "lng": 46.88746998086572}], "length":
> 22.0}, {"points": [{"lat": 24.828799981623888, "lng": 46.88746998086572}, {"lat": 24.829199966043234, "lng": 46.886930018663406}], "length": 70.0}, {"points": [{"lat": 24.829199966043234, "lng": 46.886930018663406}, {"lat": 24.82987998984754, "lng": 46.88601999543607}], "length": 119.0}, {"points": [{"lat": 24.82987998984754, "lng": 46.88601999543607},
> {"lat": 24.830019967630506, "lng": 46.885829977691174}], "length":
> 25.0}]}}, "currentFlow": {"speed": 8.333334, "speedUncapped": 8.333334, "freeFlow": 9.722222, "jamFactor": 0.6, "confidence": 0.71, "traversability": "open"}}, {"location": {"description":
> "\u0634\u0627\u0631\u0639 \u0627\u0644\u0635\u062d\u0627\u0628\u0629",
> "length": 1103.0, "shape": {"links": [{"points": [{"lat":
> 24.803550001233816, "lng": 46.769810020923615}, {"lat": 24.803940011188388, "lng": 46.770779974758625}], "length": 107.0}, {"points": [{"lat": 24.803940011188388, "lng": 46.770779974758625},
> {"lat": 24.804060040041804, "lng": 46.77109002135694}, {"lat":
> 24.804279981181026, "lng": 46.77162000909448}], "length": 93.0}, {"points": [{"lat": 24.804279981181026, "lng": 46.77162000909448},
> {"lat": 24.804349970072508, "lng": 46.77179996855557}], "length":
> 20.0}, {"points": [{"lat": 24.804349970072508, "lng": 46.77179996855557}, {"lat": 24.80441995896399, "lng": 46.771959979087114}], "length": 18.0}, {"points": [{"lat": 24.80441995896399, "lng": 46.771959979087114}, {"lat": 24.80476001277566, "lng": 46.77283002063632}], "length": 96.0}, {"points": [{"lat": 24.80476001277566, "lng": 46.77283002063632},
> {"lat": 24.80492999777198, "lng": 46.77323997952044}], "length":
> 45.0}, {"points": [{"lat": 24.80492999777198, "lng": 46.77323997952044}, {"lat": 24.805459985509515, "lng": 46.77444001659751}], "length": 135.0}, {"points": [{"lat": 24.805459985509515, "lng": 46.77444001659751}, {"lat": 24.805540032684803, "lng": 46.77461000159383}], "length": 19.0}, {"points": [{"lat": 24.805540032684803, "lng": 46.77461000159383},
> {"lat": 24.805650003254414, "lng": 46.77487998269498}], "length":
> 30.0}, {"points": [{"lat": 24.805650003254414, "lng": 46.77487998269498}, {"lat": 24.805780006572604, "lng": 46.77513001486659}], "length": 29.0}, {"points": [{"lat": 24.805780006572604, "lng": 46.77513001486659}, {"lat": 24.805840020999312, "lng": 46.77526001818478}], "length": 15.0}, {"points": [{"lat": 24.805840020999312, "lng": 46.77526001818478},
> {"lat": 24.806040013208985, "lng": 46.77575002424419}], "length":
> 54.0}, {"points": [{"lat": 24.806040013208985, "lng": 46.77575002424419}, {"lat": 24.806360034272075, "lng": 46.7765299603343}], "length": 86.0}, {"points": [{"lat": 24.806360034272075, "lng": 46.7765299603343}, {"lat": 24.80663001537323, "lng": 46.777179976925254}], "length": 72.0}, {"points": [{"lat": 24.80663001537323, "lng": 46.777179976925254},
> {"lat": 24.807410035282373, "lng": 46.77894998341799}], "length":
> 198.0}, {"points": [{"lat": 24.807410035282373, "lng": 46.77894998341799}, {"lat": 24.807629976421595, "lng": 46.77946999669075}], "length": 58.0}, {"points": [{"lat": 24.807629976421595, "lng": 46.77946999669075}, {"lat": 24.80772997252643, "lng": 46.77972002886236}], "length": 28.0}]}}, "currentFlow": {"speed": 8.611112, "speedUncapped": 8.611112,
> "freeFlow": 10.555556, "jamFactor": 1.7, "confidence": 0.94,
> "traversability": "open", "subSegments": [{"length": 819.0, "speed":
> 10.277778, "speedUncapped": 10.277778, "freeFlow": 10.0, "jamFactor": 0.0, "confidence": 0.94, "traversability": "open"}, {"length": 284.0, "speed": 6.111111, "speedUncapped": 6.111111, "freeFlow": 11.388889,
> "jamFactor": 3.1, "confidence": 0.94, "traversability": "open"}]}},
> {"location": {"description": "\u0634\u0627\u0631\u0639
> \u0627\u0644\u0628\u0631\u0642", "length": 180.0, "shape": {"links":
> [{"points": [{"lat": 24.808140015229583, "lng": 46.886649979278445},
> {"lat": 24.808549974113703, "lng": 46.88592997379601}], "length":
> 86.0}, {"points": [{"lat": 24.808549974113703, "lng": 46.88592997379601}, {"lat": 24.80871995911002, "lng": 46.885659992694855}], "length": 33.0}, {"points": [{"lat": 24.80871995911002, "lng": 46.885659992694855}, {"lat": 24.808839987963438, "lng": 46.885469974949956}], "length": 23.0}, {"points": [{"lat": 24.808839987963438, "lng": 46.885469974949956},
> {"lat": 24.808960016816854, "lng": 46.88526998274028}], "length":
> 24.0}, {"points": [{"lat": 24.808960016816854, "lng": 46.88526998274028}, {"lat": 24.809030005708337, "lng": 46.88516001217067}], "length": 14.0}]}}, "currentFlow": {"speed": 5.277778, "speedUncapped": 5.277778, "freeFlow": 8.055555, "jamFactor": 2.5, "confidence": 0.9, "traversability": "open"}},
> {"location": {"description": "\u0634\u0627\u0631\u0639
> \u0627\u0644\u0646\u062f\u0648\u0629", "length": 19.0, "shape":
> {"links": [{"points": [{"lat": 24.80036001652479, "lng":
> 46.892340034246445}, {"lat": 24.800450038164854, "lng": 46.8921800237149}], "length": 19.0}]}}, "currentFlow": {"speed": 7.777778, "speedUncapped": 7.777778, "freeFlow": 7.777778, "jamFactor": 0.0, "confidence": 0.84, "traversability": "open"}}, ...
> ] }

correct me if I'm wrong, it is a messy data that needs post processing manually in order to have it supported by geopandas -fiona drivers- which are:

{'ARCGEN': 'r', 'DXF': 'rw', 'CSV': 'raw', 'OpenFileGDB': 'r', 'ESRIJSON': 'r', 'ESRI Shapefile': 'raw', 'FlatGeobuf': 'rw', 'GeoJSON': 'raw', 'GeoJSONSeq': 'rw', 'GPKG': 'raw', 'GML': 'rw', 'OGR_GMT': 'rw', 'GPX': 'rw', 'GPSTrackMaker': 'rw', 'Idrisi': 'r', 'MapInfo File': 'raw', 'DGN': 'raw', 'PCIDSK': 'raw', 'OGR_PDS': 'r', 'S57': 'r', 'SQLite': 'raw', 'TopoJSON': 'r'}

I really would be happy if you show me a way that I can get the data in any supported format, or preferably in geojson.

and another question, is their location referencing static? I mean, for every time I request the traffic data, do I get the same segments of roads but with different traffic data or does the segmentation change depending on traffic?

Thank you!

1

There are 1 best solutions below

1
Pankaj Kashyap On

The HERE apis are returning the data in the JSON format. The data should be processed at application level based on the use-case.

And the answer to second query related to segments and traffic. The Segments will not change, only the traffic data will be updated.