Can't make px.choropleth_mapbox to draw multipolygon

Viewed 83

So I have a geojson file that is part of a bigger collection, but I'm using this one to test something out.

I have no problem in reading the geojson:

import pandas as pd
import geopandas as gpd 
import json
import plotly.express as px 
with open('/content/cartaxo.geojson') as response:
  county = json.load(response)

Running county["features"][0] returns

{'geometry': {'coordinates': [[[[-56497.98620000016, -56294.48699999973],
     [-56538.73869999964, -56197.87010000087],
     [-56587.94560000021, -56286.044600000605],
     [-56616.04860000033, -56336.403999999166],
     [-56627.22609999962, -56356.43209999986],
     [-56643.096699999645, -56384.87030000053],
     [-56658.49230000004, -56412.45839999989],
     [-56674.48390000034, -56442.55460000038],
     [-56690.07959999982, -56471.9068],
     [-56658.87579999957, -56473.55159999989],
     [-56580.009200000204, -56474.78099999949],
     [-56546.001299999654, -56474.57569999993],
     [-56531.39639999997, -56474.36959999986],
     [-56473.800599999726, -56470.95419999957],
     [-56473.39460000023, -56468.96419999935],
     [-56471.68439999968, -56463.26009999961],
     [-56442.968500000425, -56420.05379999988],
     [-56441.460500000045, -56419.04780000076],
     [-56449.69900000002, -56399.976800000295],
     [-56456.69560000021, -56383.77690000087],
     [-56462.82129999995, -56370.92889999971],
     [-56488.1409, -56317.830000000075],
     [-56497.98620000016, -56294.48699999973]]]],
  'type': 'MultiPolygon'},
 'properties': {'BGRI2021': '14060900412',
  'CC': '06',
  'DT': '14',
  'DT21': '14',
  'DTMN21': '1406',
  'DTMNFR21': '140609',
  'DTMNFRSEC21': '140609004',
  'N_AGREGADOS': 0,
  'N_ALOJAMENTOS': 1,
  'N_EDIFICIOS_CLASSICOS': 1,
  'N_INDIVIDUOS_RESIDENT': 0,
  'OBJECTID': 1,
  'OBJECTID_1': 149773,
  'SEC': '004',
  'SECNUM21': '004',
  'SECSSNUM21': '00412',
  'SS': '12',
  'SSNUM21': '12',
  'fr': '09'},
 'type': 'Feature'}

I then read the geojson file into a dataframe and drop the geometry column

df = gpd.read_file('/content/cartaxo.geojson')
df = df.drop(['geometry'], axis=1)

Running df.info() returns the structure of the dataframe as expected

<class 'geopandas.geodataframe.GeoDataFrame'>
RangeIndex: 514 entries, 0 to 513
Data columns (total 19 columns):
 #   Column                 Non-Null Count  Dtype 
---  ------                 --------------  ----- 
 0   OBJECTID               514 non-null    int64 
 1   BGRI2021               514 non-null    object
 2   DT21                   514 non-null    object
 3   DTMN21                 514 non-null    object
 4   DTMNFR21               514 non-null    object
 5   DTMNFRSEC21            514 non-null    object
 6   SECNUM21               514 non-null    object
 7   SSNUM21                514 non-null    object
 8   SECSSNUM21             514 non-null    object
 9   OBJECTID_1             514 non-null    int64 
 10  DT                     514 non-null    object
 11  CC                     514 non-null    object
 12  fr                     514 non-null    object
 13  SEC                    514 non-null    object
 14  SS                     514 non-null    object
 15  N_EDIFICIOS_CLASSICOS  514 non-null    int64 
 16  N_ALOJAMENTOS          514 non-null    int64 
 17  N_AGREGADOS            514 non-null    int64 
 18  N_INDIVIDUOS_RESIDENT  514 non-null    int64 
dtypes: int64(6), object(13)
memory usage: 76.4+ KB

Since the only unique element for each county is DTMN21 I will use that to make my map:

fig = px.choropleth_mapbox(df, geojson=county,locations='DTMN21',featureidkey='properties.DTMN21',color='N_INDIVIDUOS_RESIDENT',
                            color_continuous_scale="Blues",
                          mapbox_style="open-street-map",
                          zoom=7,
                          opacity=1,
                          height=900
                          )
fig.show()

However what I get in return is a blank map with no added layers to it as you can see in the image. Map with no additional info

I've already tried everything to sort this out, as much as my knowledge can, and I can't figure it out. Any help with be much appreciated.

Disclaimer: The solution will be used in a non-for-profit NGO project. No commercial use will be made of the solution presented

1 Answers

The coordinate information in the geojson file used is not in a format that can be used by Plotly and needs to be converted: convert from ESPG:3763 to ESPG:4326. Draw a map with the converted geopandas data file.

import pandas as pd
import geopandas as gpd 
import json
import plotly.express as px
import pyproj

with open('./data/cartaxo.geojson') as response:
    county = json.load(response)
gdf = gpd.read_file('./data/cartaxo.geojson')
#df = df.drop(['geometry'], axis=1)
gdf.to_crs(epsg=4326, inplace=True)

gdf.crs

Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

gdf.set_index('BGRI2021', inplace=True)
fig = px.choropleth_mapbox(gdf,
                           geojson=gdf['geometry'],
                           locations=gdf.index,
                           color='N_INDIVIDUOS_RESIDENT',
                           color_continuous_scale="Blues",
                           mapbox_style="open-street-map",
                           center={'lat':39.16022, 'lon':-8.78741},
                           zoom=10,
                           opacity=1,
                           height=900
                          )
fig.show()

enter image description here

Related