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

grass.jupyter: Allow users to draw simple geometries and save it as vector #4003

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

29riyasaxena
Copy link
Contributor

Hi there,

Here’s my basic idea for implementing a feature that allows users to draw simple geometries to add as a GRASS native vector map. I’d love to hear your suggestions on how to improve it.

@github-actions github-actions bot added Python Related code is in Python libraries notebook labels Jul 8, 2024
@petrasovaa
Copy link
Contributor

Right now it's not creating a GRASS vector, it's just adding geojson layer to the map. The vector needs to be imported, I think v.import should do that as we discussed before.

@29riyasaxena
Copy link
Contributor Author

should do

Hi Anna,
Yes, I remember. I hadn't thought of saving them as vectors. Sorry about that. I've pushed the changes now.

@petrasovaa
Copy link
Contributor

This is not working, in the jupyterlab log I get:

Failed to import geometries into GRASS GIS: a bytes-like object is required, not 'str'

Please test more. Run it in the notebook and check the output is correctly written as a vector map.

@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is rather trivial function, perhaps this could include the temporary file creation (and removal).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, should I add a button to delete the the drawn vector?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am talking about moving some of the code handling temporary file creation to this function. The temporary file needs to be removed once it's not needed.

@petrasovaa petrasovaa changed the title Allow Users to draw simple geometries Jul 11, 2024
@29riyasaxena
Copy link
Contributor Author

Hi Anna, I can successfully add vectors and save them, but I am stuck on the part when deleting them. I tried clearing all the geometries and removing layers when clicking the delete button, but things aren't working. Could you please share your opinions on this?

param geojson_filename: name of the geojson file to be saved
param name: name with which vector should be saved
"""
with tempfile.NamedTemporaryFile(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the temp file is not being removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why and when to delete the temp file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the why, its because it is not deleted automatically with delete=False. Take a look at the section here: https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile, it goes through the behavior of NamedTemporaryFile vs other tempfile classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries notebook Python Related code is in Python
3 participants