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

Improve save_draw_features function #928

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/notebooks/99_wetlands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
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
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ plugins:
"notebooks/87_actinia.ipynb",
"notebooks/88_nasa_earth_data.ipynb",
"notebooks/94_mapbox.ipynb",
"notebooks/99_wetlands.ipynb",
]

markdown_extensions:
Expand Down