Creating polygon based on depth data extracted from TIFF file

31 Views Asked by At

I am trying to extract and create polygons out of depth data from a TIFF file. I have a TIFF file of the depth model of Denmark, and I wish to create polygons that would resemble where sailing would not be possible (land and waters shallower than 2 meters).

I have extracted data points where the water depth is in the range 2-2.2 meters,

Extracted data points

but I am unsure of how to connect the dots such that I get distinct polygons resembling each of the landmasses/islands of the country. I parse the TIFF file pixels from the top left corner to the bottom right, which means that if I simply connect them all I get one large polygon with all the islands connected

The large polygon,

which is not the intention.

This is the code I use for extracting the points

import rasterio
import numpy as np

metadata = {
    'driver': 'GTiff',
    'dtype': 'float32',
    'nodata': 3.4028234663852886e+38,
    'width': 15798,
    'height': 8324,
    'count': 1,
    #'crs': CRS.from_epsg(3034),
    'transform': Affine(50.0, 0.0, 3602375.0, 0.0, -50.0, 3471675.0)
}

# Load the GeoTIFF file
tif_file_path = "ddm_50m.dybde.tiff"
with rasterio.open(tif_file_path,'r') as r:
    # Read the depth data as a NumPy array
    depth_data = r.read(1)
    # Define a threshold
    depth_threshold = 2

    # Create a mask for points with depth in range 2-2.2 meters    
    depth_mask = (depth_data > -depth_threshold*1.1) & (depth_data < -depth_threshold) & (depth_data < metadata['nodata'])

    # Get the coordinates of points with depth less than the threshold
    points_below_threshold = np.where(depth_mask)

    # Print the points to file
    with open('PointsBelowThreshold.txt', 'w') as file:
        for y, x in zip(*points_below_threshold):
            # Access depth value at this point
            depth_value = depth_data[y, x]
            file.write((str(y) + "," + str(x) + "," + str(depth_value)))
            file.write("\n")

If there exists a more clever way to determine this do let me know!

0

There are 0 best solutions below