Created
April 17, 2024 12:56
-
-
Save jjclavijo/90ed0cbd451a739a273ea551e2465682 to your computer and use it in GitHub Desktop.
Overpass Consulta y Descarga.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNetJ+IK2gF3DxzIL9dzQUp", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jjclavijo/90ed0cbd451a739a273ea551e2465682/overpass-consulta-y-descarga.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import requests" | |
], | |
"metadata": { | |
"id": "ApyqeIZUF7Vr" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"id": "_yB-w_6hE23d" | |
}, | |
"outputs": [], | |
"source": [ | |
"query = b\"\"\"\n", | |
"[out:json];\n", | |
"area[\"ISO3166-1\"=\"AR\"]->.ar;\n", | |
"way(area.ar)[\"highway\"=\"trunk\"];\n", | |
"(._;>;);out;\n", | |
"\"\"\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"consulta_web = requests.post(\"https://overpass-api.de/api/interpreter\",data = query)" | |
], | |
"metadata": { | |
"id": "P1dEorUcFS4m" | |
}, | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install osm2geojson" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "2LoezJEDGvN6", | |
"outputId": "944bc005-a324-46bd-878e-27e092a4609a" | |
}, | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Collecting osm2geojson\n", | |
" Downloading osm2geojson-0.2.5.tar.gz (14 kB)\n", | |
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
"Requirement already satisfied: shapely in /usr/local/lib/python3.10/dist-packages (from osm2geojson) (2.0.3)\n", | |
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from osm2geojson) (2.31.0)\n", | |
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->osm2geojson) (3.3.2)\n", | |
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->osm2geojson) (3.6)\n", | |
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->osm2geojson) (2.0.7)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->osm2geojson) (2024.2.2)\n", | |
"Requirement already satisfied: numpy<2,>=1.14 in /usr/local/lib/python3.10/dist-packages (from shapely->osm2geojson) (1.25.2)\n", | |
"Building wheels for collected packages: osm2geojson\n", | |
" Building wheel for osm2geojson (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for osm2geojson: filename=osm2geojson-0.2.5-py3-none-any.whl size=13947 sha256=088f5e4cbbbd4f4b1ca1afa55c1aaaeaf73176a6632399fd378a64ae32980a7d\n", | |
" Stored in directory: /root/.cache/pip/wheels/44/9d/4c/a859e0f56957a67bf06c3a5a14899205c3b1864471fcc3c2cc\n", | |
"Successfully built osm2geojson\n", | |
"Installing collected packages: osm2geojson\n", | |
"Successfully installed osm2geojson-0.2.5\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import osm2geojson\n", | |
"import json" | |
], | |
"metadata": { | |
"id": "MlQrLraiGzK_" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"with open('data.geojson','w') as f:\n", | |
" json.dump(\n", | |
" osm2geojson.json2geojson( consulta_web.json() ),\n", | |
" f\n", | |
" )" | |
], | |
"metadata": { | |
"id": "rzK8ITloFzFe" | |
}, | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"from google.colab import files" | |
], | |
"metadata": { | |
"id": "GXB9KS8HHoLJ" | |
}, | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"files.download('data.geojson')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 17 | |
}, | |
"id": "sdNGihfQH7Cr", | |
"outputId": "c80cba3c-54a8-46b9-a6a1-8f942159d638" | |
}, | |
"execution_count": 14, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"\n", | |
" async function download(id, filename, size) {\n", | |
" if (!google.colab.kernel.accessAllowed) {\n", | |
" return;\n", | |
" }\n", | |
" const div = document.createElement('div');\n", | |
" const label = document.createElement('label');\n", | |
" label.textContent = `Downloading \"${filename}\": `;\n", | |
" div.appendChild(label);\n", | |
" const progress = document.createElement('progress');\n", | |
" progress.max = size;\n", | |
" div.appendChild(progress);\n", | |
" document.body.appendChild(div);\n", | |
"\n", | |
" const buffers = [];\n", | |
" let downloaded = 0;\n", | |
"\n", | |
" const channel = await google.colab.kernel.comms.open(id);\n", | |
" // Send a message to notify the kernel that we're ready.\n", | |
" channel.send({})\n", | |
"\n", | |
" for await (const message of channel.messages) {\n", | |
" // Send a message to notify the kernel that we're ready.\n", | |
" channel.send({})\n", | |
" if (message.buffers) {\n", | |
" for (const buffer of message.buffers) {\n", | |
" buffers.push(buffer);\n", | |
" downloaded += buffer.byteLength;\n", | |
" progress.value = downloaded;\n", | |
" }\n", | |
" }\n", | |
" }\n", | |
" const blob = new Blob(buffers, {type: 'application/binary'});\n", | |
" const a = document.createElement('a');\n", | |
" a.href = window.URL.createObjectURL(blob);\n", | |
" a.download = filename;\n", | |
" div.appendChild(a);\n", | |
" a.click();\n", | |
" div.remove();\n", | |
" }\n", | |
" " | |
] | |
}, | |
"metadata": {} | |
}, | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.Javascript object>" | |
], | |
"application/javascript": [ | |
"download(\"download_47006197-7add-47dc-bb05-3f21a5c45cb1\", \"data.geojson\", 14370426)" | |
] | |
}, | |
"metadata": {} | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment