我正在尝试使用自定义geojson文件显示一个scattermapbox,但是我很难获取要渲染的地图。
如果将图层的source
设置为托管数据的网址,则可以很好地渲染geojson文件。下面提供了一个示例以及一张图片:
...
app.layout = html.Div( [ dcc.Graph(
id = 'TxWCD-choropleth',
figure = dict(
data = [dict(
type = 'scattermapbox'
)],
layout = dict(
mapbox = dict(
layers =[ layer_t ],
accesstoken = mapbox_access_token,
center = dict(
lat = 31.3,
lon = -99.2
),
zoom = 5.2,
style='light'
),
height = 900,
autosize = True
)
)
)])
...
但是,我想先将json下载到python中的对象中,以便我可以过滤和自定义数据。但是,当我尝试执行此操作时,地图将挂起并且永远不会渲染。每当我在浏览器中打开检查器时,也不会出错。以下代码重新创建了我的问题以及错误图片:
import dash
import json
import os
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import geopandas as gpd
# public token
mapbox_access_token = MAPBOX_TOKEN
app = dash.Dash(__name__)
pf = pd.read_json( "https://raw.githubusercontent.com/TNRIS/tx.geojson/master/counties/tx_counties.geojson" )
layer_t = dict(
sourcetype = "geojson",
type = "fill",
color = "#265465",
source = json.loads( pf.to_json() )
)
app.layout = html.Div( [ dcc.Graph(
id = 'TxWCD-choropleth',
figure = dict(
data = [dict(
type = 'scattermapbox'
)],
layout = dict(
mapbox = dict(
layers =[ layer_t ],
accesstoken = mapbox_access_token,
center = dict(
lat = 31.3,
lon = -99.2
),
zoom = 5.2,
style='light'
),
height = 900,
autosize = True
)
)
)])
if __name__ == '__main__':
app.run_server( port = 8051, debug = True )
我尝试按照线程here进行操作,以查看执行的操作中是否存在任何错误,但我认为自己计算正确。我也here,但是与示例相比,我的代码没有出现问题。
任何帮助将不胜感激。
谢谢!