{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import pandas as pd\n", "import geopandas as gpd\n", "import folium\n", "import shapely\n", "import matplotlib.pyplot as plt\n", "from shapely.ops import unary_union\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CODE_CHFCODE_DEPTCODE_REGID_GEOFLANOM_CHFNOM_DEPTNOM_REGX_CENTROIDX_CHF_LIEUY_CENTROIDY_CHF_LIEUgeometry
03003927DEPARTEM0000000000000001LONS-LE-SAUNIERJURABOURGOGNE-FRANCHE-COMTE88617289519866415486622537(POLYGON ((886244.1999999511 6641236.40000158,...
12184284DEPARTEM0000000000000002SAINT-ETIENNELOIREAUVERGNE-RHONE-ALPES79565580864665215816482549POLYGON ((764370.2999999805 6544751.000001852,...
25407628DEPARTEM0000000000000003ROUENSEINE-MARITIMENORMANDIE55094256235569528426928721POLYGON ((511688.8000000215 6966776.900000649,...
30248927DEPARTEM0000000000000004AUXERREYONNEBOURGOGNE-FRANCHE-COMTE74821174244767508556744261POLYGON ((709449.099999998 6765661.000001209, ...
40666844DEPARTEM0000000000000005COLMARHAUT-RHINALSACE-CHAMPAGNE-ARDENNE-LORRAINE1016174102412567638946784581POLYGON ((992779.0999999393 6768618.20000122, ...
\n", "
" ], "text/plain": [ " CODE_CHF CODE_DEPT CODE_REG ID_GEOFLA NOM_CHF \\\n", "0 300 39 27 DEPARTEM0000000000000001 LONS-LE-SAUNIER \n", "1 218 42 84 DEPARTEM0000000000000002 SAINT-ETIENNE \n", "2 540 76 28 DEPARTEM0000000000000003 ROUEN \n", "3 024 89 27 DEPARTEM0000000000000004 AUXERRE \n", "4 066 68 44 DEPARTEM0000000000000005 COLMAR \n", "\n", " NOM_DEPT NOM_REG X_CENTROID X_CHF_LIEU \\\n", "0 JURA BOURGOGNE-FRANCHE-COMTE 886172 895198 \n", "1 LOIRE AUVERGNE-RHONE-ALPES 795655 808646 \n", "2 SEINE-MARITIME NORMANDIE 550942 562355 \n", "3 YONNE BOURGOGNE-FRANCHE-COMTE 748211 742447 \n", "4 HAUT-RHIN ALSACE-CHAMPAGNE-ARDENNE-LORRAINE 1016174 1024125 \n", "\n", " Y_CENTROID Y_CHF_LIEU geometry \n", "0 6641548 6622537 (POLYGON ((886244.1999999511 6641236.40000158,... \n", "1 6521581 6482549 POLYGON ((764370.2999999805 6544751.000001852,... \n", "2 6952842 6928721 POLYGON ((511688.8000000215 6966776.900000649,... \n", "3 6750855 6744261 POLYGON ((709449.099999998 6765661.000001209, ... \n", "4 6763894 6784581 POLYGON ((992779.0999999393 6768618.20000122, ... " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gdf = gpd.GeoDataFrame.from_file(\"DEPARTEMENT/DEPARTEMENT.shp\")\n", "gdf.head()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "json = gdf.to_json()" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open(\"DEPARTEMENT/dep.geojson\", \"w\") as f:\n", " f.write(json)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(global) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " var force = \"1\";\n", "\n", " if (typeof (window._bokeh_onload_callbacks) === \"undefined\" || force !== \"\") {\n", " window._bokeh_onload_callbacks = [];\n", " window._bokeh_is_loading = undefined;\n", " }\n", "\n", "\n", " \n", " if (typeof (window._bokeh_timeout) === \"undefined\" || force !== \"\") {\n", " window._bokeh_timeout = Date.now() + 5000;\n", " window._bokeh_failed_load = false;\n", " }\n", "\n", " var NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " if (window.Bokeh !== undefined) {\n", " Bokeh.$(\"#5bee9007-46da-4f7c-81d1-414e140a434c\").text(\"BokehJS successfully loaded.\");\n", " } else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", " function run_callbacks() {\n", " window._bokeh_onload_callbacks.forEach(function(callback) { callback() });\n", " delete window._bokeh_onload_callbacks\n", " console.info(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(js_urls, callback) {\n", " window._bokeh_onload_callbacks.push(callback);\n", " if (window._bokeh_is_loading > 0) {\n", " console.log(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.log(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " window._bokeh_is_loading = js_urls.length;\n", " for (var i = 0; i < js_urls.length; i++) {\n", " var url = js_urls[i];\n", " var s = document.createElement('script');\n", " s.src = url;\n", " s.async = false;\n", " s.onreadystatechange = s.onload = function() {\n", " window._bokeh_is_loading--;\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: all BokehJS libraries loaded\");\n", " run_callbacks()\n", " }\n", " };\n", " s.onerror = function() {\n", " console.warn(\"failed to load library \" + url);\n", " };\n", " console.log(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.getElementsByTagName(\"head\")[0].appendChild(s);\n", " }\n", " };var element = document.getElementById(\"5bee9007-46da-4f7c-81d1-414e140a434c\");\n", " if (element == null) {\n", " console.log(\"Bokeh: ERROR: autoload.js configured with elementid '5bee9007-46da-4f7c-81d1-414e140a434c' but no matching script tag was found. \")\n", " return false;\n", " }\n", "\n", " var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.3.min.js'];\n", "\n", " var inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " \n", " function(Bokeh) {\n", " \n", " Bokeh.$(\"#5bee9007-46da-4f7c-81d1-414e140a434c\").text(\"BokehJS is loading...\");\n", " },\n", " function(Bokeh) {\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-0.12.3.min.css\");\n", " console.log(\"Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.3.min.css\");\n", " Bokeh.embed.inject_css(\"https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.3.min.css\");\n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if ((window.Bokeh !== undefined) || (force === \"1\")) {\n", " for (var i = 0; i < inline_js.length; i++) {\n", " inline_js[i](window.Bokeh);\n", " }if (force === \"1\") {\n", " display_loaded();\n", " }} else if (Date.now() < window._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!window._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " window._bokeh_failed_load = true;\n", " } else if (!force) {\n", " var cell = $(\"#5bee9007-46da-4f7c-81d1-414e140a434c\").parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (window._bokeh_is_loading === 0) {\n", " console.log(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(js_urls, function() {\n", " console.log(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(this));" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from bokeh.io import show, output_notebook\n", "from bokeh.models import GeoJSONDataSource, HoverTool, LinearColorMapper\n", "from bokeh.plotting import figure\n", "from bokeh.palettes import Viridis256\n", "\n", "output_notebook()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "with open(r'DEPARTEMENT/dep.geojson') as f:\n", " geo_src = GeoJSONDataSource(geojson=f.read())" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cmap = LinearColorMapper(palette=Viridis256)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "bokeh.model.Viewable" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(GeoJSONDataSource)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "
\n", "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TOOLS = \"pan,wheel_zoom,box_zoom,reset,hover,save\"\n", "\n", "p = figure(title='France Département', tools=TOOLS, x_axis_location=None, y_axis_location=None, width=500, height=500)\n", "p.grid.grid_line_color = None\n", "\n", "p.patches('xs', 'ys', fill_alpha=0.7, fill_color={'field': 'CODE_DEPT', 'transform': cmap},\n", " line_color='black', line_width=0.5, source=geo_src)\n", "\n", "hover = p.select_one(HoverTool)\n", "hover.point_policy = 'follow_mouse'\n", "hover.tooltips = [('Département:', '@NOM_DEPT')]\n", "\n", "show(p)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:geopandas]", "language": "python", "name": "conda-env-geopandas-py" }, "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": 1 }