Skip to content

Commit

Permalink
Improve save_draw_features function
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Oct 21, 2024
1 parent 3e13bff commit 3613898
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions leafmap/leafmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3818,28 +3818,32 @@ def get_pc_collections(self) -> None:
setattr(self, "pc_collections", get_pc_collections())

def save_draw_features(
self, out_file: str, indent: int = 4, crs: Optional[str] = "epsg:4326", **kwargs
self, out_file: str, crs: Optional[str] = "EPSG:4326", **kwargs
) -> None:
"""Save the draw features to a file.
Args:
out_file (str): The output file path.
indent (int, optional): The indentation level when saving data as a GeoJSON. Defaults to 4.
crs (str, optional): The CRS of the output GeoJSON. Defaults to "epsg:4326".
crs (str, optional): The CRS of the output GeoJSON. Defaults to "EPSG:4326".
"""
import geopandas as gpd

out_file = check_file_path(out_file)
if self.user_rois is not None:
import geopandas as gpd

self.update_draw_features()
geojson = {
"type": "FeatureCollection",
"features": self.draw_features,
}
out_file = check_file_path(out_file)

gdf = gpd.GeoDataFrame.from_features(geojson)
gdf.crs = "epsg:4326"
gdf.to_crs(crs).to_file(out_file, **kwargs)
self.update_draw_features()
geojson = {
"type": "FeatureCollection",
"features": self.draw_features,
}

gdf = gpd.GeoDataFrame.from_features(geojson, crs="EPSG:4326")
if crs != "EPSG:4326":
gdf = gdf.to_crs(crs)
gdf.to_file(out_file, **kwargs)
else:
print("No draw features to save.")

def update_draw_features(self) -> None:
"""Update the draw features by removing features that have been edited and no longer exist."""
Expand Down

0 comments on commit 3613898

Please sign in to comment.