{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Geopandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[](https://colab.research.google.com/github/davemlz/spyndex/blob/main/docs/tutorials/geopandas.ipynb)\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome to the next level: Level 5 - `spyndex + geopandas`!\n", "\n", "Remember to install `spyndex`!\n", "\n", "> And also remember to install `geopandas`! I recommend you to install it using conda!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U spyndex" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's start!\n", "\n", "First, import `spyndex` and `geopandas`:" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "import spyndex\n", "import geopandas as gpd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `geopandas.GeoDataFrame`\n", "\n", "In `geopandas`, each column is a `pandas.Series`. The exception is the `geometry` column, which is a `geopandas.GeoSeries`.\n", "\n", "Here we are going to compute spectral indices using `pandas.Series` data types, but adding them to a `geopandas.GeoDataFrame`, making them possible to map!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's create a random sample of reflectances from Sentinel-2. For this, we are going to use `Google Earth Engine`. Let's install some useful packages:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install eemont geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, let's import them and initialize `Earth Engine`." ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "import ee, eemont, geemap\n", "\n", "Map = geemap.Map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's use the first image in the Sentinel-2 SR collection and sample 1000 random points from it.\n", "\n", "> With the `eemont` package we can mask clouds and scale and offset the image!" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "poi = ee.ImageCollection(\"COPERNICUS/S2_SR\") \\\n", " .first() \\\n", " .maskClouds() \\\n", " .scaleAndOffset() \\\n", " .sample(numPixels = 1000,geometries = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's use `geemap` to convert the samples to a `geopandas.GeoDataFrame`:" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "gdf = geemap.ee_to_geopandas(poi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see that the `geopandas.GeoDataFrame` has the `geometry` column and the remaining columns are the samples from all bands in the image!" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | geometry | \n", "AOT | \n", "B1 | \n", "B11 | \n", "B12 | \n", "B2 | \n", "B3 | \n", "B4 | \n", "B5 | \n", "B6 | \n", "... | \n", "B8A | \n", "B9 | \n", "QA10 | \n", "QA20 | \n", "QA60 | \n", "SCL | \n", "TCI_B | \n", "TCI_G | \n", "TCI_R | \n", "WVP | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "POINT (27.29076 26.40036) | \n", "0.197 | \n", "0.1070 | \n", "0.6306 | \n", "0.5504 | \n", "0.1367 | \n", "0.2429 | \n", "0.3712 | \n", "0.4120 | \n", "0.4322 | \n", "... | \n", "0.4331 | \n", "0.4570 | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "139 | \n", "248 | \n", "255 | \n", "0.695 | \n", "
| 1 | \n", "POINT (28.06818 26.58967) | \n", "0.198 | \n", "0.1697 | \n", "0.5636 | \n", "0.4815 | \n", "0.2111 | \n", "0.3039 | \n", "0.3790 | \n", "0.4122 | \n", "0.4194 | \n", "... | \n", "0.4274 | \n", "0.4324 | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "215 | \n", "255 | \n", "255 | \n", "0.815 | \n", "
| 2 | \n", "POINT (27.87679 26.33568) | \n", "0.193 | \n", "0.1670 | \n", "0.6501 | \n", "0.5622 | \n", "0.2118 | \n", "0.3114 | \n", "0.4240 | \n", "0.4666 | \n", "0.4760 | \n", "... | \n", "0.4990 | \n", "0.5049 | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "216 | \n", "255 | \n", "255 | \n", "0.644 | \n", "
| 3 | \n", "POINT (27.04346 26.21331) | \n", "0.195 | \n", "0.1363 | \n", "0.7335 | \n", "0.6691 | \n", "0.1859 | \n", "0.3268 | \n", "0.4842 | \n", "0.5332 | \n", "0.5514 | \n", "... | \n", "0.5692 | \n", "0.5823 | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "189 | \n", "255 | \n", "255 | \n", "0.624 | \n", "
| 4 | \n", "POINT (27.53605 26.84894) | \n", "0.194 | \n", "0.2098 | \n", "0.6491 | \n", "0.5494 | \n", "0.2606 | \n", "0.3834 | \n", "0.5202 | \n", "0.5680 | \n", "0.5758 | \n", "... | \n", "0.5684 | \n", "0.5798 | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "255 | \n", "255 | \n", "255 | \n", "0.797 | \n", "
5 rows × 22 columns
\n", "| \n", " | geometry | \n", "AOT | \n", "B1 | \n", "B11 | \n", "B12 | \n", "B2 | \n", "B3 | \n", "B4 | \n", "B5 | \n", "B6 | \n", "... | \n", "QA10 | \n", "QA20 | \n", "QA60 | \n", "SCL | \n", "TCI_B | \n", "TCI_G | \n", "TCI_R | \n", "WVP | \n", "IRECI | \n", "NDVI | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "POINT (27.29076 26.40036) | \n", "0.197 | \n", "0.1070 | \n", "0.6306 | \n", "0.5504 | \n", "0.1367 | \n", "0.2429 | \n", "0.3712 | \n", "0.4120 | \n", "0.4322 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "139 | \n", "248 | \n", "255 | \n", "0.695 | \n", "0.073747 | \n", "0.097496 | \n", "
| 1 | \n", "POINT (28.06818 26.58967) | \n", "0.198 | \n", "0.1697 | \n", "0.5636 | \n", "0.4815 | \n", "0.2111 | \n", "0.3039 | \n", "0.3790 | \n", "0.4122 | \n", "0.4194 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "215 | \n", "255 | \n", "255 | \n", "0.815 | \n", "0.045786 | \n", "0.070281 | \n", "
| 2 | \n", "POINT (27.87679 26.33568) | \n", "0.193 | \n", "0.1670 | \n", "0.6501 | \n", "0.5622 | \n", "0.2118 | \n", "0.3114 | \n", "0.4240 | \n", "0.4666 | \n", "0.4760 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "216 | \n", "255 | \n", "255 | \n", "0.644 | \n", "0.066105 | \n", "0.091688 | \n", "
| 3 | \n", "POINT (27.04346 26.21331) | \n", "0.195 | \n", "0.1363 | \n", "0.7335 | \n", "0.6691 | \n", "0.1859 | \n", "0.3268 | \n", "0.4842 | \n", "0.5332 | \n", "0.5514 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "189 | \n", "255 | \n", "255 | \n", "0.624 | \n", "0.085109 | \n", "0.097904 | \n", "
| 4 | \n", "POINT (27.53605 26.84894) | \n", "0.194 | \n", "0.2098 | \n", "0.6491 | \n", "0.5494 | \n", "0.2606 | \n", "0.3834 | \n", "0.5202 | \n", "0.5680 | \n", "0.5758 | \n", "... | \n", "0 | \n", "0 | \n", "0 | \n", "5 | \n", "255 | \n", "255 | \n", "255 | \n", "0.797 | \n", "0.061128 | \n", "0.060926 | \n", "
5 rows × 24 columns
\n", "