简体   繁体   中英

Display a map on streamlit by retrieving the data with an api

I would like to display a map on STREAMLIT by retrieving the data with an api.

I want to use the following result (it gives me the districts for a city in France): https://public.opendatasoft.com/api/records/1.0/search/?dataset=georef-france-iris-millesime&q=Lille&sort=year&facet=year&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.year=2020&refine.com_name=Lille

I try to have this (a geojson):

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

but i have this:

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

I'm looking for but have no idea how to get the data to be recognized to create a map. Do you have any advice on how to convert the result of the api into geojson? Thanks for your help !

Here is how I would generate your geojson polygons from your API results:

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...

Your initial definition of a Polygon seems wrong to me, you should check this link: https://github.com/jazzband/geojson#polygon .

After looking around a bit, I think Streamlit might not be the best option to display your map as it does not seem to support drawing polygons (I might be wrong here). If that is the case, you should have a look at GeoPandas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM