8

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.

Community
  • 1
  • 1
edub
  • 659
  • 4
  • 8
  • 19

2 Answers2

19

fiona library wrapped by geopandas supports unofficialy a KML driver that you have to enable by hand.

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')

Note that it can also read KML files, but does not work really well on 'nested' kml files, see this gist for more details

MCMZL
  • 1,078
  • 9
  • 21
  • For me this only half worked. The geometry was saved well. But the saved KML files have discarded the first two data column values and the last two data column names, so that only part of the data is found (all but the first two columns) and even these data have shifted column names. E.g. if I saved A B C D 7 8 9 0 1 2 3 4 The result would be: A B 9 0 3 4 – ErnestScribbler Jun 19 '19 at 19:52
  • Using this method makes all the polygons red colored. Is there a way to set the color so that it stays between geopandas and kml? – skrhee Nov 04 '19 at 22:58
  • @skrhee How can you tell that your polygons inside the kml are "red-colored". What do you use for visualization? – MCMZL Nov 05 '19 at 16:05
  • @MCMZL I am using google earth for visualization. Thank you for your response! – skrhee Nov 06 '19 at 17:35
  • It looks like red is the default color with fiona driver. Maybe it can be changed but did not find it in the documentation. Quick fix is to use `sed` in bash to replace red with something else. – MCMZL Nov 08 '19 at 09:02
6

So I may have found a solution...

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

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

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)

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.

edub
  • 659
  • 4
  • 8
  • 19
  • This ends in error of `ERROR 4: Failed to read GeoJSON data FAILURE: Unable to open datasource....json' with the following drivers.` – nish Nov 11 '16 at 05:07
  • Has anybody been able to figure out the colors? – skrhee Nov 04 '19 at 22:58