eodaGmbH / py-maplibregl

Python bindings for MapLibre GL JS
https://eodagmbh.github.io/py-maplibregl/
MIT License
27 stars 2 forks source link

Edit and delete draw features programmatically #72

Open giswqs opened 1 week ago

giswqs commented 1 week ago

The mapbox-gl-draw plugin has the following functions for adding, updating, and deleting draw features programmatically. Is it possbile to support these well?

https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md

delete(ids: string | Array<string>): draw Removes features with the specified ids. Returns the draw instance for chaining.

deleteAll(): draw Removes all features. Returns the draw instance for chaining.

add(geojson: Object) => Array<string> This method takes either a GeoJSON Feature, FeatureCollection, or Geometry and adds it to Draw. It returns an array of ids for interacting with the added features. If a feature does not have its own id, one is automatically generated. The supported GeoJSON feature types are supported: Point, LineString, Polygon, MultiPoint, MultiLineString, and MultiPolygon. If you add() a feature with an id that is already in use, the existing feature will be updated and no new feature will be added.

set(featureCollection: FeatureCollection): Array<string> Sets Draw's features to those in the specified FeatureCollection. Performs whatever delete, create, and update actions are necessary to make Draw's features match the specified FeatureCollection. Effectively, this is the same as Draw.deleteAll() followed by Draw.add(featureCollection) except that it does not affect performance as much.

crazycapivara commented 1 week ago

Yep, this should be no problem. I just did not add it, because I had no use case for this. At the moment you can just add features initially when adding the controls.

giswqs commented 1 week ago

One use case is to allow users to open vector data and edit them interactively. For now, we can only load the features when adding the draw control. If users want to edit a different file, they need to create a new map.

The trash tool can only delete on features at a time. Users may want to draw a bunch of features and then clear them all at once.

https://github.com/eodaGmbH/py-maplibregl/assets/5016453/100db58b-733a-49ab-9df6-fbcdf529cf9e

Martenz commented 1 week ago

Maybe a dumb question sorry.. but I can not import maplibre.plugins ..

from maplibre.plugins import MapboxDrawControls, MapboxDrawOptions

this gives me >> ModuleNotFoundError: No module named 'maplibre.plugins'

I installed in a venv like doc:

pip install maplibre pip install "maplibre[all]"

I'm on python v3.12.2

Should I install other libraries ?

Thanks

Martenz commented 1 week ago

I solved installing it form git dev

pip install git+https://github.com/eodaGmbH/py-maplibregl@dev

crazycapivara commented 1 week ago

@Martenz Maybe you had an older version installed. In this case you need to run

pip install -U maplibre

to update to the latest one (it is supported since v0.2.4)

Martenz commented 1 week ago

Thanks that solved also the new LayerSwitchControl! Super Thanks great repo!