Plotly如何从json文件在地图上绘制线条?

2024-10-03 09:17:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个json文件在plotly中在地图上绘制一条线,并不断收到以下错误。plotly网站上的所有示例都使用.csv文件。但是,我想使用json文件。任何帮助都将不胜感激

Error: No valid mapbox style found, please set mapbox.style to one of: open-street-map, white-bg, carto-positron, carto-darkmatter, stamen-terrain, stamen-toner, stamen-watercolor or register a Mapbox access token to use a Mapbox-served style.

Python:

import plotly.graph_objects as go
import pandas as pd
import json

with open('fcRailroad.geojson') as json_file:
    fcRailroad = json.load(json_file)

fig = go.Figure(go.Scattermapbox())

fig.update_layout(mapbox_style="stamen-terrain", 
                mapbox_zoom=10, 
                mapbox_center_lat = 40.58,
                mapbox_center_lon = -105.08,
                margin={"r":0,"t":0,"l":0,"b":0},
                mapbox=go.layout.Mapbox(
                    layers=[{
                        'sourcetype': 'geojson',
                        'source': fcRailroad,
                        'type': 'line',
                    }]
                ))

fig.show()

Json:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {},
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        -825.0913953781128,
                        40.49348616373978
                    ],
                    [
                        -825.0906443595885,
                        40.49508532104079
                    ],
                    [
                        -825.0863313674927,
                        40.502411585011934
                    ]
                ]
            }
        }
    ]
}

Tags: 文件toimportjsongostyleastype
1条回答
网友
1楼 · 发布于 2024-10-03 09:17:47

您没有正确读取json文件。您试图将“fcrailway.json”读入json。而且它显然不是有效的json。以下是从文件加载内容的方式:

with open('fcRailroad.json') as json_file:
    fcRailroad = json.load(json_file)

相关问题 更多 >