Skip to content

Commit

Permalink
Improve save_draw_features function (#928)
Browse files Browse the repository at this point in the history
* Improve save_draw_features function

* Update NWI notebook

* Skip notebook 99
  • Loading branch information
giswqs authored Oct 21, 2024
1 parent 083f5ae commit b9c3fdf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
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

0 comments on commit b9c3fdf

Please sign in to comment.