From 029fcf04b7216d36c6d0883af8babd740fa70b6d Mon Sep 17 00:00:00 2001 From: 29riyasaxena <29riyasaxena@gmail.com> Date: Tue, 9 Jul 2024 23:12:03 +0530 Subject: [PATCH] Allow user to save the drawn vectors --- python/grass/jupyter/interactivemap.py | 12 +++++++++--- python/grass/jupyter/utils.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/python/grass/jupyter/interactivemap.py b/python/grass/jupyter/interactivemap.py index 1b76fb7f5a8..e18eacbfce6 100644 --- a/python/grass/jupyter/interactivemap.py +++ b/python/grass/jupyter/interactivemap.py @@ -16,6 +16,7 @@ import base64 import json from .reprojection_renderer import ReprojectionRenderer +from .utils import save_vector def get_backend(interactive_map): @@ -407,11 +408,16 @@ def save_geometries(b): "type": "FeatureCollection", "features": drawn_geometries, } + geojson_filename = f"/tmp/{name}.geojson" + with open(geojson_filename, "w") as f: + json.dump(geo_json, f) + try: + save_vector(geojson_filename, name) + print(f"Imported geometry with name '{name}' into GRASS GIS.") + except Exception as e: + print(f"Failed to import geometries into GRASS GIS: {e}") geo_json_layer = self._ipyleaflet.GeoJSON(data=geo_json, name=name) self.map.add_layer(geo_json_layer) - print( - f"Saved {len(drawn_geometries)} geometries with name '{name}'" - ) save_button.on_click(save_geometries) diff --git a/python/grass/jupyter/utils.py b/python/grass/jupyter/utils.py index f06adfa36d1..5c43eda9eda 100644 --- a/python/grass/jupyter/utils.py +++ b/python/grass/jupyter/utils.py @@ -202,6 +202,16 @@ def get_rendering_size(region, width, height, default_width=600, default_height= return (default_width, round(default_width * region_height / region_width)) +def save_vector(geojson_filename, name): + """ + Saves the user drawn vector. + + param geojson_filename: name of the geojson file to be saved + param name: name with which vector should be saved + """ + gs.run_command("v.import", input=geojson_filename, output=name) + + def save_gif( input_files, output_filename,