Skip to content

Instantly share code, notes, and snippets.

@nlehuby
Created February 12, 2019 20:47
Show Gist options
  • Save nlehuby/ccd12762e07e124facfcf9aacb087f15 to your computer and use it in GitHub Desktop.
Save nlehuby/ccd12762e07e124facfcf9aacb087f15 to your computer and use it in GitHub Desktop.
POC - tracés de route relations
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from polyline.codec import PolylineCodec\n",
"\n",
"osm_route_id = 9190120"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# On récupère les arrêts du parcours, dans l'ordre de desserte\n",
"(en espérant qu'ils sont déjà dans le bon ordre dans OSM)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Bus 702 : Gare d'Aulnay-sous-Bois → Val de Fontenay RER\n",
"Autobus du Fort\n",
"Autobus du Fort\n"
]
}
],
"source": [
"overpass_base = \"https://overpass-api.de/api/interpreter?data=\"\n",
"overpass_request = \"[out:json][timeout:25];relation({});(._;>;);out;\".format(osm_route_id)\n",
"\n",
"overpass_call = requests.get (overpass_base + overpass_request)\n",
"\n",
"nodes = {}\n",
"stops=[]\n",
"\n",
"for element in overpass_call.json()['elements']:\n",
" if element['type'] == \"node\":\n",
" nodes[element['id']] = element\n",
" \n",
" if element['id'] == osm_route_id:\n",
" print(element['tags']['name'])\n",
" print(element['tags']['operator'])\n",
" print(element['tags']['network'])\n",
" \n",
" for member in element['members']:\n",
" if member['role'] in ['platform', 'platform_entry_only', 'platform_exit_only']:\n",
" member_id = member['ref']\n",
" stops.append(nodes[member_id])\n",
"\n",
"#stops"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# On calcule un itinéraire (voiture) entre ces arrêts dans l'ordre\n",
"\n",
"http://project-osrm.org/docs/v5.5.1/api/#route-service"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs":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mrm_base = \"https://routing.openstreetmap.de/routed-car/route/v1/driving/\"\n",
"\n",
"osrm_request = \"\"\n",
"for stop in stops:\n",
" osrm_request += \"{},{};\".format(stop['lon'], stop['lat'])\n",
"\n",
"osrm_request = osrm_request[:-1]\n",
"osrm_call = requests.get (osmrm_base + osrm_request, params = {\"overview\": \"full\"} )\n",
"\n",
"geom_in_polyline = osrm_call.json()['routes'][0]['geometry']\n",
"geom_in_polyline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# On cherche avec overpass les chemins qui passent à proximité "
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def polyline_to_overpass_around(polyline = None):\n",
" if polyline == None:\n",
" raise Exception(\"Need a Polyline as parameter\")\n",
"\n",
" waypoints = None\n",
" try:\n",
" waypoints = PolylineCodec().decode(polyline)\n",
" except Exception as e:\n",
" raise Exception(\"Error decoding polyline. err: {}\".format(e))\n",
"\n",
" overpass_query = \"(around:20\"\n",
"\n",
" for point in waypoints:\n",
" lat, lon = point\n",
" overpass_query += \",{},{}\".format(lat,lon)\n",
" #print(lat,lon)\n",
" \n",
" overpass_query += \")\"\n",
" return overpass_query"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'[out:xml][timeout:250];(way[highway](around:20,48.93134,2.49445,48.93134,2.49442,48.93133,2.4939,48.93133,2.49367,48.93168,2.49364,48.93171,2.49363,48.93175,2.49358,48.93178,2.49353,48.93179,2.49351,48.9318,2.49344,48.93186,2.49313,48.93193,2.49234,48.93192,2.49145,48.9319,2.49081,48.93192,2.49082,48.93194,2.49082,48.93196,2.4908,48.93197,2.49079,48.93198,2.49076,48.93199,2.49073,48.93199,2.4907,48.93198,2.49067,48.93197,2.49065,48.93195,2.49063,48.93193,2.49063,48.93191,2.49062,48.9319,2.49063,48.93188,2.49065,48.93183,2.4903,48.93177,2.4896,48.9317,2.48928,48.93159,2.48886,48.93157,2.48874,48.93153,2.48861,48.93148,2.48837,48.93142,2.48798,48.93146,2.48761,48.9315,2.48741,48.93157,2.48722,48.93165,2.48703,48.93172,2.48679,48.93178,2.48655,48.93181,2.4863,48.93182,2.4862,48.93182,2.48617,48.93184,2.48563,48.93184,2.48535,48.93183,2.48517,48.93179,2.48495,48.93173,2.48477,48.93134,2.48384,48.93125,2.48366,48.93114,2.48351,48.93098,2.48336,48.9309,2.4833,48.93087,2.48307,48.93081,2.4828,48.93079,2.48275,48.93071,2.48256,48.93068,2.48248,48.93061,2.48233,48.93048,2.48201,48.93044,2.48178,48.93043,2.48171,48.93043,2.48154,48.93044,2.48135,48.93043,2.48122,48.93039,2.48112,48.93032,2.48103,48.93019,2.48092,48.93003,2.48082,48.92984,2.48081,48.9297,2.48078,48.92951,2.48084,48.9291,2.48089,48.92899,2.48091,48.92868,2.48095,48.92824,2.48096,48.92745,2.48108,48.92689,2.48113,48.9264,2.48116,48.92629,2.48128,48.92579,2.48119,48.92517,2.48108,48.92478,2.48099,48.92405,2.48077,48.92315,2.48041,48.92041,2.47924,48.91961,2.47891,48.91844,2.47849,48.91785,2.47832,48.91748,2.47821,48.91634,2.47789,48.91569,2.4777,48.9149,2.47745,48.91469,2.47738,48.91448,2.4773,48.91412,2.47716,48.91376,2.47701,48.913,2.47663,48.91228,2.47624,48.91143,2.4757,48.91089,2.4753,48.91064,2.47511,48.90964,2.47432,48.9082,2.47317,48.90797,2.473,48.90753,2.47269,48.90703,2.47245,48.90672,2.47232,48.90626,2.47213,48.90372,2.47119,48.90314,2.47094,48.90283,2.47078,48.90209,2.47036,48.90135,2.46998,48.90091,2.46977,48.90065,2.46964,48.89986,2.4694,48.89902,2.46929,48.89866,2.46927,48.89829,2.4693,48.89796,2.46935,48.89752,2.46948,48.89692,2.46975,48.89664,2.46987,48.89589,2.47037,48.89517,2.47091,48.8938,2.47195,48.8933,2.47227,48.89286,2.47252,48.89247,2.4727,48.892,2.47287,48.89091,2.47315,48.89025,2.4733,48.88933,2.47362,48.88921,2.47361,48.88898,2.47367,48.88862,2.47379,48.88838,2.47382,48.88813,2.47383,48.88782,2.47382,48.88754,2.47376,48.88725,2.47365,48.88701,2.47353,48.88665,2.4733,48.88636,2.47305,48.88609,2.47275,48.88596,2.47259,48.88569,2.47224,48.88555,2.47204,48.88543,2.47183,48.88523,2.47142,48.88505,2.471,48.88499,2.47081,48.88491,2.47056,48.88484,2.47037,48.88477,2.4702,48.88461,2.46989,48.88447,2.46971,48.88432,2.46959,48.88428,2.46957,48.8842,2.46953,48.88406,2.46949,48.88393,2.46948,48.8838,2.46952,48.88365,2.4696,48.88348,2.46975,48.88333,2.46994,48.88319,2.47022,48.88275,2.47127,48.88249,2.47187,48.88176,2.47339,48.8816,2.4737,48.88135,2.47418,48.88123,2.47442,48.8811,2.47462,48.88096,2.47482,48.88081,2.47498,48.88049,2.47525,48.87939,2.47596,48.87926,2.47606,48.87918,2.47611,48.8778,2.47699,48.87754,2.47716,48.87708,2.47747,48.87553,2.47847,48.87468,2.47896,48.87371,2.47951,48.87317,2.4799,48.87265,2.48042,48.87168,2.48151,48.87156,2.48162,48.87121,2.48194,48.8712,2.48196,48.87079,2.4822,48.87037,2.48237,48.86988,2.48246,48.86957,2.48244,48.86905,2.48228,48.8682,2.48193,48.86744,2.48163,48.8669,2.48149,48.8665,2.48143,48.8662,2.48145,48.86589,2.4815,48.86541,2.48166,48.86501,2.48186,48.86371,2.4825,48.86337,2.48264,48.86314,2.48265,48.86182,2.48292,48.86161,2.48303,48.86139,2.48316,48.86117,2.48338,48.86096,2.48373,48.86084,2.48399,48.86068,2.48441,48.86055,2.48482,48.86041,2.48532,48.86028,2.48588,48.86024,2.48622,48.86022,2.48657,48.86023,2.48682,48.86024,2.48727,48.86025,2.48762,48.86025,2.48794,48.86024,2.48827,48.86021,2.48856,48.86015,2.4889,48.86008,2.4892,48.86,2.48941,48.85987,2.48969,48.85964,2.49005,48.8595,2.49021,48.85936,2.49038,48.85913,2.4906,48.85883,2.49078,48.85866,2.49087,48.85788,2.49127,48.85757,2.49143,48.85753,2.49145,48.85711,2.49167,48.8569,2.49177,48.85672,2.49187,48.85622,2.49213,48.85507,2.49274,48.85494,2.4928,48.8548,2.49286,48.85458,2.49294,48.85445,2.49299,48.85432,2.49304,48.85424,2.49307,48.85417,2.49309,48.85409,2.4931,48.854,2.49309,48.8539,2.49303,48.85383,2.49297,48.85379,2.49292,48.85375,2.49286,48.85372,2.49281,48.85361,2.49255,48.85337,2.49196,48.85289,2.49084,48.85285,2.49078,48.8528,2.49073,48.85275,2.49066,48.85275,2.4906,48.85274,2.49055,48.85257,2.48959,48.8525,2.48919,48.8525,2.48916,48.85246,2.48865,48.85246,2.48809,48.85247,2.48748,48.85248,2.48691,48.85249,2.48668,48.85251,2.48655,48.85252,2.4865,48.85256,2.48634,48.85263,2.48628,48.8527,2.48618,48.85274,2.48605,48.85285,2.48608,48.8529,2.48605,48.85306,2.48584,48.85312,2.48573,48.85321,2.48578,48.85385,2.48683,48.85387,2.48688,48.85406,2.4872););out meta;>;out meta qt;'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"full_overpass_request = \"[out:xml][timeout:250];(way[highway]{};);out meta;>;out meta qt;\".format(polyline_to_overpass_around(geom_in_polyline))\n",
"full_overpass_request"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# On charge cette requête dans JOSM\n",
"\n",
"Fichier > Télécharger les données > Télécharger depuis l'API Overpass\n",
"\n",
"![](https://i.imgur.com/l8AXKRt.png)\n",
"\n",
"Puis on cartographie avec le plugin PT Assistant et les validateurs Jungle Bus ;)"
]
}
],
"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