无法使 px.choropleth_mapbox 绘制多边形

Can't make px.choropleth_mapbox to draw multipolygon

所以我有一个 geojson file 是一个更大的集合的一部分,但我用这个来测试一些东西。

我读取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)

运行 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'}

然后我将 geojson 文件读入数据框并删除几何列

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

运行 df.info() returns 预期的数据帧结构

<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

由于每个县的唯一独特元素是 DTMN21 我将使用它来制作我的地图:

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()

然而,我在 return 中得到的是一张没有添加任何图层的空白地图,正如您在图像中看到的那样。

我已经尽我所能尝试了一切来解决这个问题,但我还是想不通。非常感谢任何帮助。

免责声明:该解决方案将用于非营利性 NGO 项目。不会将所提供的解决方案用于商业用途

使用的geojson文件中的坐标信息不是Plotly可以使用的格式,需要转换:从ESPG:3763转换为ESPG:4326。使用转换后的 geopandas 数据文件绘制地图。

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()