Last active
April 1, 2022 09:07
-
-
Save liketaurus/d3fe15d6ca88a68d10842739f2ba97ff to your computer and use it in GitHub Desktop.
Air Alarms - a Python sample
This file contains hidden or 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": { | |
| "name": "Air Alarms", | |
| "provenance": [], | |
| "collapsed_sections": [], | |
| "authorship_tag": "ABX9TyODVcGTf/1AQk4Uws8nrBGi", | |
| "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/liketaurus/d3fe15d6ca88a68d10842739f2ba97ff/air-alarms.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Приклад - карта повітряних тривог в Україні\n", | |
| "Просто демонстрація можливостей **Python** та **Google Colaboratory**, створена з використанням API від [ukrzen](https://www.ukrzen.in.ua/), а також [Nominatium](https://nominatim.org/) та [Folium](https://python-visualization.github.io/folium/)\n", | |
| "\n", | |
| "---\n", | |
| "\n", | |
| "УВАГА! <small>Натисніть <kbd>CTRL</kbd>+<kbd>F9</kbd> щоб виконати всі комірки одна за одною!</small>" | |
| ], | |
| "metadata": { | |
| "id": "mRg3Y7pMPrCN" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "zQQ3SijX08sE", | |
| "outputId": "048950c1-0f87-4b6b-82cf-092335969a40" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "# оримуємо дані - список назв місць, де лунає тривога\n", | |
| "\n", | |
| "json_url=\"https://war-api.ukrzen.in.ua/alerts/api/alerts/active.json\" \n", | |
| "\n", | |
| "import requests, json\n", | |
| "url = requests.get(json_url)\n", | |
| "text = url.text\n", | |
| "data = json.loads(text)\n", | |
| "\n", | |
| "alerts = data[\"alerts\"]\n", | |
| "places = [] \n", | |
| "for i in alerts:\n", | |
| " places.append(i[\"location_title\"])\n", | |
| "\n", | |
| "# виводимо список назв у стовпчик\n", | |
| "print(*places, sep = \"\\n\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# отримуємо координати за назвою для всіх місць з отриманого списку\n", | |
| "\n", | |
| "from geopy.geocoders import Nominatim\n", | |
| "\n", | |
| "geolocator = Nominatim(user_agent=\"air_alarms\")\n", | |
| "coords = []\n", | |
| "\n", | |
| "for j in places:\n", | |
| " coords.append([geolocator.geocode(j).latitude,geolocator.geocode(j).longitude])\n", | |
| "\n", | |
| "# виводимо список координат у стовпчик\n", | |
| "print(*coords, sep = \"\\n\")" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "JzoXXaO3_02Q", | |
| "outputId": "1ccf9515-4f5d-4373-e4fc-5b0ba0438dee" | |
| }, | |
| "execution_count": 14, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# формуємо карту України\n", | |
| "import folium\n", | |
| "from folium import plugins\n", | |
| "\n", | |
| "m = folium.Map(location=[50.450001, 30.523333], zoom_start=6.1)\n", | |
| "\n", | |
| "# ставимо маркери в місцях, де лунає тривога (з іконкою та спливаючим вікном з назвою)\n", | |
| "for k in coords:\n", | |
| " folium.Marker(\n", | |
| " location=k,\n", | |
| " popup=folium.Popup(max_width=450, html=places[coords.index(k)]), # places[coords.index(k)]\n", | |
| " icon=folium.Icon(color='red', icon='fighter-jet', prefix='fa')\n", | |
| " ).add_to(m)\n", | |
| "\n", | |
| "# додаємо перемикач шарів карти\n", | |
| "folium.TileLayer('stamentoner').add_to(m)\n", | |
| "folium.TileLayer('cartodbpositron').add_to(m)\n", | |
| "folium.TileLayer('openstreetmap').add_to(m)\n", | |
| "\n", | |
| "folium.LayerControl().add_to(m)\n", | |
| "\n", | |
| "# можемо також додати зображення чи логотип поверх карти\n", | |
| "# from folium.plugins import FloatImage\n", | |
| "# image_url=\"https://static-s.aa-cdn.net/img/amazon/30600000064804/deb2b1adfaa66c2f7b15b647c15fb4b3?v=1\"\n", | |
| "# FloatImage(image_url, bottom=60, left=6).add_to(m)\n", | |
| "\n", | |
| "# відображаємо карту\n", | |
| "m" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 725 | |
| }, | |
| "id": "YwR8b6uO8byA", | |
| "outputId": "9c7ca821-5988-459b-b658-b2ce653505ce" | |
| }, | |
| "execution_count": 15, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "<folium.folium.Map at 0x7f495060bc50>" | |
| ], | |
| "text/html": [ | |
| "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe src=\"about:blank\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" data-html=%3C%21DOCTYPE%20html%3E%0A%3Chead%3E%20%20%20%20%0A%20%20%20%20%3Cmeta%20http-equiv%3D%22content-type%22%20content%3D%22text/html%3B%20charset%3DUTF-8%22%20/%3E%0A%20%20%20%20%3Cscript%3EL_PREFER_CANVAS%3Dfalse%3B%20L_NO_TOUCH%3Dfalse%3B%20L_DISABLE_3D%3Dfalse%3B%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//cdn.jsdelivr.net/npm/leaflet%401.4.0/dist/leaflet.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//code.jquery.com/jquery-1.12.4.min.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js%22%3E%3C/script%3E%0A%20%20%20%20%3Cscript%20src%3D%22https%3A//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js%22%3E%3C/script%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//cdn.jsdelivr.net/npm/leaflet%401.4.0/dist/leaflet.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css%22/%3E%0A%20%20%20%20%3Clink%20rel%3D%22stylesheet%22%20href%3D%22https%3A//rawcdn.githack.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css%22/%3E%0A%20%20%20%20%3Cstyle%3Ehtml%2C%20body%20%7Bwidth%3A%20100%25%3Bheight%3A%20100%25%3Bmargin%3A%200%3Bpadding%3A%200%3B%7D%3C/style%3E%0A%20%20%20%20%3Cstyle%3E%23map%20%7Bposition%3Aabsolute%3Btop%3A0%3Bbottom%3A0%3Bright%3A0%3Bleft%3A0%3B%7D%3C/style%3E%0A%20%20%20%20%0A%20%20%20%20%3Cmeta%20name%3D%22viewport%22%20content%3D%22width%3Ddevice-width%2C%0A%20%20%20%20%20%20%20%20initial-scale%3D1.0%2C%20maximum-scale%3D1.0%2C%20user-scalable%3Dno%22%20/%3E%0A%20%20%20%20%3Cstyle%3E%23map_61b7f04c23a945ae951e8b13b702a12f%20%7B%0A%20%20%20%20%20%20%20%20position%3A%20relative%3B%0A%20%20%20%20%20%20%20%20width%3A%20100.0%25%3B%0A%20%20%20%20%20%20%20%20height%3A%20100.0%25%3B%0A%20%20%20%20%20%20%20%20left%3A%200.0%25%3B%0A%20%20%20%20%20%20%20%20top%3A%200.0%25%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%3C/style%3E%0A%3C/head%3E%0A%3Cbody%3E%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%3Cdiv%20class%3D%22folium-map%22%20id%3D%22map_61b7f04c23a945ae951e8b13b702a12f%22%20%3E%3C/div%3E%0A%3C/body%3E%0A%3Cscript%3E%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20var%20bounds%20%3D%20null%3B%0A%20%20%20%20%0A%0A%20%20%20%20var%20map_61b7f04c23a945ae951e8b13b702a12f%20%3D%20L.map%28%0A%20%20%20%20%20%20%20%20%27map_61b7f04c23a945ae951e8b13b702a12f%27%2C%20%7B%0A%20%20%20%20%20%20%20%20center%3A%20%5B50.450001%2C%2030.523333%5D%2C%0A%20%20%20%20%20%20%20%20zoom%3A%206.1%2C%0A%20%20%20%20%20%20%20%20maxBounds%3A%20bounds%2C%0A%20%20%20%20%20%20%20%20layers%3A%20%5B%5D%2C%0A%20%20%20%20%20%20%20%20worldCopyJump%3A%20false%2C%0A%20%20%20%20%20%20%20%20crs%3A%20L.CRS.EPSG3857%2C%0A%20%20%20%20%20%20%20%20zoomControl%3A%20true%2C%0A%20%20%20%20%20%20%20%20%7D%29%3B%0A%0A%0A%20%20%20%20%0A%20%20%20%20var%20tile_layer_69217c91e0b74b0f89f92c54a707437d%20%3D%20L.tileLayer%28%0A%20%20%20%20%20%20%20%20%27https%3A//%7Bs%7D.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png%27%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22attribution%22%3A%20null%2C%0A%20%20%20%20%20%20%20%20%22detectRetina%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22maxNativeZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22maxZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22minZoom%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%22noWrap%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22opacity%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22subdomains%22%3A%20%22abc%22%2C%0A%20%20%20%20%20%20%20%20%22tms%22%3A%20false%0A%7D%29.addTo%28map_61b7f04c23a945ae951e8b13b702a12f%29%3B%0A%20%20%20%20var%20tile_layer_7dfc4cbdb9b540d78480ae81e638e0f2%20%3D%20L.tileLayer%28%0A%20%20%20%20%20%20%20%20%27https%3A//stamen-tiles-%7Bs%7D.a.ssl.fastly.net/toner/%7Bz%7D/%7Bx%7D/%7By%7D.png%27%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22attribution%22%3A%20null%2C%0A%20%20%20%20%20%20%20%20%22detectRetina%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22maxNativeZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22maxZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22minZoom%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%22noWrap%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22opacity%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22subdomains%22%3A%20%22abc%22%2C%0A%20%20%20%20%20%20%20%20%22tms%22%3A%20false%0A%7D%29.addTo%28map_61b7f04c23a945ae951e8b13b702a12f%29%3B%0A%20%20%20%20var%20tile_layer_7abe78256f344425a1731bb0cd800795%20%3D%20L.tileLayer%28%0A%20%20%20%20%20%20%20%20%27https%3A//cartodb-basemaps-%7Bs%7D.global.ssl.fastly.net/light_all/%7Bz%7D/%7Bx%7D/%7By%7D.png%27%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22attribution%22%3A%20null%2C%0A%20%20%20%20%20%20%20%20%22detectRetina%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22maxNativeZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22maxZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22minZoom%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%22noWrap%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22opacity%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22subdomains%22%3A%20%22abc%22%2C%0A%20%20%20%20%20%20%20%20%22tms%22%3A%20false%0A%7D%29.addTo%28map_61b7f04c23a945ae951e8b13b702a12f%29%3B%0A%20%20%20%20var%20tile_layer_5d79892779e8419ba4b9c66a04bf0eab%20%3D%20L.tileLayer%28%0A%20%20%20%20%20%20%20%20%27https%3A//%7Bs%7D.tile.openstreetmap.org/%7Bz%7D/%7Bx%7D/%7By%7D.png%27%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%22attribution%22%3A%20null%2C%0A%20%20%20%20%20%20%20%20%22detectRetina%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22maxNativeZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22maxZoom%22%3A%2018%2C%0A%20%20%20%20%20%20%20%20%22minZoom%22%3A%200%2C%0A%20%20%20%20%20%20%20%20%22noWrap%22%3A%20false%2C%0A%20%20%20%20%20%20%20%20%22opacity%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22subdomains%22%3A%20%22abc%22%2C%0A%20%20%20%20%20%20%20%20%22tms%22%3A%20false%0A%7D%29.addTo%28map_61b7f04c23a945ae951e8b13b702a12f%29%3B%0A%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20var%20layer_control_ff010355924d460cb4beda075f117e83%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20base_layers%20%3A%20%7B%20%22openstreetmap%22%20%3A%20tile_layer_5d79892779e8419ba4b9c66a04bf0eab%2C%22stamentoner%22%20%3A%20tile_layer_7dfc4cbdb9b540d78480ae81e638e0f2%2C%22cartodbpositron%22%20%3A%20tile_layer_7abe78256f344425a1731bb0cd800795%2C%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20overlays%20%3A%20%7B%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20L.control.layers%28%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20layer_control_ff010355924d460cb4beda075f117e83.base_layers%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20layer_control_ff010355924d460cb4beda075f117e83.overlays%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7Bposition%3A%20%27topright%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20collapsed%3A%20true%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20autoZIndex%3A%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%29.addTo%28map_61b7f04c23a945ae951e8b13b702a12f%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tile_layer_7dfc4cbdb9b540d78480ae81e638e0f2.remove%28%29%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20tile_layer_7abe78256f344425a1731bb0cd800795.remove%28%29%3B%0A%20%20%20%20%20%20%20%20%0A%3C/script%3E onload=\"this.contentDocument.open();this.contentDocument.write( decodeURIComponent(this.getAttribute('data-html')));this.contentDocument.close();\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 15 | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment