Skip to content

Commit 3613898

Browse files
committed
Improve save_draw_features function
1 parent 3e13bff commit 3613898

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

leafmap/leafmap.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3818,28 +3818,32 @@ def get_pc_collections(self) -> None:
38183818
setattr(self, "pc_collections", get_pc_collections())
38193819

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

3832-
out_file = check_file_path(out_file)
3830+
if self.user_rois is not None:
3831+
import geopandas as gpd
38333832

3834-
self.update_draw_features()
3835-
geojson = {
3836-
"type": "FeatureCollection",
3837-
"features": self.draw_features,
3838-
}
3833+
out_file = check_file_path(out_file)
38393834

3840-
gdf = gpd.GeoDataFrame.from_features(geojson)
3841-
gdf.crs = "epsg:4326"
3842-
gdf.to_crs(crs).to_file(out_file, **kwargs)
3835+
self.update_draw_features()
3836+
geojson = {
3837+
"type": "FeatureCollection",
3838+
"features": self.draw_features,
3839+
}
3840+
3841+
gdf = gpd.GeoDataFrame.from_features(geojson, crs="EPSG:4326")
3842+
if crs != "EPSG:4326":
3843+
gdf = gdf.to_crs(crs)
3844+
gdf.to_file(out_file, **kwargs)
3845+
else:
3846+
print("No draw features to save.")
38433847

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

0 commit comments

Comments
 (0)