Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add df_to_gdf function #586

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11605,3 +11605,28 @@ def gdb_layer_names(gdb_path: str) -> List[str]:
# Close the GDB dataset
gdb_dataset = None
return layer_names


def df_to_gdf(df, geometry_column="geometry", crs="EPSG:4326"):
"""
Converts a pandas DataFrame to a GeoPandas GeoDataFrame.

Args:
df (pandas.DataFrame): The pandas DataFrame to convert.
geometry_column (str): The name of the geometry column in the DataFrame.
crs (str): The coordinate reference system (CRS) of the GeoDataFrame. Default is "EPSG:4326".

Returns:
geopandas.GeoDataFrame: The converted GeoPandas GeoDataFrame.
"""
import geopandas as gpd
from shapely import wkt

# Convert the geometry column to Shapely geometry objects
df[geometry_column] = df[geometry_column].apply(lambda x: wkt.loads(x))

# Convert the pandas DataFrame to a GeoPandas GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry=geometry_column)
gdf.crs = crs

return gdf