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

Added default options to tile_layer #236

Merged
merged 2 commits into from
Nov 9, 2015
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
83 changes: 51 additions & 32 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ def __init__(self, location=None, width='100%', height='100%',

Examples
--------
>>>map = folium.Map(location=[45.523, -122.675], width=750, height=500)
>>>map = folium.Map(location=[45.523, -122.675],
tiles='Mapbox Control Room')
>>>map = folium.Map(location=(45.523, -122.675), max_zoom=20,
tiles='Cloudmade', API_key='YourKey')
>>>map = folium.Map(location=[45.523, -122.675], zoom_start=2,
tiles=('http://{s}.tiles.mapbox.com/v3/'
>>> map = folium.Map(location=[45.523, -122.675], width=750,
... height=500)
>>> map = folium.Map(location=[45.523, -122.675],
tiles='Mapbox Control Room')
>>> map = folium.Map(location=(45.523, -122.675), max_zoom=20,
tiles='Cloudmade', API_key='YourKey')
>>> map = folium.Map(location=[45.523, -122.675], zoom_start=2,
tiles=('http://{s}.tiles.mapbox.com/v3/'
'mapbox.control-room/{z}/{x}/{y}.png'),
attr='Mapbox attribution')

Expand Down Expand Up @@ -209,24 +210,46 @@ def __init__(self, location=None, width='100%', height='100%',
self.template_vars.setdefault('image_layers', [])

@iter_obj('simple')
def add_tile_layer(self, tile_name=None, tile_url=None, active=False):
"""Adds a simple tile layer.
def add_tile_layer(self, tile_name=None, tile_url=None, **kw):
"""
Adds a simple tile layer.

Parameters
----------
tile_name: string
name of the tile layer
tile_url: string
url location of the tile layer
active: boolean
should the layer be active when added

For the available options see:
http://leafletjs.com/reference.html#tilelayer

"""
# Same defaults.
tms = kw.pop('tms', False)
minZoom = kw.pop('minZoom', 0)
opacity = kw.pop('opacity', 1)
maxZoom = kw.pop('maxZoom', 18)
noWrap = kw.pop('noWrap', False)
zoomOffset = kw.pop('zoomOffset', 0)
zoomReverse = kw.pop('zoomReverse', False)
continuousWorld = kw.pop('continuousWorld', False)

Copy link
Contributor

Choose a reason for hiding this comment

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

Provided that you handle when tms,noWrap,zoomReverse,continuousWorld are bool, I would set defaults to False instead of 'false'. What do you think ?
Anyway, this is a matter of taste and obviously not blocking for merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. I was not comfortable with that either.

Done and rebased!

if tile_name not in self.added_layers:
tile_name = tile_name.replace(" ", "_")
tile_temp = self.env.get_template('tile_layer.js')

tile = tile_temp.render({'tile_name': tile_name,
'tile_url': tile_url})
tile = tile_temp.render({
'tile_name': tile_name,
'tile_url': tile_url,
'minZoom': minZoom,
'maxZoom': maxZoom,
'tms': str(tms).lower(),
'continuousWorld': str(continuousWorld).lower(),
'noWrap': str(noWrap).lower(),
'zoomOffset': zoomOffset,
'zoomReverse': str(zoomReverse).lower(),
'opacity': opacity,
})

self.template_vars.setdefault('tile_layers', []).append((tile))

Expand Down Expand Up @@ -305,8 +328,8 @@ def simple_marker(self, location=None, popup=None,

Example
-------
>>>map.simple_marker(location=[45.5, -122.3], popup='Portland, OR')
>>>map.simple_marker(location=[45.5, -122.3], popup=(vis, 'vis.json'))
>>> map.simple_marker(location=[45.5, -122.3], popup='Portland, OR')
>>> map.simple_marker(location=[45.5, -122.3], popup=(vis, 'vis.json'))

"""
count = self.mark_cnt['simple']
Expand Down Expand Up @@ -364,8 +387,8 @@ def line(self, locations,

Example
-------
>>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)])
>>>map.line(locations=[(45.5, -122.3), (42.3, -71.0)],
>>> map.line(locations=[(45.5, -122.3), (42.3, -71.0)])
>>> map.line(locations=[(45.5, -122.3), (42.3, -71.0)],
line_color='red', line_opacity=1.0)

"""
Expand Down Expand Up @@ -409,7 +432,6 @@ def multiline(self, locations, line_color=None, line_opacity=None,

Example
-------
# FIXME: Add another example.
>>> m.multiline(locations=[[(45.5236, -122.675), (45.5236, -122.675)],
[(45.5237, -122.675), (45.5237, -122.675)],
[(45.5238, -122.675), (45.5238, -122.675)]])
Expand Down Expand Up @@ -467,9 +489,9 @@ def circle_marker(self, location=None, radius=500, popup=None,

Example
-------
>>>map.circle_marker(location=[45.5, -122.3],
>>> map.circle_marker(location=[45.5, -122.3],
radius=1000, popup='Portland, OR')
>>>map.circle_marker(location=[45.5, -122.3],
>>> map.circle_marker(location=[45.5, -122.3],
radius=1000, popup=(bar_chart, 'bar_data.json'))

"""
Expand Down Expand Up @@ -584,7 +606,7 @@ def click_for_marker(self, popup=None):

Example
-------
>>>map.click_for_marker(popup='Your Custom Text')
>>> map.click_for_marker(popup='Your Custom Text')

"""
latlng = '"Latitude: " + lat + "<br>Longitude: " + lng '
Expand Down Expand Up @@ -802,9 +824,9 @@ def geo_json(self, geo_path=None, geo_str=None, data_out='data.json',
keyword argument will enable conversion to GeoJSON.
reset: boolean, default False
Remove all current geoJSON layers, start with new layer
freescale: if True use free format for the scale, where min and max values
are taken from the data. It also allow to plot allow to plot values < 0
and float legend labels.
freescale: if True use free format for the scale, where min and max
values are taken from the data. It also allow to plot
values < 0 and float legend labels.

Output
------
Expand Down Expand Up @@ -893,7 +915,7 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,

# D3 Color scale.
series = data[columns[1]]
if freescale == False:
if not freescale:
if threshold_scale and len(threshold_scale) > 6:
raise ValueError
domain = threshold_scale or utilities.split_six(series=series)
Expand All @@ -918,18 +940,15 @@ def json_style(style_cnt, line_color, line_weight, line_opacity,

if not freescale:
legend = leg_templ.render({'lin_min': 0,
'lin_max': int(domain[-1]*1.1),
'tick_labels': tick_labels,
'caption': name})


'lin_max': int(domain[-1]*1.1),
'tick_labels': tick_labels,
'caption': name})
else:
legend = leg_templ.render({'lin_min': domain[0],
'lin_max': domain[-1],
'tick_labels': tick_labels,
'caption': name})


self.template_vars.setdefault('map_legends', []).append(legend)

# Style with color brewer colors.
Expand Down
6 changes: 6 additions & 0 deletions folium/templates/leaflet-dvf.markers.min.js

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion folium/templates/tile_layer.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
var {{ tile_name }} = L.tileLayer('{{ tile_url }}');
var {{ tile_name }} = L.tileLayer('{{ tile_url }}',{
'minZoom': {{minZoom}},
'maxZoom': {{maxZoom}},
'tms': {{tms}},
'continuousWorld': {{continuousWorld}},
'noWrap': {{noWrap}},
'zoomOffset': {{zoomOffset}},
'zoomReverse': {{zoomReverse}},
'opacity': {{opacity}}
});
35 changes: 35 additions & 0 deletions tests/folium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,41 @@ def test_wms_layer(self):
'wms_transparent': 'true'})
assert map.template_vars['wms_layers'][0] == wms

def test_add_tile_layer(self):
"""Test add_layer URLs."""

map = folium.Map(location=[44, -73], zoom_start=3)
tile_name = "Temperature"
tile_url = 'http://gis.srh.noaa.gov/arcgis/services/NDFDTemps/'
tile_url += 'MapServer/WMSServer'
minZoom = 1
maxZoom = 5
tms = True
continuousWorld = False
noWrap = True
zoomOffset = 1
zoomReverse = True
opacity = 2
map.add_tile_layer(tile_name=tile_name, tile_url=tile_url, active=True,
minZoom=minZoom, maxZoom=maxZoom, tms=tms,
continuousWorld=continuousWorld, noWrap=noWrap,
zoomOffset=zoomOffset, zoomReverse=zoomReverse,
opacity=opacity, attribution='Leaflet')
tile_temp = self.env.get_template('tile_layer.js')
tile = tile_temp.render({
'tile_name': tile_name,
'tile_url': tile_url,
'minZoom': minZoom,
'maxZoom': maxZoom,
'tms': str(tms).lower(),
'continuousWorld': str(continuousWorld).lower(),
'noWrap': str(noWrap).lower(),
'zoomOffset': zoomOffset,
'zoomReverse': str(zoomReverse).lower(),
'opacity': opacity,
})
assert map.template_vars['tile_layers'][0] == tile

def test_simple_marker(self):
"""Test simple marker addition."""

Expand Down