File size: 11,431 Bytes
d033501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""Functions to derive flood extent using Google Earth Engine."""
import time

import ee


def _check_task_completed(task_id, verbose=False):
    """
    Return True if a task export completes successfully, else returns false.

    Inputs:
        task_id (str): Google Earth Engine task id

    Returns:
        boolean

    """
    status = ee.data.getTaskStatus(task_id)[0]
    if status["state"] in (
        ee.batch.Task.State.CANCELLED,
        ee.batch.Task.State.FAILED,
    ):
        if "error_message" in status:
            if verbose:
                print(status["error_message"])
        return True
    elif status["state"] == ee.batch.Task.State.COMPLETED:
        return True
    return False


def wait_for_tasks(task_ids, timeout=3600, verbose=False):
    """
    Wait for tasks to complete, fail, or timeout.

    Wait for all active tasks if task_ids is not provided.
    Note: Tasks will not be canceled after timeout, and
    may continue to run.
    Inputs:
        task_ids (list):
        timeout (int):

    Returns:
        None
    """
    start = time.time()
    elapsed = 0
    while elapsed < timeout or timeout == 0:
        elapsed = time.time() - start
        finished = [_check_task_completed(task) for task in task_ids]
        if all(finished):
            if verbose:
                print(f"Tasks {task_ids} completed after {elapsed}s")
            return True
        time.sleep(5)
    if verbose:
        print(
            f"Stopped waiting for {len(task_ids)} tasks \
            after {timeout} seconds"
        )
    return False


def export_flood_data(
    flooded_area_vector,
    flooded_area_raster,
    image_before_flood,
    image_after_flood,
    region,
    filename="flood_extents",
    verbose=False,
):
    """
    Export the results of derive_flood_extents function to Google Drive.

    Inputs:
        flooded_area_vector (ee.FeatureCollection): Detected flood extents as
            vector geometries.
        flooded_area_raster (ee.Image): Detected flood extents as a binary
            raster.
        image_before_flood (ee.Image): The 'before' Sentinel-1 image.
        image_after_flood (ee.Image): The 'after' Sentinel-1 image containing
            view of the flood waters.
        region (ee.Geometry.Polygon): Geographic extent of analysis area.
        filename (str): Desired filename prefix for exported files

    Returns:
        None
    """
    if verbose:
        print(
            "Exporting detected flood extents to your Google Drive. \
            Please wait..."
        )
    s1_before_task = ee.batch.Export.image.toDrive(
        image=image_before_flood,
        description="export_before_s1_scene",
        scale=30,
        region=region,
        fileNamePrefix=filename + "_s1_before",
        crs="EPSG:4326",
        fileFormat="GeoTIFF",
    )

    s1_after_task = ee.batch.Export.image.toDrive(
        image=image_after_flood,
        description="export_flooded_s1_scene",
        scale=30,
        region=region,
        fileNamePrefix=filename + "_s1_after",
        crs="EPSG:4326",
        fileFormat="GeoTIFF",
    )

    raster_task = ee.batch.Export.image.toDrive(
        image=flooded_area_raster,
        description="export_flood_extents_raster",
        scale=30,
        region=region,
        fileNamePrefix=filename + "_raster",
        crs="EPSG:4326",
        fileFormat="GeoTIFF",
    )

    vector_task = ee.batch.Export.table.toDrive(
        collection=flooded_area_vector,
        description="export_flood_extents_polygons",
        fileFormat="shp",
        fileNamePrefix=filename + "_polygons",
    )

    s1_before_task.start()
    s1_after_task.start()
    raster_task.start()
    vector_task.start()

    if verbose:
        print("Exporting before Sentinel-1 scene: Task id ", s1_before_task.id)
        print("Exporting flooded Sentinel-1 scene: Task id ", s1_after_task.id)
        print("Exporting flood extent geotiff: Task id ", raster_task.id)
        print("Exporting flood extent shapefile:  Task id ", vector_task.id)

    wait_for_tasks(
        [s1_before_task.id, s1_after_task.id, raster_task.id, vector_task.id]
    )


def retrieve_image_collection(
    search_region,
    start_date,
    end_date,
    polarization="VH",
    pass_direction="Ascending",
):
    """
    Retrieve Sentinel-1 immage collection from Google Earth Engine.

    Inputs:
        search_region (ee.Geometry.Polygon): Geographic extent of image search.
        start_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        end_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        polarization (str): Synthetic aperture radar polarization mode, e.g.,
            'VH' or 'VV'. VH is mostly is the preferred polarization for
            flood mapping.
        pass_direction (str): Synthetic aperture radar pass direction, either
            'Ascending' or 'Descending'.

    Returns:
        collection (ee.ImageCollection): Sentinel-1 images matching the search
        criteria.
    """
    collection = (
        ee.ImageCollection("COPERNICUS/S1_GRD")
        .filter(ee.Filter.eq("instrumentMode", "IW"))
        .filter(
            ee.Filter.listContains(
                "transmitterReceiverPolarisation", polarization
            )
        )
        .filter(ee.Filter.eq("orbitProperties_pass", pass_direction.upper()))
        .filter(ee.Filter.eq("resolution_meters", 10))
        .filterDate(start_date, end_date)
        .filterBounds(search_region)
        .select(polarization)
    )

    return collection


def smooth(image, smoothing_radius=50):
    """
    Reduce the radar speckle by smoothing.

    Inputs:
        image (ee.Image): Input image.
        smoothing_radius (int): The radius of the kernel to use for focal mean
            smoothing.

    Returns:
        smoothed_image (ee.Image): The resulting image after smoothing is
            applied.
    """
    smoothed_image = image.focal_mean(
        radius=smoothing_radius, kernelType="circle", units="meters"
    )

    return smoothed_image


def mask_permanent_water(image):
    """
    Query the JRC Global Surface Water Mapping Layers, v1.3.

    The goal is to determine where perennial water bodies (water > 10
    months/yr), and mask these areas.
    Inputs:
        image (ee.Image): Input image.

    Returns:
        masked_image (ee.Image): The resulting image after surface water
        masking is applied.
    """
    surface_water = ee.Image("JRC/GSW1_4/GlobalSurfaceWater").select(
        "seasonality"
    )
    surface_water_mask = surface_water.gte(10).updateMask(
        surface_water.gte(10)
    )

    # Flooded layer where perennial water bodies(water > 10 mo / yr) is
    # assigned a 0 value
    where_surface_water = image.where(surface_water_mask, 0)

    masked_image = image.updateMask(where_surface_water)

    return masked_image


def reduce_noise(image):
    """
    Reduce noise in the image.

    Compute connectivity of pixels to eliminate those connected to 8 or fewer
    neighbours.
    Inputs:
        image (ee.Image): A binary image.

    Returns:
        reduced_noise_image (ee.Image): The resulting image after noise
            reduction is applied.
    """
    connections = image.connectedPixelCount()
    reduced_noise_image = image.updateMask(connections.gte(8))

    return reduced_noise_image


def mask_slopes(image):
    """
    Mask out areas with more than 5 % slope with a Digital Elevation Model.

    Inputs:
        image (ee.Image): Input image.
    Returns:
         slopes_masked (ee.Image): The resulting image after slope masking is
            applied.
    """
    dem = ee.Image("WWF/HydroSHEDS/03VFDEM")
    terrain = ee.Algorithms.Terrain(dem)
    slope = terrain.select("slope")
    slopes_masked = image.updateMask(slope.lt(5))

    return slopes_masked


def derive_flood_extents(
    aoi,
    before_start_date,
    before_end_date,
    after_start_date,
    after_end_date,
    difference_threshold=1.25,
    polarization="VH",
    pass_direction="Ascending",
    export=False,
    export_filename="flood_extents",
):
    """
    Set start and end dates of a period BEFORE and AFTER a flood.

    These periods need to be long enough for Sentinel-1 to acquire an image.

    Inputs:
        aoi (ee.Geometry.Polygon): Geographic extent of analysis area.
        before_start_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        before_end_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        after_start_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        after_end_date (str): Date in format yyyy-mm-dd, e.g., '2020-10-01'.
        difference_threshold (float): Threshold to be applied on the
            differenced image (after flood - before flood). It has been chosen
            by trial and error. In case your flood extent result shows many
            false-positive or negative signals, consider changing it.
        export (bool): Flag to export derived flood extents to Google Drive
        export_filename (str): Desired filename prefix for exported files. Only
            used if export=True.

    Returns:
        flood_vectors (ee.FeatureCollection): Detected flood extents as vector
            geometries.
        flood_rasters (ee.Image): Detected flood extents as a binary raster.
        before_filtered (ee.Image): The 'before' Sentinel-1 image.
        after_filtered (ee.Image): The 'after' Sentinel-1 image containing view
            of the flood waters.
    """
    before_flood_img_col = retrieve_image_collection(
        search_region=aoi,
        start_date=before_start_date,
        end_date=before_end_date,
        polarization=polarization,
        pass_direction=pass_direction,
    )
    after_flood_img_col = retrieve_image_collection(
        search_region=aoi,
        start_date=after_start_date,
        end_date=after_end_date,
        polarization=polarization,
        pass_direction=pass_direction,
    )

    # Create a mosaic of selected tiles and clip to study area
    before_mosaic = before_flood_img_col.mosaic().clip(aoi)
    after_mosaic = after_flood_img_col.mosaic().clip(aoi)

    before_filtered = smooth(before_mosaic)
    after_filtered = smooth(after_mosaic)

    # Calculate the difference between the before and after images
    difference = after_filtered.divide(before_filtered)

    # Apply the predefined difference - threshold and create the flood extent
    # mask
    difference_binary = difference.gt(difference_threshold)
    difference_binary_masked = mask_permanent_water(difference_binary)
    difference_binary_masked_reduced_noise = reduce_noise(
        difference_binary_masked
    )
    flood_rasters = mask_slopes(difference_binary_masked_reduced_noise)

    # Export the extent of detected flood in vector format
    flood_vectors = flood_rasters.reduceToVectors(
        scale=10,
        geometryType="polygon",
        geometry=aoi,
        eightConnected=False,
        bestEffort=True,
        tileScale=2,
    )

    if export:
        export_flood_data(
            flooded_area_vector=flood_vectors,
            flooded_area_raster=flood_rasters,
            image_before_flood=before_filtered,
            image_after_flood=after_filtered,
            region=aoi,
            filename=export_filename,
        )

    return flood_vectors, flood_rasters, before_filtered, after_filtered