Choroplethmapbox does not show

Hi, I am trying to make a go.Choroplethmapbox but waheve I do it turns out blank.

the geodata I use (here) are epsg:4326, the keys are: dict_keys(['type', 'geometry', 'properties']) , and the properties contains a geo ID I can use (fylkesnummer).

When I plot this with a simple df with corresponding ID’s:

fig = go.Figure(go.Choroplethmapbox(z=df['Innbyggere'],
                            locations = df['Fnr'], 
                            geojson = gdf,
                                   ))

fig.update_layout(mapbox_style="carto-positron",
                 mapbox = dict(center= dict(lat=64.5, lon=18.75),            
                               zoom=3
                               ))


fig.show()

I get this (a blank map):

Hi @NRVA,
It doesn’ t show because you didn’ t pass
featureidkey=‘properties.geoID’

to the `go.Choroplethmapbox`  constructor.

Please replace geoID with the right key listed in properties. I didn't understand if this one is the id or the norwegian(?!)  word between parantheses.

Althou you did provide a working solution, I still end up in trouble when I try a different geojson file. epsg is still 4326.

my df and geo df have matching ID’s:

image

To use them together, I set locations to knr and featureidkey to "properties.kommunenum" .

like this:


Notice the map is blank. What is wrong here?

@NRVA
Could you give a link to your geojson file and paste here the header of your dataframe? Otherwise, I cannot express any opinion.

geojson file here

head of df:
image

Thanks

@NRVA

The choroplethmapbox isn’t displayed because your coordinates are given with respect to the CRS (Coordinate Reference System) EPSG :3857:

geojson['features'][0]['geometry']
{'type': 'Polygon',
 'coordinates': [[[277555.855, 7134366.42],
   [286462.22, 7125923.56],
   [293110.65, 7123671.05],
   [300266.29, 7126580.78],
   [302897.8, 7134559.19],
   [294317.97, 7138886.86],
   [292444.54, 7146601.72],
   [283901.246688648, 7152515.57758962],
   [288247, 7147820],
   [280405.99, 7144202.99],
   [277555.855, 7134366.42]]]}

These coordinates aren’t lons and lats.
If you converted a shapefile to geojson, then repeat the conversion process as follows:

import geopandas as gpd
import json

gdf = gpd.read_file("shapefile_name.shp", encoding='utf-8')

file_CRS= gdf.crs
print(file_CRS)
#if the CRS is not epsg:4326, or its equivalent name WGS84
 #then change it as follows:

gdf.to_crs(epsg=4326)
#convert gdf  to geojson:
gdf.to_file('geojsfile.json', driver = 'GeoJSON')
with open('geojsfile.json') as geofile:
    geojson_data = json.load(geofile) 

geojson_data is the geojson dict to be used for choroplethmapbox.

1 Like

You are a lifesaver:)

1 Like