diff --git a/Sentinal.ipynb b/Sentinal.ipynb new file mode 100644 index 0000000..43da07e --- /dev/null +++ b/Sentinal.ipynb @@ -0,0 +1,918 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Sentinal.ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyO2YiEsxxVI+8aR61XOcyA4", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dnhitYmUDvaK", + "colab_type": "text" + }, + "source": [ + "Install sentinalsat, geopandas and rasterio." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "USkzgZGED7iv", + "colab_type": "code", + "colab": {} + }, + "source": [ + "! pip install sentinelsat" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "N49y9erYn2_9", + "colab_type": "code", + "colab": {} + }, + "source": [ + "! pip install geopandas" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "whocVGN7oVsb", + "colab_type": "code", + "colab": {} + }, + "source": [ + "! pip install rasterio" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "B52BVeQxEBSG", + "colab_type": "text" + }, + "source": [ + "Various imports." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bmlINOZLnvRL", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from sentinelsat import SentinelAPI,read_geojson, geojson_to_wkt\n", + "import geopandas as gpd\n", + "import rasterio as rio\n", + "from rasterio.plot import show\n", + "from rasterio import mask\n", + "import numpy as np\n", + "import glob" + ], + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QK5U3-zQEGw2", + "colab_type": "text" + }, + "source": [ + "User name and Password for Sentinal." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7CtX1njrM4BP", + "colab_type": "code", + "colab": {} + }, + "source": [ + "user=" + ], + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5gQtK6LwM_5X", + "colab_type": "code", + "colab": {} + }, + "source": [ + "password=" + ], + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4eZfxuPFFE1", + "colab_type": "text" + }, + "source": [ + "Create sentinal query based on our plot coordinates, required time window and cloud cover filter. Sentinal will provide list of availalble tiles called products." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-NW_fVGINFEw", + "colab_type": "code", + "colab": {} + }, + "source": [ + "api = SentinelAPI(user, password, 'https://scihub.copernicus.eu/dhus')" + ], + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "qIp1qCYiS6MS", + "colab_type": "code", + "colab": {} + }, + "source": [ + "footprint = geojson_to_wkt(read_geojson('plot.geojson'))" + ], + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "A57H5HpiSpjE", + "colab_type": "code", + "colab": {} + }, + "source": [ + "products = api.query(footprint,\n", + " date = ('20200701', '20200831'),\n", + " platformname = 'Sentinel-2',\n", + " processinglevel = 'Level-2A',\n", + " cloudcoverpercentage = (0,100)\n", + " )" + ], + "execution_count": 10, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sI52YLYhIn6w", + "colab_type": "text" + }, + "source": [ + "load products as geopandas dataframe." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SY2AOtcGk3TZ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "products_gdf = api.to_geodataframe(products)\n", + "print(products_gdf)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jeUXr0rnGvko", + "colab_type": "text" + }, + "source": [ + "Obtain ID of most recent Sentinal dataset." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BGtEXB57tzel", + "colab_type": "code", + "colab": {} + }, + "source": [ + "uuid=products_gdf['uuid']" + ], + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VWH7wM66KKte", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#cloud=products_gdf['cloudcoverpercentage']\n", + "#print(cloud)" + ], + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s3fOjRq2zkJQ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "uuid1=uuid[0]\n", + "print(uuid1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ybHhA3esHXxo", + "colab_type": "text" + }, + "source": [ + "Download most recent Sentinal data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "drHGzgGcl0ot", + "colab_type": "code", + "colab": {} + }, + "source": [ + "api.download(uuid1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ei4RjgIwHfuO", + "colab_type": "text" + }, + "source": [ + "Obtain filename of downloaded Sentilnal zip file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "usAzSam4e6qX", + "colab_type": "code", + "colab": {} + }, + "source": [ + "for name in glob.glob('/content/*.zip'):\n", + " file=name\n", + "print(file)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7vnUqBLiJEwQ", + "colab_type": "text" + }, + "source": [ + "Unzip Sentinal file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3Bc-57nmgVAN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "! unzip '$file'" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s0xZr5tULSMG", + "colab_type": "text" + }, + "source": [ + "Obtain the name to the Sentinal data directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Me81pZsNLcOL", + "colab_type": "code", + "colab": {} + }, + "source": [ + "for name in glob.glob('/content/*.SAFE'):\n", + " file1=name\n", + "print(file1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dl6w8k-oM2I9", + "colab_type": "text" + }, + "source": [ + "Obtain the path of the Sentinal image data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2pVeY2_HMHwQ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "for name in glob.glob(file1+'/GRANULE/*'):\n", + " file2=name\n", + "print(file2)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ou5DFzPDRAeX", + "colab_type": "text" + }, + "source": [ + "Obtain the file names of B2(B),B3(G),B4(R) and B8(NIR) channel images." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PMBXmyxLXKQ-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "file3=[]" + ], + "execution_count": 18, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "DcKc6PN-Wa2l", + "colab_type": "code", + "colab": {} + }, + "source": [ + "for name in glob.glob(file2+'/IMG_DATA/R10m/*'):\n", + " file3.append(name)\n", + "file3.sort()\n", + "print(file3)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IvCARpmMRN_N", + "colab_type": "text" + }, + "source": [ + "Read the B2, B3, B4 and B8 channel data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5D1FtXQrZOBr", + "colab_type": "code", + "colab": {} + }, + "source": [ + "b4=rio.open(file3[3])" + ], + "execution_count": 20, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "M9DNLVXBZu6B", + "colab_type": "code", + "colab": {} + }, + "source": [ + "b3=rio.open(file3[2])" + ], + "execution_count": 21, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "1bP40TuCZ0Qy", + "colab_type": "code", + "colab": {} + }, + "source": [ + "b2=rio.open(file3[1])" + ], + "execution_count": 22, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "upgmmxvsaA4X", + "colab_type": "code", + "colab": {} + }, + "source": [ + "b8=rio.open(file3[4])" + ], + "execution_count": 23, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2l1_CPDbS6VA", + "colab_type": "text" + }, + "source": [ + "Create RGB tiff file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z7e0KRD1_AXs", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with rio.open('RGB.tiff','w',driver='Gtiff', width=b4.width, height=b4.height, \n", + " count=3,crs=b4.crs,transform=b4.transform, dtype=b4.dtypes[0]) as rgb:\n", + " rgb.write(b2.read(1),1) \n", + " rgb.write(b3.read(1),2) \n", + " rgb.write(b4.read(1),3) \n", + " rgb.close()" + ], + "execution_count": 24, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8AV2NGzpRj6R", + "colab_type": "text" + }, + "source": [ + "Show RGB.tiff for the complete Sentinal tile." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hYbnjtE6AZzR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "src = rio.open(\"RGB.tiff\")\n", + "show(src)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xy-SCMbYSP8H", + "colab_type": "text" + }, + "source": [ + "Read B4(red) and B8(NIR) data and calculate NDVI." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ni_S9zbzBsrV", + "colab_type": "code", + "colab": {} + }, + "source": [ + "red = b4.read()\n", + "nir = b8.read()" + ], + "execution_count": 26, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "nWhL6pLsDCe2", + "colab_type": "code", + "colab": {} + }, + "source": [ + "ndvi = (nir.astype(float)-red.astype(float))/(nir+red)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "u07weA0RDKrZ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "print(ndvi)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LbuwtZN9StCn", + "colab_type": "text" + }, + "source": [ + "Create NDVI tiff file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5rA6IYYPDg6-", + "colab_type": "code", + "colab": {} + }, + "source": [ + "meta = b4.meta\n", + "meta.update(driver='GTiff')\n", + "meta.update(dtype=rio.float32)\n", + "\n", + "with rio.open('NDVI.tiff', 'w', **meta) as dst:\n", + " dst.write(ndvi.astype(rio.float32))" + ], + "execution_count": 29, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "d7AXjmN2TPU8", + "colab_type": "text" + }, + "source": [ + "Show NDVI for complete Sentinal tile." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "n3E_iDkzEEAN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "src = rio.open(\"NDVI.tiff\")\n", + "show(src)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g5VvnCGkT3Td", + "colab_type": "text" + }, + "source": [ + "Extract the Coordinate Reference System(crs) of the Sentinal data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "m9WHYG90sffD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b6c938e3-ad1d-487f-db91-2423e68a794b" + }, + "source": [ + "crs=src.crs\n", + "print(crs)" + ], + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "text": [ + "EPSG:32643\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a6FD4gfZUbiu", + "colab_type": "text" + }, + "source": [ + "Convert plot coodinates to Sentinal crs." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ANk0VziC9Zfa", + "colab_type": "code", + "colab": {} + }, + "source": [ + "plot=gpd.read_file('plot.geojson')" + ], + "execution_count": 32, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "4S46bBPB_mqv", + "colab_type": "code", + "colab": {} + }, + "source": [ + "plot1=plot.to_crs({'init': crs})" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5-18qkPC_OHN", + "colab_type": "code", + "colab": {} + }, + "source": [ + "geo=plot1.geometry\n", + "print(geo)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vAdH8UHqU9uO", + "colab_type": "text" + }, + "source": [ + "Extract plot RGB from Sentinal data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "iihHPoqfAJ3r", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with rio.open(\"RGB.tiff\") as src:\n", + " out_image, out_transform = rio.mask.mask(src,geo,crop=True)\n", + " out_meta = src.meta.copy()\n", + " out_meta.update({\"driver\": \"GTiff\",\n", + " \"height\": out_image.shape[1],\n", + " \"width\": out_image.shape[2],\n", + " \"transform\": out_transform})" + ], + "execution_count": 35, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5ckSQTWeVOOA", + "colab_type": "text" + }, + "source": [ + "Create RGB tiff file for plot and show." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ta_DzlPnAoG2", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with rio.open(\"RGB_plot.tiff\", \"w\", **out_meta) as dest:\n", + " dest.write(out_image)" + ], + "execution_count": 36, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "kGGHyTfnAq3e", + "colab_type": "code", + "colab": {} + }, + "source": [ + "src = rio.open(\"RGB_plot.tiff\")\n", + "show(src)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "VZNpqTjOWVRD", + "colab_type": "text" + }, + "source": [ + "Extract plot NDVI." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "hLmeUB96BVxI", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with rio.open(\"NDVI.tiff\") as src:\n", + " out_image, out_transform = rio.mask.mask(src,geo,crop=True)\n", + " out_meta = src.meta.copy()\n", + " out_meta.update({\"driver\": \"GTiff\",\n", + " \"height\": out_image.shape[1],\n", + " \"width\": out_image.shape[2],\n", + " \"transform\": out_transform})" + ], + "execution_count": 38, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cJiKCJ9-WwFm", + "colab_type": "text" + }, + "source": [ + "Create NDVI tiff file for plot and show." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "f-QTnQXvBgzd", + "colab_type": "code", + "colab": {} + }, + "source": [ + "with rio.open(\"NDVI_plot.tiff\", \"w\", **out_meta) as dest:\n", + " dest.write(out_image)" + ], + "execution_count": 39, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "cTMS6k61Bqgo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "src = rio.open(\"NDVI_plot.tiff\")\n", + "show(src)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4RPnxg2TYLGY", + "colab_type": "text" + }, + "source": [ + "Read plot NDVI data, drop zero values and calculate mean NDVI." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9M1NB917D5ab", + "colab_type": "code", + "colab": {} + }, + "source": [ + "data=src.read(1)" + ], + "execution_count": 41, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "COUyIc-ZHgCj", + "colab_type": "code", + "colab": {} + }, + "source": [ + "data1=data[data != 0]" + ], + "execution_count": 42, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "CpVA4jFkKgly", + "colab_type": "code", + "colab": {} + }, + "source": [ + "Mean_NDVI_Plot=np.sum(data1)/data1.size\n", + "print(Mean_NDVI_Plot)" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file