Bring Drag & Drop to Dash with Dashboard Engine. 💫 Learn how at our next webinar!

Draw polygons using dash

HI,

I am have a dataframe which these details

||ttype|month|geometry|median| geojson_id|
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|0|LOW|201912|POLYGON ((-79.544 43.59, -79.54401 43.59000995...||21.00|0||
|1|LOW|201912|POLYGON ((-79.67399999999999 43.64899999999999...||17.75|1||
|2|LOW|202006|POLYGON ((-79.59100000000001 43.662, -79.59102...||18.00|2||

…

I need to plot this in a map. Could you please tell me how to do it.
I tried converting to geojson , but my geojson looks like this

{'type': 'FeatureCollection',
 'crs': {'type': 'name',
  'properties': {'name': 'urn:ogc:def:crs:OGC:1.3:CRS84'}},
 'features': [{'type': 'Feature',
   'properties': {'times': '2019-12-01',
    'band_type': 'MED',
    'median_rsrp': 22.75},
   'geometry': {'type': 'Polygon',
    'coordinates': [[[-79.85, 43.711],
      [-79.84997, 43.7110300495],
      [-79.84994, 43.71106019600001]
}

It doesnt have an id field , I referred to this link https://plotly.com/python/choropleth-maps/
but couldnt plot the polygons

Help me fix it

Just add an id field to each of the features:

for i, feature in enumerate(geojson["features"]):
    feature["id"] = str(i)
1 Like

@JayasriCimba Here https://chart-studio.plotly.com/~empet/15779/polygons-overlaying-a-mapbox-map/#/ are presented two methods to draw polygons on a mapbix map.

This helped to create the id’s :slight_smile:

@empet How do I do it for 2400 polygons ? So my geojson has around 2400 polygons in total , am not sure how I loop it.

@JayasriCimba,
With this big number of polygons the best method is the second one. But you need to set up a dataframe with a column containing the ids you added to each feature in geojson['features'], and a numerical column of values associated to polygons. These values will be mapped to colors in a chosen colorscale.
Hence no for loop is needed. From the geojson file and the associated dataframe, as above, you can define fig=go.Choroplethmapbox(), or fig=px.choropleth_mapbox().

it worked , Thank you so much :slight_smile: