将GeoDataFrame多边形转换为kml文件

将GeoDataFrame多边形转换为kml文件

本文介绍了将GeoDataFrame多边形转换为kml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Geopandas GeoDataFrame,它具有用于绘制气象数据的各种多边形和颜色(我问了另一个问题):

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,...

我想将其转换为kml/kmz文件,但是我以前从未使用过该文件类型,因此我不确定如何继续.我尝试使用脚本,但是它需要一些脚本我没有的高度场.在python中是否有一个好/简单的方法来做到这一点?如果可能的话,我想避免使用在线转换器工具.

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.

推荐答案

所以我可能找到了解决方法...

So I may have found a solution...

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

I installed the Geospatial Data Abstraction Library and have been using the ogr2ogr function.

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

As I explained in my question, I have a geopandas GeoDataFrame with polygons and associated colors, which I write to a json file:

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

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

In the Terminal / command line, I type:

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

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

You could technically call this command from within a python script using the library 'subprocess':

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

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

This generates a kml file with my lat/lon-based polygons. However, it automatically sets all the line colors to red with no fill color (even though my json file has colors in it). I haven't found a good solution to this, so I've been editing the KML file by hand to get the styling I want.

这篇关于将GeoDataFrame多边形转换为kml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:46