{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3.0.dev\n" ] } ], "source": [ "import os\n", "import folium\n", "\n", "print(folium.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " # Hacking folium" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from folium.features import Template\n", "\n", "\n", "class Map3d(folium.Map):\n", "\n", " def __init__(self, location=None, width='100%', height='100%', left='0%',\n", " top='0%', position='relative', tiles='OpenStreetMap', API_key=None,\n", " max_zoom=18, min_zoom=1, zoom_start=10, attr=None, min_lat=-90,\n", " max_lat=90, min_lon=-180, max_lon=180, detect_retina=False, crs='EPSG3857'):\n", " super(Map3d, self).__init__(\n", " location=location, width=width, height=height,\n", " left=left, top=top, position=position, tiles=tiles,\n", " API_key=API_key, max_zoom=max_zoom, min_zoom=min_zoom,\n", " zoom_start=zoom_start, attr=attr, min_lat=min_lat,\n", " max_lat=max_lat, min_lon=min_lon, max_lon=max_lon,\n", " detect_retina=detect_retina, crs=crs\n", " )\n", " self._template = Template(u\"\"\"\n", " {% macro header(this, kwargs) %}\n", " \n", " \n", " {% endmacro %}\n", " {% macro html(this, kwargs) %}\n", "
\n", " {% endmacro %}\n", "\n", " {% macro script(this, kwargs) %}\n", "\n", " var southWest = L.latLng({{ this.min_lat }}, {{ this.min_lon }});\n", " var northEast = L.latLng({{ this.max_lat }}, {{ this.max_lon }});\n", " var bounds = L.latLngBounds(southWest, northEast);\n", "\n", " var {{this.get_name()}} = WE.map('{{this.get_name()}}', {\n", " center:[{{this.location[0]}},{{this.location[1]}}],\n", " zoom: {{this.zoom_start}},\n", " maxBounds: bounds,\n", " layers: [],\n", " crs: L.CRS.{{this.crs}}\n", " });\n", " {% endmacro %}\n", " \"\"\")\n", "\n", "\n", "class TileLayer3d(folium.TileLayer):\n", "\n", " def __init__(self, tiles='OpenStreetMap', min_zoom=1, max_zoom=18, attr=None,\n", " API_key=None, detect_retina=False, name=None, overlay=False, control=True):\n", " super(TileLayer3d, self).__init__(\n", " tiles=tiles, min_zoom=min_zoom, max_zoom=max_zoom,\n", " attr=attr, API_key=API_key, detect_retina=detect_retina,\n", " name=name, overlay=overlay, control=control\n", " )\n", " self._template = Template(u\"\"\"\n", " {% macro script(this, kwargs) %}\n", " var {{this.get_name()}} = WE.tileLayer(\n", " '{{this.tiles}}',\n", " {\n", " maxZoom: {{this.max_zoom}},\n", " minZoom: {{this.min_zoom}},\n", " attribution: '{{this.attr}}',\n", " detectRetina: {{this.detect_retina.__str__().lower()}}\n", " }\n", " ).addTo({{this._parent.get_name()}});\n", "\n", " {% endmacro %}\n", " \"\"\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "<__main__.Map3d at 0x7fdb1e2e4160>" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m = Map3d(location=[-42, -42], tiles=None, zoom_start=2)\n", "m.add_child(TileLayer3d(tiles='OpenStreetMap'))\n", "\n", "url = ('http://services.arcgisonline.com/arcgis/rest/services'\n", " 'Ocean/World_Ocean_Base'\n", " 'MapServer/tile/{z}/{y}/{x}')\n", "\n", "folium.TileLayer(\n", " tiles=url,\n", " name='World_Ocean_Base',\n", " attr='ESRI',\n", " overlay=True\n", ").add_to(m)\n", "\n", "\n", "m.save(os.path.join('results', 'Hacking_folium_3D_globe.html'))\n", "\n", "m" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 0 }