{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Copy of Omdena_learning_series_ Nairobi Imagery.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "s_UfmKI-KF3t" }, "source": [ "# Sentinel-2A Data Download & Preprocessing\n", "This notebook shows you how to use GeoPandas, Google Earth Engine, and Rasterio to:\n", "\n", "1. Fetch Kenya administrative boundaries from a provided Geojson file \n", "2. Generate an Sentinel-2A RGB for one county\n" ] }, { "cell_type": "markdown", "metadata": { "id": "3uT3nGcmwNHj" }, "source": [ "## Installation" ] }, { "cell_type": "code", "metadata": { "id": "JN_NZz2XtgKm", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "b33844d1-0f35-4b62-e091-2dfd25371d6c" }, "source": [ "!pip -q install geopandas\n", "!pip -q install geojson\n", "!pip -q install --upgrade folium\n", "!pip -q install geemap\n", "!pip -q install rasterio\n", "!pip -q install tqdm\n" ], "execution_count": 1, "outputs": [ { "output_type": "stream", "text": [ "\u001b[K |████████████████████████████████| 1.0MB 4.1MB/s \n", "\u001b[K |████████████████████████████████| 15.4MB 196kB/s \n", "\u001b[K |████████████████████████████████| 6.6MB 41.7MB/s \n", "\u001b[K |████████████████████████████████| 102kB 3.1MB/s \n", "\u001b[31mERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.12.1 which is incompatible.\u001b[0m\n", "\u001b[K |████████████████████████████████| 471kB 3.8MB/s \n", "\u001b[K |████████████████████████████████| 1.3MB 10.4MB/s \n", "\u001b[K |████████████████████████████████| 81kB 8.1MB/s \n", "\u001b[K |████████████████████████████████| 225kB 15.7MB/s \n", "\u001b[K |████████████████████████████████| 1.6MB 17.6MB/s \n", "\u001b[K |████████████████████████████████| 102kB 10.2MB/s \n", "\u001b[K |████████████████████████████████| 3.3MB 25.4MB/s \n", "\u001b[K |████████████████████████████████| 1.2MB 41.2MB/s \n", "\u001b[K |████████████████████████████████| 81kB 9.2MB/s \n", "\u001b[K |████████████████████████████████| 143kB 42.0MB/s \n", "\u001b[K |████████████████████████████████| 71kB 7.6MB/s \n", "\u001b[K |████████████████████████████████| 122kB 43.4MB/s \n", "\u001b[K |████████████████████████████████| 122kB 44.1MB/s \n", "\u001b[K |████████████████████████████████| 552kB 39.5MB/s \n", "\u001b[K |████████████████████████████████| 399kB 42.6MB/s \n", "\u001b[K |████████████████████████████████| 81kB 9.4MB/s \n", "\u001b[K |████████████████████████████████| 71kB 8.3MB/s \n", "\u001b[?25h Building wheel for pyshp (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for ipynb-py-convert (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Building wheel for pycrs (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "\u001b[31mERROR: jupyter-server 1.9.0 has requirement tornado>=6.1.0, but you'll have tornado 5.1.1 which is incompatible.\u001b[0m\n", "\u001b[K |████████████████████████████████| 19.3MB 106kB/s \n", "\u001b[?25h" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "L0ZDKowgwPaM" }, "source": [ "## Imports & Setup" ] }, { "cell_type": "code", "metadata": { "id": "Ew3_MrrGslr4" }, "source": [ "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "from tqdm import tqdm\n", "import json\n", "import geojson\n", "import requests\n", "\n", "import rasterio as rio\n", "from rasterio.plot import show\n", "import rasterio.mask\n", "import geopandas as gpd\n", "import numpy as np\n", "\n", "\n", "from rasterio import windows\n", "from shapely.geometry import box\n", "import pandas as pd\n", "\n", "import ee\n", "import geemap\n", "import geemap.eefolium as emap" ], "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "u7UQeErEItTQ" }, "source": [ "## Mount Drive" ] }, { "cell_type": "code", "metadata": { "id": "IQvQCFgfIsoo", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "7ab8722c-6ef2-46d5-a155-332e49bfe8d5" }, "source": [ "from google.colab import drive\n", "drive.mount('/content/drive', force_remount=True)" ], "execution_count": 3, "outputs": [ { "output_type": "stream", "text": [ "Mounted at /content/drive\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "N8JfmJxa043J" }, "source": [ "### Authenticate Google Earth Engine" ] }, { "cell_type": "code", "metadata": { "id": "w5nXj4Fo03Wr", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "a962ca67-8271-467b-e4ce-54911a48e279" }, "source": [ "ee.Authenticate()\n", "ee.Initialize()" ], "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ "To authorize access needed by Earth Engine, open the following URL in a web browser and follow the instructions. If the web browser does not start automatically, please manually browse the URL below.\n", "\n", " https://accounts.google.com/o/oauth2/auth?client_id=517222506229-vsmmajv00ul0bs7p89v5m89qs8eb9359.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fearthengine+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&code_challenge=9lMJtQT4x_EVAYWaekGzvomNf4AfunCvQJmd_5NpoG8&code_challenge_method=S256\n", "\n", "The authorization workflow will generate a code, which you should paste in the box below. \n", "Enter verification code: 4/1AY0e-g75DOiFcQUzGz2fXDB0c25EMCeUiokY7orfIvg4P3YUsgeI10dNCNs\n", "\n", "Successfully saved authorization token.\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "_4hf8Z5CwSOE" }, "source": [ "## Fetch Kenya from google drive...hopefully you had earlier saved it" ] }, { "cell_type": "code", "metadata": { "id": "Qsd6loaZXfgR" }, "source": [ "kenya_Counties = '/content/drive/MyDrive/flood_impact/constituencies.geojson' #Change this to your geojson file destination path\n" ], "execution_count": 17, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "8LXtN8i_AYFU", "outputId": "fe1e546b-d8ac-4ffe-943a-6692e49566e4", "colab": { "base_uri": "https://localhost:8080/" } }, "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ], "execution_count": 18, "outputs": [ { "output_type": "stream", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "DU6WjKOAzWh5" }, "source": [ "## Visualize Counties in Kenya" ] }, { "cell_type": "code", "metadata": { "id": "hW8zZOy9t613", "colab": { "base_uri": "https://localhost:8080/", "height": 193 }, "outputId": "4c142bb5-aad5-4a8d-e73d-f830179abe42" }, "source": [ "\n", "##load the geojson and find the column containing county names\n", "counties=gpd.read_file(kenya_Counties)\n", "counties.head(3)" ], "execution_count": 19, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", " | OBJECTID | \n", "COUNTY_NAM | \n", "CONST_CODE | \n", "CONSTITUEN | \n", "COUNTY_ASS | \n", "COUNTY_A_1 | \n", "REGIST_CEN | \n", "REGISTRATI | \n", "COUNTY_COD | \n", "Shape_Leng | \n", "Shape_Area | \n", "geometry | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "NAIROBI | \n", "288.0 | \n", "KAMUKUNJI | \n", "0.0 | \n", "None | \n", "0.0 | \n", "None | \n", "47.0 | \n", "0.164632 | \n", "0.000978 | \n", "MULTIPOLYGON (((36.87595 -1.26253, 36.87894 -1... | \n", "
1 | \n", "2 | \n", "NAIROBI | \n", "274.0 | \n", "WESTLANDS | \n", "0.0 | \n", "None | \n", "0.0 | \n", "None | \n", "47.0 | \n", "0.412418 | \n", "0.005909 | \n", "MULTIPOLYGON (((36.81926 -1.20643, 36.83463 -1... | \n", "
2 | \n", "3 | \n", "NAIROBI | \n", "275.0 | \n", "DAGORETTI NORTH | \n", "0.0 | \n", "None | \n", "0.0 | \n", "None | \n", "47.0 | \n", "0.232409 | \n", "0.002385 | \n", "MULTIPOLYGON (((36.79214 -1.26085, 36.79290 -1... | \n", "