def create_map(self, path='map.html', plugin_data_out=True, template=None): '''Write Map output to HTML and data output to JSON if available Parameters: ----------- path: string, default 'map.html' Path for HTML output for map plugin_data_out: boolean, default True If using plugins such as awesome markers, write all plugin data such as JS/CSS/images to path template: string, default None Custom template to render ''' self.map_path = path self._build_map(template) with codecs.open(path, 'w', 'utf8') as f: f.write(self.HTML) if self.json_data: for path, data in iteritems(self.json_data): with open(path, 'w') as g: json.dump(data, g) if self.plugins and plugin_data_out: for name, plugin in iteritems(self.plugins): with open(name, 'w') as f: if isinstance(plugin, binary_type): plugin = text_type(plugin, 'utf8') f.write(plugin)
def create_map(self, path='map.html', plugin_data_out=True, template=None): """Write Map output to HTML and data output to JSON if available. Parameters: ----------- path: string, default 'map.html' Path for HTML output for map plugin_data_out: boolean, default True If using plugins such as awesome markers, write all plugin data such as JS/CSS/images to path template: string, default None Custom template to render """ self.map_path = path self._build_map(template) with codecs.open(path, 'w', 'utf8') as f: f.write(self.HTML) if self.json_data: for path, data in iteritems(self.json_data): with open(path, 'w') as g: json.dump(data, g) if self.plugins and plugin_data_out: for name, plugin in iteritems(self.plugins): with open(name, 'w') as f: if isinstance(plugin, binary_type): plugin = text_type(plugin, 'utf8') f.write(plugin)
def __init__(self, location=None, width=960, height=500, tiles='OpenStreetMap', API_key=None, max_zoom=18, min_zoom=1, zoom_start=10, attr=None, min_lat=-90, max_lat=90, min_lon=-180, max_lon=180): '''Create a Map with Folium and Leaflet.js Generate a base map of given width and height with either default tilesets or a custom tileset URL. The following tilesets are built-in to Folium. Pass any of the following to the "tiles" keyword: -"OpenStreetMap" -"MapQuest Open" -"MapQuest Open Aerial" -"Mapbox Bright" (Limited levels of zoom for free tiles) -"Mapbox Control Room" (Limited levels of zoom for free tiles) -"Stamen Terrain" -"Stamen Toner" -"Cloudmade" (Must pass API key) -"Mapbox" (Must pass API key) You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: http://{s}.yourtiles.com/{z}/{x}/{y}.png Parameters ---------- location: tuple or list, default None Latitude and Longitude of Map (Northing, Easting). width: int, default 960 Width of the map. height: int, default 500 Height of the map. tiles: str, default 'OpenStreetMap' Map tileset to use. Can use defaults or pass a custom URL. API_key: str, default None API key for Cloudmade or Mapbox tiles. max_zoom: int, default 18 Maximum zoom depth for the map. zoom_start: int, default 10 Initial zoom level for the map. attr: string, default None Map tile attribution; only required if passing custom tile URL. Returns ------- Folium Map Object 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/' 'mapbox.control-room/{z}/{x}/{y}.png'), attr='Mapbox attribution') ''' #Inits self.map_path = None self.render_iframe = False self.map_type = 'base' self.map_id = '_'.join(['folium', uuid4().hex]) #Mark counter, JSON, Plugins self.mark_cnt = {} self.json_data = {} self.plugins = {} #Location if not location: raise ValueError('You must pass a Lat/Lon location to initialize' ' your map') self.location = location #Map Size Parameters self.width = width self.height = height self.map_size = {'width': width, 'height': height} self._size = ('style="width: {0}px; height: {1}px"' .format(width, height)) #Templates self.env = ENV self.template_vars = dict(lat=location[0], lon=location[1], size=self._size, max_zoom=max_zoom, zoom_level=zoom_start, map_id=self.map_id, min_zoom=min_zoom, min_lat=min_lat, max_lat=max_lat, min_lon=min_lon, max_lon=max_lon) #Tiles self.tiles = ''.join(tiles.lower().strip().split()) if self.tiles in ('cloudmade', 'mapbox') and not API_key: raise ValueError('You must pass an API key if using Cloudmade' ' or non-default Mapbox tiles.') self.default_tiles = ['openstreetmap', 'mapboxcontrolroom', 'mapquestopen', 'mapquestopenaerial', 'mapboxbright', 'mapbox', 'cloudmade', 'stamenterrain', 'stamentoner'] self.tile_types = {} for tile in self.default_tiles: tile_path = 'tiles/%s' % tile self.tile_types[tile] = { 'templ': self.env.get_template('%s/%s' % (tile_path, 'tiles.txt')), 'attr': self.env.get_template('%s/%s' % (tile_path, 'attr.txt')), } if self.tiles in self.tile_types: self.template_vars['Tiles'] = (self.tile_types[self.tiles]['templ'] .render(API_key=API_key)) self.template_vars['attr'] = (self.tile_types[self.tiles]['attr'] .render()) else: self.template_vars['Tiles'] = tiles if not attr: raise ValueError('Custom tiles must' ' also be passed an attribution') if isinstance(attr, binary_type): attr = text_type(attr, 'utf8') self.template_vars['attr'] = attr self.tile_types.update({'Custom': {'template': tiles, 'attr': attr}}) self.added_layers = [] self.template_vars.setdefault('wms_layers', []) self.template_vars.setdefault('tile_layers', [])
def __init__(self, location=None, width='100%', height='100%', tiles='OpenStreetMap', API_key=None, max_zoom=18, min_zoom=1, zoom_start=10, attr=None, min_lat=-90, max_lat=90, min_lon=-180, max_lon=180): """Create a Map with Folium and Leaflet.js Generate a base map of given width and height with either default tilesets or a custom tileset URL. The following tilesets are built-in to Folium. Pass any of the following to the "tiles" keyword: - "OpenStreetMap" - "MapQuest Open" - "MapQuest Open Aerial" - "Mapbox Bright" (Limited levels of zoom for free tiles) - "Mapbox Control Room" (Limited levels of zoom for free tiles) - "Stamen" (Terrain, Toner, and Watercolor) - "Cloudmade" (Must pass API key) - "Mapbox" (Must pass API key) - "CartoDB" (positron and dark_matter) You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: http://{s}.yourtiles.com/{z}/{x}/{y}.png Parameters ---------- location: tuple or list, default None Latitude and Longitude of Map (Northing, Easting). width: pixel int or percentage string (default: '100%') Width of the map. height: pixel int or percentage string (default: '100%') Height of the map. tiles: str, default 'OpenStreetMap' Map tileset to use. Can use defaults or pass a custom URL. API_key: str, default None API key for Cloudmade or Mapbox tiles. max_zoom: int, default 18 Maximum zoom depth for the map. zoom_start: int, default 10 Initial zoom level for the map. attr: string, default None Map tile attribution; only required if passing custom tile URL. Returns ------- Folium Map Object 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/' 'mapbox.control-room/{z}/{x}/{y}.png'), attr='Mapbox attribution') """ # Inits. self.map_path = None self.render_iframe = False self.map_type = 'base' self.map_id = '_'.join(['folium', uuid4().hex]) # Mark counter, JSON, Plugins. self.mark_cnt = {} self.json_data = {} self.plugins = {} # No location means we will use automatic bounds and ignore zoom self.location = location # If location is not passed, we center the map at 0,0 if not location: location = [0, 0] zoom_start = min_zoom # Map Size Parameters. try: if isinstance(width, int): width_type = 'px' assert width > 0 else: width_type = '%' width = int(width.strip('%')) assert 0 <= width <= 100 except: msg = "Cannot parse width {!r} as {!r}".format raise ValueError(msg(width, width_type)) self.width = width try: if isinstance(height, int): height_type = 'px' assert height > 0 else: height_type = '%' height = int(height.strip('%')) assert 0 <= height <= 100 except: msg = "Cannot parse height {!r} as {!r}".format raise ValueError(msg(height, height_type)) self.height = height self.map_size = {'width': width, 'height': height} self._size = ('style="width: {0}{1}; height: {2}{3}"' .format(width, width_type, height, height_type)) # Templates. self.env = ENV self.template_vars = dict(lat=location[0], lon=location[1], size=self._size, max_zoom=max_zoom, zoom_level=zoom_start, map_id=self.map_id, min_zoom=min_zoom, min_lat=min_lat, max_lat=max_lat, min_lon=min_lon, max_lon=max_lon) # Tiles. self.tiles = ''.join(tiles.lower().strip().split()) if self.tiles in ('cloudmade', 'mapbox') and not API_key: raise ValueError('You must pass an API key if using Cloudmade' ' or non-default Mapbox tiles.') self.default_tiles = ['openstreetmap', 'mapboxcontrolroom', 'mapquestopen', 'mapquestopenaerial', 'mapboxbright', 'mapbox', 'cloudmade', 'stamenterrain', 'stamentoner', 'stamenwatercolor', 'cartodbpositron', 'cartodbdark_matter'] self.tile_types = {} for tile in self.default_tiles: tile_path = 'tiles/%s' % tile self.tile_types[tile] = { 'templ': self.env.get_template('%s/%s' % (tile_path, 'tiles.txt')), 'attr': self.env.get_template('%s/%s' % (tile_path, 'attr.txt')), } if self.tiles in self.tile_types: self.template_vars['Tiles'] = (self.tile_types[self.tiles]['templ'] .render(API_key=API_key)) self.template_vars['attr'] = (self.tile_types[self.tiles]['attr'] .render()) else: self.template_vars['Tiles'] = tiles if not attr: raise ValueError('Custom tiles must' ' also be passed an attribution') if isinstance(attr, binary_type): attr = text_type(attr, 'utf8') self.template_vars['attr'] = attr self.tile_types.update({'Custom': {'template': tiles, 'attr': attr}}) self.added_layers = [] self.template_vars.setdefault('wms_layers', []) self.template_vars.setdefault('tile_layers', []) self.template_vars.setdefault('image_layers', [])
def __init__(self, location=None, width=960, height=500, tiles='OpenStreetMap', API_key=None, max_zoom=18, zoom_start=10, attr=None): '''Create a Map with Folium and Leaflet.js Generate a base map of given width and height with either default tilesets or a custom tileset URL. The following tilesets are built-in to Folium. Pass any of the following to the "tiles" keyword: -"OpenStreetMap" -"MapQuest Open" -"MapQuest Open Aerial" -"Mapbox Bright" (Limited levels of zoom for free tiles) -"Mapbox Control Room" (Limited levels of zoom for free tiles) -"Stamen Terrain" -"Stamen Toner" -"Cloudmade" (Must pass API key) -"Mapbox" (Must pass API key) You can pass a custom tileset to Folium by passing a Leaflet-style URL to the tiles parameter: http://{s}.yourtiles.com/{z}/{x}/{y}.png Parameters ---------- location: tuple or list, default None Latitude and Longitude of Map (Northing, Easting). width: int, default 960 Width of the map. height: int, default 500 Height of the map. tiles: str, default 'OpenStreetMap' Map tileset to use. Can use defaults or pass a custom URL. API_key: str, default None API key for Cloudmade or Mapbox tiles. max_zoom: int, default 18 Maximum zoom depth for the map. zoom_start: int, default 10 Initial zoom level for the map. attr: string, default None Map tile attribution; only required if passing custom tile URL. Returns ------- Folium Map Object 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/' 'mapbox.control-room/{z}/{x}/{y}.png'), attr='Mapbox attribution') ''' #Inits self.map_path = None self.render_iframe = False self.map_type = 'base' self.map_id = '_'.join(['folium', uuid4().hex]) #Mark counter, JSON, Plugins self.mark_cnt = {} self.json_data = {} self.plugins = {} #Location if not location: raise ValueError('You must pass a Lat/Lon location to initialize' ' your map') self.location = location #Map Size Parameters self.width = width self.height = height self.map_size = {'width': width, 'height': height} self._size = ('style="width: {0}px; height: {1}px"' .format(width, height)) #Templates self.env = ENV self.template_vars = {'lat': location[0], 'lon': location[1], 'size': self._size, 'max_zoom': max_zoom, 'zoom_level': zoom_start, 'map_id': self.map_id} #Tiles self.tiles = ''.join(tiles.lower().strip().split()) if self.tiles in ('cloudmade', 'mapbox') and not API_key: raise ValueError('You must pass an API key if using Cloudmade' ' or non-default Mapbox tiles.') self.default_tiles = ['openstreetmap', 'mapboxcontrolroom', 'mapquestopen', 'mapquestopenaerial', 'mapboxbright', 'mapbox', 'cloudmade', 'stamenterrain', 'stamentoner'] self.tile_types = {} for tile in self.default_tiles: join_tile_path = functools.partial(os.path.join, 'tiles', tile) self.tile_types[tile] = { 'templ': self.env.get_template(join_tile_path('tiles.txt')), 'attr': self.env.get_template(join_tile_path('attr.txt')), } if self.tiles in self.tile_types: self.template_vars['Tiles'] = (self.tile_types[self.tiles]['templ'] .render(API_key=API_key)) self.template_vars['attr'] = (self.tile_types[self.tiles]['attr'] .render()) else: self.template_vars['Tiles'] = tiles if not attr: raise ValueError('Custom tiles must' ' also be passed an attribution') if isinstance(attr, binary_type): attr = text_type(attr, 'utf8') self.template_vars['attr'] = attr self.tile_types.update({'Custom': {'template': tiles, 'attr': attr}}) self.added_layers = [] self.template_vars.setdefault('wms_layers', []) self.template_vars.setdefault('tile_layers', [])