4

I have two GeoPandas GeoDataFrames, and I want to export them to a KMZ file with a nested folder structure. Below is a stand-alone code to create the GeoDataFrames, and a screenshot of how I would like the KMZ structure to look.

I want to do this without ArcGIS or QGIS. Using Python 3.7.

###############################################
### load libraries
###############################################
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from shapely.geometry import LineString

###############################################
### Build Stand-Alone Geodataframes
### 'p' is a points-type gdf
### 'pl' is a polyline-type gdf
###############################################

pname = ['Project1'] * 5
id1 = [1, 2, 3, 4, 5]
lat = [36.42, 36.4, 36.32, 36.28, 36.08]
long = [-118.11, -118.12, -118.07, -117.95, -117.95]
cat = ['X', 'X', 'X', 'Y', 'Y']
id2 = ['A', 'A', 'B', 'B', 'B']
df = pd.DataFrame(list(zip(pname, id1, lat, long, cat, id2)), 
                  columns =['pname', 'id1', 'lat', 'long', 'cat', 'id2']) 
df.reset_index(drop=True, inplace=True)


p = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['long'], df['lat']))
p = p.set_crs(epsg=4326)
display(p.style)


pl = p.groupby(['pname', 'id2'])['geometry'].apply(lambda x: LineString(x.tolist()))
pl = gpd.GeoDataFrame(pl, geometry='geometry').reset_index()
pl = pl.set_crs(epsg=4326)
display(pl.style)


fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)
p.plot(ax=ax1)
pl.plot(ax=ax2)
ax1.set_aspect('equal')

enter image description here

The desired KMZ structure

enter image description here

1 Answer 1

2

Geopandas is using fiona to handle spatial information, and Fiona doesn't support KMZ I/O. Reference: https://gis.stackexchange.com/a/191380/

>>> import fiona
>>> fiona.supported_drivers
{'ESRI Shapefile': 'raw', 'ARCGEN': 'r', 'PCIDSK': 'r', 'SUA': 'r', 
'DGN': 'raw', 'SEGY': 'r', 'MapInfo File': 'raw', 'GeoJSON': 'rw', 'PDS': 'r', 
'FileGDB': 'raw', 'GPX': 'raw', 'DXF': 'raw', 'GMT': 'raw', 'Idrisi': 'r', 
'GPKG': 'rw', 'OpenFileGDB': 'r', 'BNA': 'raw', 'AeronavFAA': 'r', 
'GPSTrackMaker': 'raw'}

Therefore, you have to export as KML and pack them with zipfile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.