Skip to content

re-factor WMS #696

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 1 commit into from
Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ API changes
If a popup requires rendering use the `kwarg` `parse_html=True`.
- `Circle` and `CircleMarker` are set to leaflet's defaults and accepted all
`Path` optional arguments (ocefpaf #683)
- `WmsTileLayer` is set to leaflet's defaults and accepted all `TileLayer.WMS`
optional arguments (ocefpaf #695)
- Changed default `max_bounds` to `False` to reflect leaflet's default value (rdd9999 #641)
- Modified `Fullscreen` plugin `kwargs` to be more "pythonic"
- All `.format` properties are now `.fmt` for consistency
Expand Down
40 changes: 13 additions & 27 deletions folium/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from branca.utilities import (_locations_tolist, _parse_size, image_to_url, iter_points, none_max, none_min) # noqa

from folium.map import FeatureGroup, Icon, Layer, Marker, Popup
from folium.utilities import _parse_path, _validate_coordinates, _validate_location
from folium.utilities import _parse_path, _parse_wms, _validate_coordinates, _validate_location

from jinja2 import Template

Expand All @@ -34,11 +34,11 @@ class WmsTileLayer(Layer):
The url of the WMS server.
name : string, default None
The name of the Layer, as it will appear in LayerControls
layers : str, default None
layers : str, default ''
The names of the layers to be displayed.
styles : str, default None
styles : str, default ''
Comma-separated list of WMS styles.
fmt : str, default None
fmt : str, default 'image/jpeg'
The format of the service output.
Ex: 'image/png'
transparent: bool, default True
Expand All @@ -61,34 +61,20 @@ class WmsTileLayer(Layer):
http://leafletjs.com/reference.html#tilelayer-wms

"""
def __init__(self, url, name=None, layers=None, styles=None, fmt=None,
transparent=True, version='1.1.1', attr=None, overlay=True,
control=True, **kwargs):
def __init__(self, url, name=None, attr='', overlay=True, control=True, **kwargs):
super(WmsTileLayer, self).__init__(overlay=overlay, control=control, name=name) # noqa
self.url = url
self.attribution = attr if attr is not None else ''
# Options.
self.layers = layers if layers else ''
self.styles = styles if styles else ''
self.fmt = fmt if fmt else 'image/jpeg'
self.transparent = transparent
self.version = version
self.kwargs = kwargs
options = _parse_wms(**kwargs)
options.update({'attribution': attr})

self.options = json.dumps(options, sort_keys=True, indent=2)

self._template = Template(u"""
{% macro script(this, kwargs) %}
var {{this.get_name()}} = L.tileLayer.wms(
'{{ this.url }}',
{
{% for key, value in this.kwargs.items() %}
{{key}}: '{{ value }}',
{% endfor %}
layers: '{{ this.layers }}',
styles: '{{ this.styles }}',
format: '{{ this.fmt }}',
transparent: {{ this.transparent.__str__().lower() }},
version: '{{ this.version }}',
{% if this.attribution %} attribution: '{{this.attribution}}'{% endif %}
}
{{ this.options }}
).addTo({{this._parent.get_name()}});

{% endmacro %}
Expand Down Expand Up @@ -825,10 +811,10 @@ class Circle(Marker):
See http://leafletjs.com/reference-1.2.0.html#path for more otions.

"""
def __init__(self, location, radius=10, popup=None, **kw):
def __init__(self, location, radius=10, popup=None, **kwargs):
super(Circle, self).__init__(_validate_location(location), popup=popup)
self._name = 'circle'
options = _parse_path(**kw)
options = _parse_path(**kwargs)

options.update({'radius': radius})
self.options = json.dumps(options, sort_keys=True, indent=2)
Expand Down
13 changes: 13 additions & 0 deletions folium/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ def _parse_path(**kw):
'fillRule': kw.pop('fill_rule', 'evenodd'),
'bubblingMouseEvents': kw.pop('bubbling_mouse_events', True),
}


def _parse_wms(**kw):
"""Parse tilelayer-wms http://leafletjs.com/reference-1.2.0.html#tilelayer-wms options."""
return {
'layers': kw.pop('layers', ''),
'styles': kw.pop('styles', ''),
'format': kw.pop('fmt', 'image/jpeg'),
'transparent': kw.pop('transparent', False),
'version': kw.pop('version', '1.1.1'),
'crs': kw.pop('crs', None),
'uppercase': kw.pop('uppercase', False),
}