导出 Plotly scatter 为 kml - python

问题描述 投票:0回答:2

是否可以将 Plotly 散点图导出为 kml 文件?我在下面有一个使用

matplotlib
的示例,但是是否可以使用 Plotly 执行相同的输出?

Plotly 图是一个散点图。它可以转换为 kml 输出吗?

import plotly.express as px
import geopandas as gpd
import simplekml
import matplotlib.pyplot as ppl
from pylab import rcParams

##matplotlib
rcParams['figure.figsize'] = (8,8)
# create rectangle over 0 to 10 degrees longitude and 0 to 10 degrees latitude
x = [0, 10, 10, 0, 0]
y = [10, 10, 0, 0, 10]
x1 = range(0,11)    # to draw a diagonal line

fig = ppl.figure(1)
ax = fig.add_axes([0,0,1,1])
ax.axis('off')
fig.patch.set_facecolor('blue')  # so we can see the true extent

ppl.plot(x, y, 'r', linewidth=3)
ppl.plot(x, y, '.b', linewidth=3)
ppl.plot(x1, x1, 'g', linewidth=3)

ppl.axis('off')
border1 = ppl.axis()

if False:
    ppl.show()
else:
    pngName = 'Overlay.png'
    fig.savefig(pngName, facecolor=fig.get_facecolor(), transparent=False)

bottomleft  = (border1[0],border1[2])
bottomright = (border1[1],border1[2])
topright    = (border1[1],border1[3])
topleft     = (border1[0],border1[3])

kml = simplekml.Kml()
ground = kml.newgroundoverlay(name='GroundOverlay')
ground.icon.href = pngName
ground.gxlatlonquad.coords =[bottomleft, bottomright, topright, topleft]
kml.save("GroundOverlay.kml")

##plotly
df = px.data.gapminder()
fig = px.area(df, x="year", y="pop", color="continent", line_group="country")

gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))

gdf['LON'] = gdf['geometry'].x
gdf['LAT'] = gdf['geometry'].y

fig = px.scatter_mapbox(data_frame = gdf, 
                               lat = 'LAT', 
                               lon = 'LON',
                               zoom = 1,
                               mapbox_style = 'carto-positron', 
                               )

fig.show()
python matplotlib plotly kml
2个回答
0
投票

理论上,您应该能够(借助其他库的一些帮助,例如 plotly.subplot

使用 plotly.subplot 的 plotly_figure_to_gdf() 函数,您可以将 Plotly 散点图转换为 GeoDataFrame.

这将返回一个带有点数据的GeoDataFrame,您可以使用它们使用simplekml库创建一个KML文件

也许你可以尝试这样的事情?

import plotly.subplots as sp
import plotly.graph_objs as go
import geopandas as gpd
import simplekml

# Example Plotly scatter figure
fig = go.Figure(go.Scattermapbox(
    latitude=["45.5231"],
    longitude=["-122.6765"],
    mode="markers",
    marker=go.scattermapbox.Marker(size=12),
))

# Convert Plotly figure to a GeoDataFrame
gdf = sp.plotly_figure_to_gdf(fig)

# Create the KML file
kml = simplekml.Kml()
for index, row in gdf.iterrows():
    kml.newpoint(name=str(index), coords=[(row['geometry'].x, row['geometry'].y)])
kml.save("scatter.kml")

0
投票

如果您想从“naturalearth_cities”数据集生成 KML 作为一组具有点几何的地标,您可以使用 to_file() 函数直接从 GeoDataFrame 创建 KML。

注意 KML 在 Fiona(geopandas 使用)中默认禁用,因此必须在使用前启用它。

将 GeoDataFrame 转换为 KML 的 Python 代码:

import geopandas as gpd

gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_cities"))

# enable KML driver which is disable by default
gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = "rw"

# With newer versions of Fiona, you might need to use libkml
gpd.io.file.fiona.drvsupport.supported_drivers['LIBKML'] = "rw"

gdf.to_file('test.kml', driver='KML')

输出:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document id="root_doc">
...
<Placemark id="test.200">
        <name>Paris</name>
        <ExtendedData>
          <SchemaData schemaUrl="#test.schema">
            <SimpleData name="LON">2.33138946713035</SimpleData>
            <SimpleData name="LAT">48.8686387898146</SimpleData>
          </SchemaData>
        </ExtendedData>
        <Point>
          <coordinates>
            2.33138946713035,48.8686387898146,0
          </coordinates>
        </Point>
...

如果您想向 KML 输出添加自定义样式,您可以迭代 GeoDataFrame 并使用 simplekml 创建 KML。

© www.soinside.com 2019 - 2024. All rights reserved.