-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add the GeoMan plugin #2124
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
Merged
Merged
Add the GeoMan plugin #2124
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from branca.element import MacroElement | ||
|
||
from folium.elements import JSCSSMixin | ||
from folium.template import Template | ||
from folium.utilities import remove_empty | ||
|
||
|
||
class GeoMan(JSCSSMixin, MacroElement): | ||
""" | ||
An Open Source Leaflet Plugin for editing polygons | ||
|
||
Examples | ||
-------- | ||
>>> m = folium.Map() | ||
>>> Geoman().add_to(m) | ||
|
||
For more info please check | ||
https://github.com/geoman-io/leaflet-geoman/ | ||
|
||
""" | ||
|
||
_template = Template( | ||
""" | ||
{% macro script(this, kwargs) %} | ||
{%- if this.feature_group %} | ||
var drawnItems_{{ this.get_name() }} = | ||
{{ this.feature_group.get_name() }}; | ||
{%- else %} | ||
// FeatureGroup is to store editable layers. | ||
var drawnItems_{{ this.get_name() }} = | ||
new L.featureGroup().addTo( | ||
{{ this._parent.get_name() }} | ||
); | ||
{%- endif %} | ||
/* The global varianble below is needed to prevent streamlit-folium | ||
from barfing :-( | ||
*/ | ||
var drawnItems = drawnItems_{{ this.get_name() }}; | ||
|
||
{{this._parent.get_name()}}.pm.addControls( | ||
{{this.options|tojavascript}} | ||
) | ||
drawnItems_{{ this.get_name() }}.eachLayer(function(layer){ | ||
L.PM.reInitLayer(layer); | ||
{%- for event, handler in this.on.items() %} | ||
layer.on( | ||
"{{event}}", | ||
{{handler}} | ||
); | ||
{%- endfor %} | ||
}); | ||
|
||
{{ this._parent.get_name() }}.on("pm:create", function(e) { | ||
var layer = e.layer, | ||
type = e.layerType; | ||
|
||
{%- for event, handler in this.on.items() %} | ||
layer.on( | ||
"{{event}}", | ||
{{handler}} | ||
); | ||
{%- endfor %} | ||
drawnItems_{{ this.get_name() }}.addLayer(layer); | ||
}); | ||
{{ this._parent.get_name() }}.on("pm:remove", function(e) { | ||
var layer = e.layer, | ||
type = e.layerType; | ||
drawnItems_{{ this.get_name() }}.removeLayer(layer); | ||
}); | ||
|
||
{% endmacro %} | ||
""" | ||
) | ||
|
||
default_js = [ | ||
( | ||
"leaflet_geoman_js", | ||
"https://unpkg.com/@geoman-io/leaflet-geoman-free@latest/dist/leaflet-geoman.js", | ||
) | ||
] | ||
default_css = [ | ||
( | ||
"leaflet_geoman_css", | ||
"https://unpkg.com/@geoman-io/leaflet-geoman-free@latest/dist/leaflet-geoman.css", | ||
) | ||
] | ||
|
||
def __init__( | ||
self, | ||
position="topleft", | ||
feature_group=None, | ||
on=None, | ||
**kwargs, | ||
): | ||
super().__init__() | ||
self._name = "GeoMan" | ||
self.feature_group = feature_group | ||
self.on = on or {} | ||
self.options = remove_empty( | ||
position=position, layer_group=feature_group, **kwargs | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
""" | ||
Test GeoMan | ||
---------------- | ||
|
||
""" | ||
|
||
import folium | ||
from folium import plugins | ||
from folium.template import Template | ||
from folium.utilities import normalize | ||
|
||
|
||
def test_geoman(): | ||
m = folium.Map([47, 3], zoom_start=1) | ||
fs = plugins.GeoMan().add_to(m) | ||
|
||
out = normalize(m._parent.render()) | ||
|
||
# verify that the GeoMan plugin was added to | ||
# the map | ||
tmpl = Template( | ||
""" | ||
{{this._parent.get_name()}}.pm.addControls( | ||
{{this.options|tojavascript}} | ||
) | ||
""" | ||
) | ||
|
||
assert normalize(tmpl.render(this=fs)) in out |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use a versioned URL here? I'm also on the fence when doing this for plugins.
BTW, I would recommend all new plugins to be their own package. However, this one does make sense to be here IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference is for unversioned most of the time. It causes a lot of small annoyances every time something breaks. It avoids being unable to bring a plugin up to date because too much time has passed and the old versions are no longer supported.
What do you mean "their own package"? You mean like a separate github repository?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. We took that approach to