通过使用api检索数据,在streamlight上显示地图



我想通过使用api检索数据来在STREAMLIT上显示地图。

我想使用以下结果(它为我提供了法国一个城市的地区(:https://public.opendatasoft.com/api/records/1.0/search/?dataset=georef-法国鸢尾花&q=里尔&sort=年份&facet=年份&facet=reg_name&facet=dep_name&facet=arrdep_name&facet=ze2020_name&facet=bv2012_name&facet=epci_name&facet=ept_name&facet=com_name&facet=com_arm_name&facet=iris_name&facet=iris_area_code&facet=iris_type&refine.ayear=2020&refine.com_name=里尔

我试着拥有这个(一个geojson(:

POLYGON ((3.069402968274157 50.63987328751279, 3.069467907250858 50.63988940474122,...)

但我有这个:

coordinates": [
[
[
3.061943586904849,
50.636758694822056
],
[
3.061342144816787,
50.63651758657737
],...]]

我正在寻找,但不知道如何获得识别数据来创建地图。关于如何将api的结果转换为geojson,你有什么建议吗?谢谢你的帮助!

以下是我如何从API结果生成geojson多边形:

import json
from geojson import Polygon
# Load the content of the API response
file = open('data.json')
data = json.load(file)
# This array will contain your polygons for each district
polygons = []
# Iterate through the response records
for record in data["records"]:
# This array will contain coordinates to draw a polygon
coordinates = []

# Iterate through the coordinates of the record
for coord in record["fields"]["geo_shape"]["coordinates"][0]:
lon = coord[0] # Longitude
lat = coord[1] # Latitude

# /! Order of lon & lat might be wrong here
coordinates.append((lon, lat))
# Append a new Polygon object to the polygons array
# (Note that there are outer brackets, I'm not sure if you can
# store all polygons in a single Polygon object)
polygons.append(Polygon([coordinates]))
print(polygons)
# Do something with your polygons here...

你对多边形的最初定义对我来说似乎是错误的,你应该检查这个链接:https://github.com/jazzband/geojson#polygon.

环顾四周,我认为Streamlight可能不是显示地图的最佳选项,因为它似乎不支持绘制多边形(我可能错了(。如果是这样的话,你应该看看GeoPandas。

最新更新