GeoJSON Error : Uncaught (in promise) Error: Unexpected error while fetching from {"type": "FeatureCollection",

GeoJSON Error : Uncaught (in promise) Error: Unexpected error while fetching from {"type": "FeatureCollection",

我正在尝试使用 plotly 和 geojson 数据绘制 Choropleth。虽然我能够使用 mapbox 绘制底图,但缺少用于表示空间数据的彩色多边形。

代码如下:

import pandas as pd 
import geopandas as gpd
import mapbox
from plotly import graph_objs as go
from plotly.graph_objs import *

df_geo = gpd.read_file('https://raw.githubusercontent.com/uscensusgeo.geojson')

# CRS
df_geo.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 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich


import plotly.graph_objects as go

fig = go.Figure(go.Choroplethmapbox(geojson=df_geo['geometry'].to_json(), 
                                    locations=df_geo['TRACTCE'], 
                                    z=df_geo['ALAND'],
                                    colorscale="Viridis", 
                                    zmin=df_geo['ALAND'].min(), 
                                    zmax=df_geo['ALAND'].max(), 
                                    marker_line_width=0))

fig.update_layout(mapbox_style="light", 
                  mapbox_accesstoken=token,
                  mapbox_zoom=3, 
                  mapbox_center = {"lat": 37.0902, "lon": -95.7129},
                  margin={"r":0,"t":0,"l":0,"b":0})

fig.update_layout()
fig.show()

地图中缺少彩色多边形。我在浏览器控制台中收到此错误:

Uncaught (in promise) Error: Unexpected error while fetching from {"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[-81.375782, 28.411135], [-81.372696, 28.421524], [-81.368127, 28.43621], [-81.366996, 28.440684], [-81.366866, 28.445807], [-81.364976, 28.4458], [-81.361445, 28.445845], [-81.358309, 28.446668], [-81.350651, 28.450405], [-81.351092, 28.449687], [-81.349923, 28.449939],

我也尝试从文档 [1] 中引用不同的文件,但我仍然遇到同样的错误。

[1] https://plotly.com/python/mapbox-county-choropleth/

据我了解,官方参考中的示例使用FIPS,因此不需要fueatureidkey,但如果您使用不同的geojson,则需要设置一个将geojson与用户数据相关联的密钥。多边形信息的内容我们还没有核实过,但是我们确认地图放大时描绘的是一个特定的区域。

from urllib.request import urlopen
import json
import geopandas as gpd
import plotly.graph_objects as go

url = 'https://raw.githubusercontent.com/uscensusgeo.geojson'

with urlopen(url) as response:
    geo_json = json.load(response)

df_geo = gpd.read_file(url)

mapbox_access_token = open("mapbox_api_key.txt").read()

fig = go.Figure(go.Choroplethmapbox(geojson=geo_json,
                                    locations=df_geo['TRACTCE'], 
                                    z=df_geo['ALAND'],
                                    featureidkey='properties.TRACTCE',
                                    colorscale="Viridis", 
                                    zmin=df_geo['ALAND'].min(), 
                                    zmax=df_geo['ALAND'].max(), 
                                    marker_line_width=0))

fig.update_layout(mapbox_style="light", 
                  mapbox_accesstoken=mapbox_access_token,
                  mapbox_zoom=3, 
                  mapbox_center = {"lat": 37.0902, "lon": -95.7129},
                  margin={"r":0,"t":0,"l":0,"b":0})

fig.update_layout()
fig.show()