openEO logo openEO logo openEO
  • About
    • About openEO
    • Project Steering Committee (PSC)
  • Services
  • Community
    • News
    • Meetings
    • Events
  • User Documentation
    • COOKBOOK

    • KEY CONCEPTS
    • Glossary
    • Backends
    • Datacube
    • Processes
    • Architecture
    • SETUP
    • DATA DISCOVERY

    • CUBE OPERATIONS
    • General Operations
    • Preprocessing
    • Spatial Operations
    • Temporal Operations
    • Spectral Operations
    • Cube Manipulations
    • User Defined Functions
    • Machine Learning
    • User Defined Processes
    • Execute openEO Jobs
    • EXPORT FORMATS
  • Developers
    • REST API Reference
    • Xarray/Dask Guide
    • Backend Development
  • Support
    • Contact
    • CDSE Forum
    • Notebook examples

Comparative study of before and after flash flood in Cologne using NDWI service available in EOplaza

In this notebook, we tried to perform a comparative study between the pre and post-image of Cologne during the 2021 flood. A simple technique to subtract pre- and post-images is done to determine the change in water content due to flooding in that region.

Reference: https://labo.obs-mip.fr/multitemp/the-ndwi-applied-to-the-recent-flooding-in-the-central-us/

# import necessary packages
import openeo
from openeo.api.process import Parameter
import json
from pathlib import Path
import matplotlib.pyplot as plt
import rasterio
import numpy as np

# connect with the backend
eoconn = openeo.connect("openeo.terrascope.be").authenticate_oidc()
Authenticated using refresh token.

Users can choose among different backend available here to connect to the backend. Regarding the authentication process, OpenID connect (oidc) is recommended, but it is not always straightforward to use.

# function to load geojson file
def read_json(path: Path) -> dict:
    with open(path) as input:
        field = json.load(input)
        input.close()
    return field

Since this is an already published service available service, they need not be concerned with selecting the backend. They can directly execute the process by providing time and area of interest.

aoi = {
"type": "FeatureCollection",
"name": "cologne_all",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "ID_0": 86, "ISO": "DEU", "NAME_0": "Germany", "ID_1": 10, "NAME_1": "Nordrhein-Westfalen", "ID_2": 29, "NAME_2": "Cologne", "ID_3": 289, "NAME_3": "Cologne Stadte", "TYPE_3": "Kreisfreie Stadte", "ENGTYPE_3": "Urban district", "VARNAME_3": "Koln" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 7.07279014587408, 51.013561248779297 ], [ 7.06728982925415, 51.009651184082031 ], [ 7.073749065399227, 51.002399444580135 ], [ 7.079888820648307, 50.998878479003963 ], [ 7.080210208892879, 50.995159149169979 ], [ 7.074379920959529, 50.994960784912166 ], [ 7.068879127502555, 50.991062164306641 ], [ 7.069509983062858, 50.983612060546932 ], [ 7.092838764190731, 50.984390258789119 ], [ 7.098990917205811, 50.980869293212947 ], [ 7.09995889663702, 50.969711303710994 ], [ 7.106439113616943, 50.962471008300895 ], [ 7.100930213928336, 50.958549499511719 ], [ 7.095099925994987, 50.958351135253906 ], [ 7.089589118957633, 50.954429626464901 ], [ 7.095749855041561, 50.950908660888729 ], [ 7.102229118347168, 50.943672180175781 ], [ 7.114559173584098, 50.936641693115348 ], [ 7.132079124450684, 50.937252044677678 ], [ 7.137589931488094, 50.941181182861385 ], [ 7.149271011352539, 50.94158935546875 ], [ 7.149599075317496, 50.937870025634766 ], [ 7.139239788055534, 50.922580718994141 ], [ 7.145401000976506, 50.919071197509822 ], [ 7.139890193939323, 50.915142059326172 ], [ 7.140879154205379, 50.903968811035099 ], [ 7.135369777679557, 50.900039672851619 ], [ 7.141540050506592, 50.896541595459041 ], [ 7.142879962921256, 50.881660461425724 ], [ 7.137351036071891, 50.877731323242244 ], [ 7.14319896697998, 50.87794113159174 ], [ 7.143528938293514, 50.874221801757756 ], [ 7.149710178375244, 50.870708465576172 ], [ 7.155559062957764, 50.870922088623047 ], [ 7.155889034271354, 50.867198944091911 ], [ 7.161739826202449, 50.867408752441406 ], [ 7.167930126190299, 50.863899230957145 ], [ 7.162069797515869, 50.863689422607422 ], [ 7.151039123535213, 50.855831146240234 ], [ 7.117269039154053, 50.839691162109318 ], [ 7.111761093139762, 50.835762023925838 ], [ 7.105909824371395, 50.835552215576286 ], [ 7.094879150390625, 50.827701568603572 ], [ 7.065659999847412, 50.826679229736328 ], [ 7.064010143280143, 50.845291137695312 ], [ 7.040648937225399, 50.844501495361442 ], [ 7.029290199279785, 50.840381622314453 ], [ 7.02410984039301, 50.832729339599553 ], [ 6.994900226593131, 50.831771850585994 ], [ 6.988730907440186, 50.835311889648381 ], [ 6.988409042358398, 50.839031219482365 ], [ 6.982901096344108, 50.835121154785099 ], [ 6.976739883422908, 50.838661193847713 ], [ 6.971220016479492, 50.834739685058537 ], [ 6.959869861602897, 50.83063888549799 ], [ 6.947229862213135, 50.841449737548942 ], [ 6.941400051117057, 50.841270446777344 ], [ 6.947878837585449, 50.833999633789062 ], [ 6.936210155487174, 50.833629608154411 ], [ 6.935557842254752, 50.841091156005973 ], [ 6.930049896240291, 50.837181091308707 ], [ 6.929399967193717, 50.844638824462947 ], [ 6.923570156097412, 50.844459533691349 ], [ 6.923248767852897, 50.848178863525334 ], [ 6.922268867492733, 50.859371185302791 ], [ 6.928119182586784, 50.859550476074219 ], [ 6.92778921127325, 50.863281250000057 ], [ 6.914858818054313, 50.877830505371207 ], [ 6.925550937652588, 50.889369964599723 ], [ 6.883140087127742, 50.906780242919922 ], [ 6.877630233764705, 50.902881622314567 ], [ 6.871789932250977, 50.902698516845817 ], [ 6.866270065307731, 50.898799896240291 ], [ 6.853960990905819, 50.905921936035156 ], [ 6.847489833831844, 50.913200378418026 ], [ 6.84653997421276, 50.924381256103572 ], [ 6.829020023346004, 50.923870086670036 ], [ 6.822847843170166, 50.927429199218693 ], [ 6.82221889495861, 50.934890747070312 ], [ 6.810529232025146, 50.934551239013786 ], [ 6.821899890899715, 50.938621520996151 ], [ 6.815421104431152, 50.94589996337902 ], [ 6.826489925384635, 50.953689575195369 ], [ 6.832328796386776, 50.953861236572266 ], [ 6.825850009918213, 50.961139678955078 ], [ 6.836907863617057, 50.968929290771484 ], [ 6.830749988556022, 50.972492218017692 ], [ 6.836597919464225, 50.972660064697322 ], [ 6.841807842254696, 50.980270385742131 ], [ 6.847340106964111, 50.984169006347713 ], [ 6.847030162811279, 50.987899780273381 ], [ 6.840549945831356, 50.995182037353629 ], [ 6.846078872680721, 50.999069213867244 ], [ 6.845449924468994, 51.006519317626953 ], [ 6.838980197906494, 51.013801574707145 ], [ 6.850040912628231, 51.021591186523494 ], [ 6.837719917297363, 51.028701782226676 ], [ 6.831249237060604, 51.035980224609375 ], [ 6.825398921966553, 51.035808563232536 ], [ 6.819859981536865, 51.031921386718864 ], [ 6.814010143280029, 51.031761169433537 ], [ 6.801990032196102, 51.035148620605582 ], [ 6.795498847961539, 51.042430877685604 ], [ 6.795178890228328, 51.046161651611271 ], [ 6.782839775085563, 51.053279876708984 ], [ 6.776340007781982, 51.060562133789006 ], [ 6.782208919525203, 51.060729980468864 ], [ 6.793600082397575, 51.064781188964844 ], [ 6.799459934234562, 51.064941406250114 ], [ 6.805951118469352, 51.057659149169922 ], [ 6.806261062622184, 51.053932189941406 ], [ 6.812748908996582, 51.046649932861385 ], [ 6.813058853149414, 51.042930603027401 ], [ 6.818919181823844, 51.043090820312557 ], [ 6.818290233612117, 51.050540924072379 ], [ 6.829988956451473, 51.050868988037166 ], [ 6.841380119323844, 51.054931640625057 ], [ 6.841061115264893, 51.058650970459041 ], [ 6.828111171722526, 51.073211669921875 ], [ 6.845660209655762, 51.073711395263729 ], [ 6.856739044189453, 51.081489562988395 ], [ 6.857358932495117, 51.074039459228516 ], [ 6.863208770751896, 51.074211120605526 ], [ 6.863519191742, 51.070491790771541 ], [ 6.869369983673096, 51.070659637451229 ], [ 6.887219905853271, 51.067451477050781 ], [ 6.899208068847656, 51.064071655273494 ], [ 6.905360221862793, 51.060520172119141 ], [ 6.923789024353027, 51.049869537353572 ], [ 6.960300922393856, 51.032321929931697 ], [ 6.978700160980225, 51.021690368652344 ], [ 6.97930908203125, 51.014251708984375 ], [ 6.973800182342643, 51.010349273681641 ], [ 6.979928970336914, 51.006809234619197 ], [ 6.997399806976375, 51.007369995117188 ], [ 6.997089862823543, 51.011081695556641 ], [ 7.014249801635799, 51.015361785888729 ], [ 7.020379066467342, 51.011829376220646 ], [ 7.02620887756359, 51.012031555175838 ], [ 7.049180030822811, 51.016498565673885 ], [ 7.06696891784668, 51.013359069824276 ], [ 7.07279014587408, 51.013561248779297 ] ] ] ] } }
]
}
before_date = ["2021-05-12","2021-05-12"]
after_date = ["2021-06-18", "2021-06-18"]
# aoi = read_json("cologne_all.geojson")

# Create a processing graph from the NDWI process using an active openEO connection
before_ndwi = eoconn.datacube_from_process("NDWI", namespace="vito", date=before_date
                                        ,polygon=aoi)
# Create a processing graph from the NDWI process using an active openEO connection
after_ndwi = eoconn.datacube_from_process("NDWI", namespace="vito", date=after_date
                                        ,polygon=aoi)

As you can see, a user warning for missing data pops up, which might not be an issue in typical cases. However, we wish to evaluate our results further in our process; thus, defining metadata is needed.

Not all the available services require updating metadata. If a service lacks metadata, performing further computation on the output of the service could be an issue. The user can update the metadata based on its status in such cases.

# check available information is available in metadata or not
before_ndwi.metadata
# updating our metadata
from openeo.metadata import CollectionMetadata

before_ndwi.metadata = CollectionMetadata({"cube:dimensions":{"t":{"type":"temporal"}}})
after_ndwi.metadata = CollectionMetadata({"cube:dimensions":{"t":{"type":"temporal"}}})

Once the metadata is updated, you can perform further operations like subtraction, sum, etc.

Since we now have details on temporal dimension, we can reduce dimension. As we loaded our collection for specific time intervals, it can include multiple time dimensions. Thus, reduce_dimension applies a reducer to a data cube dimension by collapsing all the pixel values along the time dimension into an output value computed by the reducer.

It is then followed by subtracting our before datacube from the later.

# compute the change between pre and post image
merging_cubes = after_ndwi.merge_cubes(-before_ndwi)
differenced_cube = merging_cubes.reduce_dimension(dimension="t",reducer='sum')

Once the process is completed, you can also save it as your process using save_user_defined_process that can later be used for a similar task. Otherwise, you can download the result either by direct download (in case of the small spatial extent with little processing) or by creating a batch job.

# download your result either using synchronous method or batch
# synchronous download
differenced_cube.download("changed_ndwi.tiff")
Back to top