Skip to content

Instantly share code, notes, and snippets.

Created October 27, 2017 00:37
Show Gist options
  • Save anonymous/28f0a54bd03991ff6968e1d762d0b5a2 to your computer and use it in GitHub Desktop.
Save anonymous/28f0a54bd03991ff6968e1d762d0b5a2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from collections import OrderedDict\n",
"\n",
"from ipyleaflet import (\n",
" Map,\n",
" Marker,\n",
" TileLayer, ImageOverlay,\n",
" Polyline, Polygon, Rectangle, Circle, CircleMarker,\n",
" GeoJSON,\n",
" DrawControl\n",
")\n",
"\n",
"from IPython.display import display\n",
"\n",
"def handle_draw(self, action, geo_json):\n",
" print(action)\n",
" print(geo_json)\n",
" \n",
"class Grass2Leaflet(object):\n",
" def __init__(self, grassimg):\n",
" self.grassimg = grassimg\n",
" self.draw_control = None\n",
" self.zoom = 15\n",
" self.center = self.centermap()\n",
" self.m = Map(default_tiles=TileLayer(opacity=1.0), center=self.center, zoom=self.zoom)\n",
"\n",
" def centermap(self):\n",
" centerlat = []\n",
" centerlon = []\n",
" for i in self.grassimg:\n",
" centerlat.append(self.grassimg[i]['C'][0])\n",
" centerlon.append(self.grassimg[i]['C'][1])\n",
" center = (sum(centerlat) / float(len(centerlat)), sum(centerlon) / float(len(centerlon)))\n",
" return center\n",
" \n",
" def imgoverlays(self):\n",
" self.leafletimg = OrderedDict()\n",
" for i in self.grassimg:\n",
" layer = ImageOverlay(url=self.grassimg[i]['raster'],\n",
" bounds=(self.grassimg[i]['LL'], self.grassimg[i]['UR']))\n",
" self.leafletimg[i] = layer\n",
" \n",
" def render(self, draw_control=None):\n",
" self.imgoverlays()\n",
" self.dc = None\n",
" options = ['None']\n",
" self.m.add_layer(self.leafletimg[list(self.grassimg.keys())[-1]])\n",
" if len(self.grassimg) >= 2:\n",
" self.maplist = widgets.Dropdown(\n",
" options=options + list(self.grassimg.keys()),\n",
" value=list(self.grassimg.keys())[-1],\n",
" description='Select Layer:',\n",
" )\n",
" self.maplist.observe(self.on_value_change, names='value')\n",
" display(self.maplist)\n",
" if draw_control:\n",
" self.dc = DrawControl()\n",
" self.dc.on_draw(handle_draw)\n",
" self.m.add_control(self.dc)\n",
" display(self.m)\n",
" return {'map': self.m, 'drawer': self.dc}\n",
" \n",
" def on_value_change(self, layername):\n",
" self.m.clear_layers()\n",
" self.m.add_layer(TileLayer(opacity=1.0))\n",
" if self.maplist.value != 'None':\n",
" self.m.add_layer(self.leafletimg[layername['new']])\n",
" \n",
" def main(self):\n",
" self.imgoverlays()\n",
" self.render()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"grass_img = OrderedDict([('prj1_bag.2',\n",
" {'C': (41.06619379389716, -68.91168489766864),\n",
" 'LL': (41.06318098836321, -68.9322551709071),\n",
" 'UR': (41.06920292379777, -68.8911127540066),\n",
" 'proj': '+proj=utm +zone=19 +datum=WGS84 +units=m +no_defs ',\n",
" 'raster': 'test.jpg'})])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "86b7707f8aaa405e83129f08b0f1476b",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>Map</code>.</p>\n",
"<p>\n",
" If you're reading this message in Jupyter Notebook or JupyterLab, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another notebook frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"Map(center=[41.06619379389716, -68.91168489766864], controls=(DrawControl(layer=FeatureGroup(), polygon={'shapeOptions': {}}, polyline={'shapeOptions': {}}),), layers=(TileLayer(options=['max_zoom', 'tile_size', 'min_zoom', 'detect_retina', 'opacity', 'attribution']), ImageOverlay(bounds=[(41.06318098836321, -68.9322551709071), (41.06920292379777, -68.8911127540066)], options=['attribution', 'opacity'], url='test.jpg')), layout=Layout(align_self='stretch', height='400px'), options=['touch_zoom', 'center', 'min_zoom', 'inertia', 'tap_tolerance', 'dragging', 'zoom_start', 'bounce_at_zoom_limits', 'box_zoom', 'zoom_control', 'world_copy_jump', 'scroll_wheel_zoom', 'attribution_control', 'zoom_animation_threshold', 'close_popup_on_click', 'double_click_zoom', 'keyboard_zoom_offset', 'zoom', 'keyboard', 'inertia_max_speed', 'max_zoom', 'tap', 'inertia_deceleration', 'keyboard_pan_offset'], zoom=15)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"leaflet_widget = Grass2Leaflet(grass_img).render(draw_control=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment