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