Python 使用Plotly可视化通用GeoJson文件

Python 使用Plotly可视化通用GeoJson文件,python,plotly,Python,Plotly,我想绘制GeoJson文件中包含的一些多边形。是否有可能在Plotly中可视化未直接链接到真实位置的GeoJson文件 例如,我可以使用GeoPandas绘制通用GeoJson文件: 导入json geodata=json.loads( “{”类型“:“FeatureCollection”, “特点”:[ {“类型”:“特征”, “几何体”:{“类型”:“多边形”,“坐标”:[[[0,0],[0,1],[1,1]]}, “属性”:{“id”:“左上方”} }, {“类型”:“特征”, “几何体”

我想绘制GeoJson文件中包含的一些多边形。是否有可能在Plotly中可视化未直接链接到真实位置的GeoJson文件

例如,我可以使用GeoPandas绘制通用GeoJson文件:

导入json
geodata=json.loads(
“{”类型“:“FeatureCollection”,
“特点”:[
{“类型”:“特征”,
“几何体”:{“类型”:“多边形”,“坐标”:[[[0,0],[0,1],[1,1]]},
“属性”:{“id”:“左上方”}
},
{“类型”:“特征”,
“几何体”:{“类型”:“多边形”,“坐标”:[[[0,0],[1,1],[1,0]]},
“属性”:{“id”:“右下”}
}
]
}""")
作为gpd导入geopandas
df_shapes=gpd.GeoDataFrame.from_要素(geodata[“要素”])
df_shapes.plot(color=“无”)
结果显示GeoJson中包含的两个多边形(三角形):

如何使用Plotly绘制相同的地图?建议使用范围限制显示的底图。如果没有底图,该怎么办

(我不是问如何用直线绘制正方形。GeoJson只是一个简化的示例。)

可以绘制

使用痕迹 然后是一个列表/dict理解的例子,将geojson多边形重新构造为绘图结构

import json

geodata = json.loads(
    """{ "type": "FeatureCollection",
    "features": [
      { "type": "Feature",
        "geometry": {"type": "Polygon", "coordinates": [[[0,0],[0,1],[1,1]]]},
        "properties": {"id": "upper_left"}
      },
      { "type": "Feature",
        "geometry": {"type": "Polygon", "coordinates": [[[0,0],[1,1],[1,0]]]},
        "properties": {"id": "lower_right"}
      }
    ]
}"""
)

go.Figure(
    [
        go.Scatter(
            **{
                "x": [p[0] for p in f["geometry"]["coordinates"][0]],
                "y": [p[1] for p in f["geometry"]["coordinates"][0]],
                "fill": "toself",
                "name": f["properties"]["id"],
            }
        )
        for f in geodata["features"]
    ]
).update_layout(height=200, width=200, showlegend=False, margin={"l":0,"r":0,"t":0,"b":0})

使用
形状
  • 使用geopandas几何图形获取SVG,然后提取路径
  • 将这些多边形作为形状添加到布局中
from bs4 import BeautifulSoup

# input to plotly is path.  use shapely geometry svg path for this
df_shapes = df_shapes.assign(
    svgpath=df_shapes["geometry"].apply(
        lambda p: BeautifulSoup(p.svg()).find("path")["d"]
    )
)

go.Figure(
    layout=dict(
        height=200,
        width=200,
        showlegend=False,
        margin={"l": 0, "r": 0, "t": 0, "b": 0},
        xaxis={"range": [0, 1]},
        yaxis={"range": [0, 1]},
        shapes=[{"type": "path", "path": p} for p in df_shapes["svgpath"]],
    )
)