如何修复ValueError:python中的输入形状不重叠光栅?
我想:如何修复ValueError:python中的输入形状不重叠光栅?,python,gis,Python,Gis,我想: 创建输入光栅的遮罩版本,其中一个字段内的像素设置为1,字段外的像素设置为0,然后 将输入光栅重新投影到提供的CRS Cde: 错误: 谢谢大家,我设法找到了解决我错误的方法: rasterio.tools.mask.mask(在较新的版本中,它是rasterio.mask.mask)包括一个选项invert。当invert=True时,该掩码将应用于与形状重叠的像素,而不是形状外部的区域 所以我改变了: out_img, out_transform = mask(data, shape
1
,字段外的像素设置为0
,然后谢谢大家,我设法找到了解决我错误的方法: rasterio.tools.mask.mask(在较新的版本中,它是rasterio.mask.mask)包括一个选项invert。当invert=True时,该掩码将应用于与形状重叠的像素,而不是形状外部的区域 所以我改变了:
out_img, out_transform = mask(data, shapes=coordinates, crop=True)
致:
错误已修复。这是我重写的完整代码。虽然我没有得到预期的输出,但是没有错误,这只是它必须满足断言要求的部分,必须被修复
def masked_raster(input_file, raster_file):
# Create a masked version of the input raster where pixels falling within one of the fields are set to `1` and pixels outside the fields are set to `0`
# the polygon GeoJSON geometry
geoms = [{'type': 'Polygon', 'coordinates': [[(250204.0, 141868.0), (250942.0, 141868.0), (250942.0, 141208.0), (250204.0, 141208.0), (250204.0, 141868.0)]]}]
# load the raster, mask it by the polygon and crop it
with rasterio.open("crops.tif") as src:
out_img, out_transform = mask(src, geoms, invert=True)
out_meta = src.meta.copy()
# save the resulting raster
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
return out_img
def reproject_raster(raster_file, dst_crs):
# Reproject the input raster to the provided CRS
with rasterio.open("masked.tif", "w", **out_meta) as dst:
dst.write(out_image)
dst = src
return dst
谢谢大家,我设法找到了解决我错误的方法: rasterio.tools.mask.mask(在较新的版本中,它是rasterio.mask.mask)包括一个选项invert。当invert=True时,该掩码将应用于与形状重叠的像素,而不是形状外部的区域 所以我改变了:
out_img, out_transform = mask(data, shapes=coordinates, crop=True)
致:
错误已修复。这是我重写的完整代码。虽然我没有得到预期的输出,但是没有错误,这只是它必须满足断言要求的部分,必须被修复
def masked_raster(input_file, raster_file):
# Create a masked version of the input raster where pixels falling within one of the fields are set to `1` and pixels outside the fields are set to `0`
# the polygon GeoJSON geometry
geoms = [{'type': 'Polygon', 'coordinates': [[(250204.0, 141868.0), (250942.0, 141868.0), (250942.0, 141208.0), (250204.0, 141208.0), (250204.0, 141868.0)]]}]
# load the raster, mask it by the polygon and crop it
with rasterio.open("crops.tif") as src:
out_img, out_transform = mask(src, geoms, invert=True)
out_meta = src.meta.copy()
# save the resulting raster
out_meta.update({"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform})
return out_img
def reproject_raster(raster_file, dst_crs):
# Reproject the input raster to the provided CRS
with rasterio.open("masked.tif", "w", **out_meta) as dst:
dst.write(out_image)
dst = src
return dst
请在发布后校对(编辑)。这与机器学习有什么关系?请在发布后校对(编辑)。这与机器学习有什么关系?