3

I have a geopandas GeoDataFrame with various polygons and colors that I'm using to plot meteorological data (another question I asked here):

        color   geometry
0   #fbfdd1 (POLYGON ((-97.12191717810094 32.569, -97.1194...
1   #f3fabf (POLYGON ((-97.12442748846019 32.569, -97.1219...
2   #ebf7b1 (POLYGON ((-97.12944810917861 32.569, -97.1269...
3   #daf0b2 (POLYGON ((-97.18969555780023 32.569, -97.1879...
4   #cbeab3 (POLYGON ((-97.18969555780023 32.5710632999095...
5   #afdfb6 (POLYGON ((-97.18467493708175 32.569, -97.1821...
6   #92d4b9 (POLYGON ((-97.17463369564484 32.5730575804109...
7   #74c9bc (POLYGON ((-97.17714400600408 32.5764063816167...
8   #5bbfc0 (POLYGON ((-97.17714400600408 32.5790959050363...
9   #40b5c3 (POLYGON ((-97.17463369564484 32.5814268890055...
10  #31a6c2 (POLYGON ((-97.17714400600408 32.5852716913413...
11  #2397c0 (POLYGON ((-97.17714400600408 32.5878055733984...
12  #1e83b9 (POLYGON ((-97.17714400600408 32.5895482376014...
13  #206eaf (POLYGON ((-97.17714400600408 32.5911487379959...
14  #2259a5 (POLYGON ((-97.17714400600408 32.5927834911588...
15  #23479d POLYGON ((-97.17463369564484 32.59421434681196...
16  #243594 POLYGON ((-97.17463369564484 32.5962866795434,...
17  #1a2b7d POLYGON ((-97.1721233852856 32.59996829071199,...

I'd like to convert this to a kml / kmz file, but I have never worked with that file type before, so I'm not sure how to proceed. I've tried using this script, but it requires some height field that I do not have. Is there a good / easy way to do this within python? I'd like to avoid using online converter tools, if possible.

4

2 回答 2

8

fiona包装的库geopandas支持非官方的 KML 驱动程序,您必须手动启用。

import geopandas as gpd
import fiona

fiona.supported_drivers['KML'] = 'rw'

gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.to_file('test.kml', driver='KML')

请注意,它也可以读取 KML 文件,但在“嵌套”kml 文件上效果不佳,请参阅此要点了解更多详细信息

于 2018-10-17T09:28:53.000 回答
3

所以我可能已经找到了解决方案......

我安装了地理空间数据抽象库并一直在使用ogr2ogr函数。

正如我在问题中解释的那样,我有一个带有多边形和相关颜色的 geopandas GeoDataFrame,我将其写入 json 文件:

with open('/Users/Me/Documents/mydata.json', 'w') as f:
    f.write(gdf.to_json())

在终端/命令行中,我输入:

ogr2ogr -f KML /Users/Me/Documents/mydata.kml /Users/Me/Documents/mydata.json

从技术上讲,您可以使用库 'subprocess' 从 python 脚本中调用此命令:

import subprocess
subprocess.call("ogr2ogr -f KML /Users/Me/Documents/mydata.kml /Users/Me/Documents/mydata.json",shell=True)

这会生成一个带有我基于纬度/经度的多边形的 kml 文件。但是,它会自动将所有线条颜色设置为没有填充颜色的红色(即使我的 json 文件中有颜色)。我还没有找到一个好的解决方案,所以我一直在手动编辑 KML 文件以获得我想要的样式。

于 2016-04-11T19:58:28.453 回答