{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Try-out automatic downloads of OSM data via turbo-overpass API:\n", "http://wiki.openstreetmap.org/wiki/Overpass_API#Python_API\n", "\n", "\n", "a python wrapper is provided by Martijn van Exel via github\n", "https://github.com/mvexel/overpass-api-python-wrapper\n", "\n", "install via pip (not available on conda repos)\n", "\"pip install overpass\"" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{\"geometry\": {\"coordinates\": [4.35839, 52.0114017], \"type\": \"Point\"}, \"id\": 274247451, \"properties\": {\"is_in\": \"NL\", \"is_in:continent\": \"Europe\", \"is_in:country\": \"The Netherlands\", \"is_in:country_code\": \"NL\", \"is_in:province\": \"South Holland\", \"name\": \"Delft\", \"name:carnaval\": \"Kabbelgat\", \"name:el\": \"\\u039d\\u03c4\\u03b5\\u03bb\\u03c6\\u03c4\", \"name:fa\": \"\\u062f\\u0644\\u0641\\u062a\", \"name:he\": \"\\u05d3\\u05dc\\u05e4\\u05d8\", \"name:ja\": \"\\u30c7\\u30eb\\u30d5\\u30c8\", \"name:ko\": \"\\ub378\\ud504\\ud2b8\", \"name:la\": \"Delphi\", \"name:nds\": \"Delft\", \"name:ru\": \"\\u0414\\u0435\\u043b\\u0444\\u0442\", \"name:sr\": \"\\u0414\\u0435\\u043b\\u0444\\u0442\", \"name:zh\": \"\\u4ee3\\u5c14\\u592b\\u7279\", \"place\": \"city\", \"population\": \"101022\", \"website\": \"http://www.delft.nl\", \"wikipedia\": \"nl:Delft\"}, \"type\": \"Feature\"}]\n" ] } ], "source": [ "import overpass\n", "api = overpass.API()\n", "\n", "# get city with name Delft\n", "response = api.Get('node[\"place\"=\"city\"][\"name\"=\"Delft\"]')\n", "\n", "# response in geojson\n", "from pprint import pprint\n", "pprint(response['features'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "response typically in geojson format. This can be visualized using folium " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ] }, "output_type": "execute_result", "metadata": {} } ], "source": [ "# now visualize with folium\n", "%matplotlib inline\n", "import folium\n", "\n", "\n", "delft_map = folium.Map(location=[52, 4.3], # NOTE: folium uses latlon format, instead of lonlat from geojson\n", " zoom_start=8)\n", "\n", "folium.GeoJson(response,\n", " name='delft'\n", " ).add_to(delft_map)\n", "\n", "delft_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "writing to file geojson" ] }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "import geojson\n", "\n", "def to_geojson(fn, js, epsg='4326'):\n", " \"\"\"writes a list of features to a geojson as a featurecollection\"\"\"\n", "\n", " crs = geojson.crs.Named(properties={\"name\": \"urn:ogc:def:crs:EPSG::{}\".format(epsg)})\n", " fcol = geojson.FeatureCollection(js['features'], crs=crs)\n", "\n", " # write file\n", " with open(fn, 'w') as f:\n", " geojson.dump(fcol, f)" ] }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# write response to geojson file\n", "gjson_fn = r'd:\\temp\\OSM_test\\Delft.geojson'\n", "to_geojson(gjson_fn, response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "now try to get all waterways in bbox" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "# all waterways -> slow!\n", "# waterway = api.Get('way[\"waterway\"!~\".\"](14.2,120.4,15.2,121);') ## bbox in north, west, south, east\n", "\n", "# download all rivers\n", "rivers = api.Get('way[\"waterway\"=\"river\"](14.2,120.4,15.2,121);') ## bbox in north, west, south, east\n", "\n", "# plot\n", "river_map = folium.Map(location=[14.8, 120.5], # NOTE: folium uses latlon format, instead of lonlat from geojson\n", " zoom_start=10)\n", "\n", "folium.GeoJson(rivers,\n", " name='delft'\n", " ).add_to(river_map)\n", "\n", "river_map" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# some utiles that might be usefull\n", "import numpy as np\n", "def flip_geojson_coordinates(geo):\n", " \"\"\"funciton to flip coordinates from latlon to lonlat and vica versa\"\"\"\n", " if isinstance(geo, dict):\n", " for k, v in geo.iteritems():\n", " if k == \"coordinates\":\n", " z = np.asarray(geo[k])\n", " f = z.flatten()\n", " geo[k] = np.dstack((f[1::2], f[::2])).reshape(z.shape).tolist()\n", " else:\n", " flip_geojson_coordinates(v)\n", " elif isinstance(geo, list):\n", " for k in geo:\n", " flip_geojson_coordinates(k)" ] } ], "metadata": { "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2.0 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }