How to convert KML file to Pandas Dataframe and CSV

Hi all,
I was provided with a KML file and I needed to turn it into a dataframe for further data manipulation with pandas, and then have it on plotly.express map.
I’m sharing with you the code, in hope it can be useful to someone.

# -----------------------------------------------
#                  LIBRARIES
# -----------------------------------------------

import geopandas as gpd 
import fiona 
import numpy as np
import pandas as pd 

# ------------------------------
#       DATA TREATMENT
# ------------------------------

# IMPORT KML DRIVER

gpd.io.file.fiona.drvsupport.supported_drivers['KML'] = 'rw'

# READ KML file to a geopandas dataframe 

geo_df = gpd.read_file('your_kml_file.kml',driver='KML')

# Create Pandas Dataframe from GeoPandas 

df= pd.DataFrame(geo_df)

# Extract latitude and longitude from the KML geometry column
df['lat'] = df.geometry.apply(lambda p: p.y)
df['lon'] = df.geometry.apply(lambda p: p.x)

# You now have a pandas dataframe with a lat and lon column that you can use to plot a mapbox plotly.express map and/or add more data to it. 

# Made with šŸ¤ by Jorge Gomes MAR 21 2022 

How can you improve this?

  • Make a loop to detect all KML files in a folder and turn them into an unique pandas dataframe
  • Save to .csv using pandas .to_csv function

Hope this can be of use to someone!

3 Likes

No, can’t be CSV. That is a type of non-relational data. You should at least consider storage formats like JSON and XML. And the best place to store data is in the database. Try GeoDatabase, MongoDB or something.

Hi @stu, while I appreciate your comment I don’t see any reason why I shouldn’t use a csv file to store lat and lon data, when that is just part of the data I need in my dataset. Not all of us have the knowledge to use ā€œGeoDatabase, MongoDB or somethingā€ and the objective here is to simplify and provide a working solution, not to complicate things even more.

Aha, I see. You are not here to seek advice. I would give a stable and reliable result. The latitude and longitude data is just a description of some points in the KML file. BTW, the geopandas library also has GeoSeries.y, GeoSeries.x, and GeoDataFrame.to_file APIs. And the common usage is gdf.to_file('dataframe.shp') or gdf.to_file('dataframe.geojson', driver='GeoJSON') etc.

Also, I suggest you change the title to How to extract coordinates of points from a KML file and save into CSV.

Hi @jgomes_eu ,
Thank you for sharing your example with us. I appreciate that you took the time to share with the community the knowledge that you gained on KML. That only makes the community a better place where people can learn Dash with greater ease.

I hope to see more of theses :slight_smile:

1 Like

Hi @jgomes_eu,

Thanks for sharing this solution to working with KML files. This is by far the easiest solution I’ve seen. Thanks for your time!

1 Like

Hi Jorge,
I am getting an Error Message in Pycharm:

Traceback (most recent call last):
for line:
gpd.io.file.fiona.drvsupport.supported_drivers[ā€˜KML’] = ā€˜rw’
AttributeError: ā€˜NoneType’ object has no attribute ā€˜drvsupport’

Is there a requirement for specific versions of geopandas or fiona?
Thanks,
Peter