{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Datafusion example\n", "\n", "How to request Sentinel-1 and Sentinel-2 data over a given time period for all coinciding acquisitions." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import json\n", "import matplotlib.pyplot as plt\n", "from sentinelhub import (SHConfig, BBox, CRS, SentinelHubRequest, DataCollection, MimeType)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set up Sentinel Hub services credentials" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Set credentials\n", "client_id = \"\"\n", "client_secret = \"\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Set up credentials for use with Sentinel Hub python package\n", "config = SHConfig()\n", "\n", "if client_id and client_secret:\n", " config.sh_client_id = client_id\n", " config.sh_client_secret = client_secret\n", " \n", "if config.sh_client_id == '' or config.sh_client_secret == '':\n", " print(\"Warning! To use Sentinel Hub services, please provide the credentials (client ID and client secret).\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set input parameters\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Set up AOI\n", "rome = [12.44693, 41.870072, 12.541001, 41.917096]\n", "rome_bbox = BBox(bbox=rome, crs=CRS.WGS84) " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# Time interval\n", "time_interval = ('2020-10-27', '2021-01-14') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run Sentinel Hub datafusion request on the common dates only" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Build Evalscript\n", "\n", "In the Evalscript, the dates for which the two satellites (S1 and S2) have acquisitions are filtered in the `preProcessScenes`. \n", "\n", "One output is created for each band. The output will contain data for each date that has a Sentinel-2 and a Sentinel-1 acquisition.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "evalscript = \"\"\"\n", "//VERSION=3\n", "\n", "function setup() {\n", " return {\n", " input: [{\n", " datasource: \"S1GRD\",\n", " bands: [\"VV\",\"VH\"]\n", " },\n", " {\n", " datasource: \"S2L2A\",\n", " bands: [\"B03\"]\n", " }\n", " ],\n", " // For each band create an output\n", " output: [\n", " {\n", " id: \"VV\",\n", " bands: 1,\n", " sampleType: SampleType.FLOAT32\n", " },\n", " {\n", " id: \"VH\",\n", " bands: 1,\n", " sampleType: SampleType.FLOAT32\n", " },\n", " {\n", " id: \"G\",\n", " bands: 1,\n", " sampleType: SampleType.FLOAT32\n", " },\n", " ],\n", " mosaicking: \"ORBIT\"\n", " };\n", "}\n", "\n", "// Filter for common dates\n", "function preProcessScenes (collections) {\n", " var s2dates = collections.S2L2A.scenes.orbits.map(function (orbit) {\n", " return orbit.dateFrom.split(\"T\")[0];\n", " });\n", " var s1dates = collections.S1GRD.scenes.orbits.map(function (orbit) {\n", " return orbit.dateFrom.split(\"T\")[0];\n", " });\n", " \n", " var allowedDates = s2dates.filter(value => s1dates.includes(value));\n", " \n", " collections.S2L2A.scenes.orbits = collections.S2L2A.scenes.orbits.filter(function (orbit) {\n", " var orbitDateFrom = orbit.dateFrom.split(\"T\")[0];\n", " return allowedDates.includes(orbitDateFrom);\n", " });\n", " collections.S1GRD.scenes.orbits = collections.S1GRD.scenes.orbits.filter(function (orbit) {\n", " var orbitDateFrom = orbit.dateFrom.split(\"T\")[0];\n", " return allowedDates.includes(orbitDateFrom);\n", " });\n", " return collections;\n", "}\n", "\n", "\n", "function updateOutput(outputs, collections) {\n", " n_bands = collections.S1GRD.scenes.orbits.length;\n", " outputs.VV.bands = n_bands;\n", " outputs.VH.bands = n_bands;\n", " outputs.G.bands = n_bands;\n", "}\n", "\n", "function evaluatePixel(samples) {\n", " // Get Sentinel-1 and Sentinel-2 data\n", " var s1_data = samples.S1GRD;\n", " var s2_data = samples.S2L2A;\n", " \n", " // Initialise bands to be written\n", " var vv_band = [];\n", " var vh_band = [];\n", " var b3_band = [];\n", "\n", " // Add Sentinel-1 data to the band array\n", " for (i=0; i" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "fig, ax = plt.subplots(ncols=3, figsize=(21,7))\n", "\n", "# Plot first date of the three bands\n", "ax[0].imshow(all_common_data[0][\"G.tif\"][:,:,0]*2.5, vmin=0, vmax=1, cmap=\"Greys_r\")\n", "ax[1].imshow(all_common_data[0][\"VV.tif\"][:,:,0], vmin=0, vmax=1, cmap=\"Greys_r\")\n", "ax[2].imshow(all_common_data[0][\"VH.tif\"][:,:,0], vmin=0, vmax=1, cmap=\"Greys_r\")\n", "\n", "ax[0].set_title(\"S2, B03\")\n", "ax[1].set_title(\"S1, VV\")\n", "ax[2].set_title(\"S1, VH\")\n", "\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }