设置 numpy.array 和几何提取的空间变换

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

很简单,我试图将具有空间属性的 tif 文件读入脚本,提取其空间属性,以数组格式对栅格进行分析,重新应用空间属性,最后将聚类像素转换为形状。

问题是输出的 shapefile 被绘制在地球上一个完全不同的地方,而不是它应该在的地方。

到目前为止基本上是这样工作的:

这部分读取栅格并提取它的

profile

## import image with spatial attributes using rasterio
spatial_image = rio.open(intermediate_path)
spatial_profile = spatial_image.profile
input_crs = spatial_profile['crs']

# read the band of spatial image to a numpy array for processing
image = spatial_image.read(1)

然后对图像进行一系列分析。

空间轮廓的重新应用

with rio.Env():
    #spatial_profile = spatial_image.profile # get profile of spatial image
    spatial_profile.update(dtype=rio.uint16, count=1, nodata=None) # update profile. count is number of bands
    with rio.open(output_image, "w", **spatial_profile) as dst: # create virtual file with nnir_profile
        dst.write(filtered_labels.astype(rio.uint16), 1) # write data to file unint 16

到目前为止,它似乎工作正常,因为我可以在底图上绘制输出图像并且它出现在正确的位置。但是,当我随后尝试读取该 tif 文件并按照 this thread 中的建议使用光栅和形状将其转换为形状时,多边形最终从它们应该在的地方绕地球绘制了一部分。

按照上面的链接尝试转换为多边形

mask = image == 1
    
with rasterio.Env():
    with rasterio.open(output_image) as src:
        print("the src profile is",src.profile)
        image = src.read(1) # first band
        results = (
        {'properties': {'raster_val': v}, 'geometry': s}
        for i, (s, v) 
        in enumerate(
            shapes(image, mask=mask, transform=src.transform)))
        
geoms = list(results)

the src profile is {'driver': 'GTiff', 'dtype': 'uint16', 'nodata': None, 'width': 2738, 'height': 6289, 'count': 1, 'crs': CRS.from_epsg(3005), 'transform': Affine(1.0000000000009355, 0.0, 1254667.5000000002, 0.0, -1.0000000000008886, 791786.4), 'blockysize': 1, 'tiled': False, 'interleave': 'band'}

我不确定其余的配置文件信息是否正确,但鉴于 crs 是正确的,我假设其余信息来自同一来源,也应该正确匹配输入图像的空间配置文件。

如果有人能告诉我在最后一步我的预测在哪里发生,将不胜感激。

谢谢

python transformation spatial shapely rasterio
© www.soinside.com 2019 - 2024. All rights reserved.