根据从 TIFF 文件提取的深度数据创建多边形

问题描述 投票:0回答:1

我正在尝试从 TIFF 文件中提取并创建深度数据之外的多边形。我有丹麦深度模型的 TIFF 文件,我希望创建类似于不可能航行的多边形(陆地和水域浅于 2 米)。

我提取了水深在2-2.2米范围内的数据点,

Extracted data points

但我不确定如何连接这些点,以便获得类似于该国每个陆地/岛屿的不同多边形。我从左上角到右下角解析 TIFF 文件像素,这意味着如果我简单地将它们全部连接起来,我会得到一个大多边形,其中所有岛屿都已连接

The large polygon,

这不是本意。

这是我用来提取点的代码

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")

如果存在更聪明的方法来确定这一点,请告诉我!

python geometry geospatial tiff geotiff
1个回答
0
投票

您考虑过使用GDAL吗? 我将使用 gdal_calc 提取 tiff 中的区域

gdal_calc -A input.tif --outfile=output.tif --type=Byte --calc="1*(A<2)" --NoDataValue=0

然后我将使用 gdal_polygonize 创建多边形。

gdal_polygonize output.tif -f "ESRI Shapefile" "extent.shp"
© www.soinside.com 2019 - 2024. All rights reserved.