{ "cells": [ { "attachments": {}, "cell_type": "markdown", "id": "5b4f2e85", "metadata": { "id": "5b4f2e85" }, "source": [ "--------\n", "\n", "# Clustering to find homogenous areas\n", "\n", "--------\n", "\n", "**Short description**\n", "\n", "This notebook performs clustering analysis on Sentinel-2 satellite data, utilizing the B02, B03 and B04 bands to identify and group areas with similar spectral characteristics for further analysis.\n", "\n", "In this notebook, you will search for, select, and obtain Sentinel-2 data for one day over a neighborhood in Barcelona, Spain. The selected data will be cloud-free to ensure accurate analysis of the study area. Specific bands, such as the B02, B03 and B04 bands will be calculated and obtained over the region of interest. A clustering analysis will be performed on these bands to group areas with similar spectral characteristics, enabling a deeper understanding of the landscape patterns. This example demonstrates the application of clustering techniques on Sentinel-2 data to identify and visualize distinct land cover types.\n", "\n", "--------" ] }, { "attachments": {}, "cell_type": "markdown", "id": "050a2fc5", "metadata": { "id": "050a2fc5" }, "source": [ "### 1 - Import spacesense object(s) and other dependencies" ] }, { "cell_type": "code", "execution_count": 1, "id": "ad7ca797", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ad7ca797", "outputId": "9a66ec72-3e06-4a89-e724-49ecc2c016e0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your api key : ··········\n" ] } ], "source": [ "from spacesense import Client, geoutils\n", "import datetime\n", "import os\n", "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import json\n", "from skimage import exposure\n", "\n", "if \"SS_API_KEY\" not in os.environ:\n", " from getpass import getpass\n", " api_key = getpass('Enter your api key : ')\n", " os.environ[\"SS_API_KEY\"] = api_key" ] }, { "attachments": {}, "cell_type": "markdown", "id": "836b0384", "metadata": { "id": "836b0384" }, "source": [ "### 2 - Define AOI and output options" ] }, { "cell_type": "code", "execution_count": 2, "id": "4T3CqExepZMR", "metadata": { "id": "4T3CqExepZMR" }, "outputs": [], "source": [ "# A neighborhood of Barcelona\n", "aoi = {\n", " \"type\": \"FeatureCollection\",\n", " \"features\": [\n", " {\n", " \"type\": \"Feature\",\n", " \"properties\": {},\n", " \"geometry\": {\n", " \"coordinates\": [\n", " [\n", " [\n", " 2.1719121924506055,\n", " 41.39760043017927\n", " ],\n", " [\n", " 2.1647389059867805,\n", " 41.39223018500084\n", " ],\n", " [\n", " 2.1682818096665244,\n", " 41.389200339725676\n", " ],\n", " [\n", " 2.175746693142031,\n", " 41.394800520638\n", " ],\n", " [\n", " 2.1719121924506055,\n", " 41.39760043017927\n", " ]\n", " ]\n", " ],\n", " \"type\": \"Polygon\"\n", " }\n", " }\n", " ]\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "c05d9b03", "metadata": { "id": "c05d9b03" }, "outputs": [], "source": [ "# Define the TOI\n", "start_date = \"2021-06-16\"\n", "end_date = \"2021-06-16\"" ] }, { "cell_type": "code", "execution_count": 4, "id": "05b56506", "metadata": { "id": "05b56506" }, "outputs": [], "source": [ "client = Client(id=\"cluster_zones\")" ] }, { "attachments": {}, "cell_type": "markdown", "id": "9b19c9d4", "metadata": { "id": "9b19c9d4" }, "source": [ "### 3 - Search S2" ] }, { "cell_type": "code", "execution_count": 5, "id": "2d70f6c8", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 179 }, "id": "2d70f6c8", "outputId": "3f0754af-a0bc-47f9-e636-ba83c2dafd6b" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "WARNING:spacesense.core:start_date and end_date are the same, adding 1 day to end_date\n" ] }, { "data": { "text/html": [ "\n", "
\n", " | id | \n", "date | \n", "tile | \n", "valid_pixel_percentage | \n", "platform | \n", "relative_orbit_number | \n", "product_id | \n", "datetime | \n", "swath_coverage_percentage | \n", "no_data | \n", "cloud_shadows | \n", "vegetation | \n", "not_vegetated | \n", "water | \n", "cloud_medium_probability | \n", "cloud_high_probability | \n", "thin_cirrus | \n", "snow | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "S2B_31TDF_20210616_0_L2A | \n", "2021-06-16 | \n", "31TDF | \n", "99.91 | \n", "sentinel-2b | \n", "008 | \n", "S2B_MSIL2A_20210616T103629_N0300_R008_T31TDF_2... | \n", "2021-06-16T10:49:42Z | \n", "100.0 | \n", "0.0 | \n", "0.0 | \n", "1.83 | \n", "98.08 | \n", "0.0 | \n", "0.0 | \n", "0.09 | \n", "0.0 | \n", "0.0 | \n", "
<xarray.Dataset>\n", "Dimensions: (time: 1, y: 94, x: 93)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-06-16\n", " * y (y) float32 41.4 41.4 41.4 41.4 41.4 ... 41.39 41.39 41.39 41.39\n", " * x (x) float32 2.165 2.165 2.165 2.165 ... 2.175 2.175 2.176 2.176\n", "Data variables:\n", " S2_B02 (time, y, x) float32 ...\n", " S2_B03 (time, y, x) float32 ...\n", " S2_B04 (time, y, x) float32 ...\n", "Attributes:\n", " transform: [ 1.18459751e-04 0.00000000e+00 2.16473355e+00 0.000...\n", " crs: +init=epsg:4326\n", " res: [1.18459751e-04 9.09349018e-05]\n", " descriptions: ['B02', 'B03', 'B04']\n", " AREA_OR_POINT: Area\n", " _FillValue: nan\n", " s2_data_lineage: {"Data origin": "S3 bucket (ARN=arn:aws:s3:::sentinel-c...\n", " ulx, uly: [ 2.16473355 41.39765899]