Skip to content

Commit

Permalink
Add function for convert building csv to vector
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Sep 16, 2023
1 parent 17f66b7 commit 117a7de
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10703,3 +10703,35 @@ def download_google_buildings(

else:
print(f"No buildings found for {location}.")


def google_buildings_csv_to_vector(filename: str, output: Optional[str] = None, **kwargs) -> None:
"""
Convert a CSV file containing Google Buildings data to a GeoJSON vector file.
Args:
filename (str): The path to the input CSV file.
output (str, optional): The path to the output GeoJSON file. If not provided, the output file will have the same
name as the input file with the extension changed to '.geojson'.
**kwargs: Additional keyword arguments that are passed to the `to_file` method of the GeoDataFrame.
Returns:
None
"""
import pandas as pd
import geopandas as gpd
from shapely import wkt

df = pd.read_csv(filename)

# Create a geometry column from the "geometry" column in the DataFrame
df["geometry"] = df["geometry"].apply(wkt.loads)

# Convert the pandas DataFrame to a GeoDataFrame
gdf = gpd.GeoDataFrame(df, geometry="geometry")
gdf.crs = "EPSG:4326"

if output is None:
output = os.path.splitext(filename)[0] + ".geojson"

gdf.to_file(output, **kwargs)

0 comments on commit 117a7de

Please sign in to comment.