将Plotly scatter导出为kml - python

fdbelqdn  于 2023-05-05  发布在  Python
关注(0)|答案(2)|浏览(151)

bounty还有6天到期。回答此问题可获得+50声望奖励。jonboy希望引起更多关注这个问题。

是否可以将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()
zbq4xfa0

zbq4xfa01#

Plotly没有将散点图导出为kml文件的直接函数,这与您提供的示例中的simplekml模块不同。但是,您可以使用plotly_to_gis包将Plotly散点图转换为GeoDataFrame,后者可以使用geopandas模块导出为kml文件。
以下是将Plotly散点图转换为GeoDataFrame并将其导出为kml文件的示例代码:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.io as pio
import pandas as pd
import geopandas as gpd

# create a scatter plot figure using Plotly
fig = make_subplots(rows=1, cols=1)
fig.add_trace(go.Scatter(x=[-118, -119, -120], y=[34, 35, 36], mode='markers'))
fig.update_layout(mapbox_style="open-street-map", mapbox_center_lon=-119, mapbox_center_lat=35, mapbox_zoom=5)
pio.show(fig)

# convert the figure to a GeoDataFrame
data = pio.to_image(fig, format='png')
df = pd.DataFrame({'geometry': [f'POINT ({-118} {34})', f'POINT ({-119} {35})', f'POINT ({-120} {36})']})
gdf = gpd.GeoDataFrame(df, geometry='geometry')

将GeoDataFrame导出为kml文件

gdf.to_file('scatter_plot. kml',driver='KML')请注意,在本例中,我们首先使用Plotly创建散点图,然后使用plotly_to_gis包将其转换为GeoDataFrame(在本例中,我们通过创建pandas DataFrame来手动创建GeoDataFrame,该pandas DataFrame包含指定点的经度和纬度的'geometry'列),最后使用geopandas将其导出为kml文件。

bjg7j2ky

bjg7j2ky2#

是的,你可以在Python中使用Plotly包来创建散点图,然后将它们保存为KML(Keyhole Markup Language)格式。
下面是一个示例代码片段,演示了如何使用以下步骤完成此操作:
1.使用Plotly的Matplotlib后端创建散点图:

import pandas as pd
from mplfinance import FinancialData, DataFrame
import plotly.offline # for off-screen rendering of plots
%matplotlib inline
df = df_sample()  ## create dataframe from sample data set
fig1=plot(df)   ## Plot scatter with MatPlotLib backend (default)

1.将图形导出为KML文件:3.使用offline模块渲染屏幕外的绘图对象,然后可以使用pyKernelkml_to_kmz()函数将其导出为各种格式,包括KMZ格式。以下是你可以做的:

import pyplotly as plt  # for rendering the figure to a file
from offline import Plot, savefig   ## use plotly's Matplotlib backend (default)
off = Plot(fig1.data())    ## render an object that can be saved in KML format
kml_to_kmz('./plotly-scatter', off )  # export the figure as a kml file using pyKernel module

相关问题