Python 用Rasterio和菲奥娜裁剪光栅
我想使用分水岭作为边界剪裁光栅数据。我用菲奥娜和Rasterio试过了。 这是我的代码:Python 用Rasterio和菲奥娜裁剪光栅,python,python-3.x,Python,Python 3.x,我想使用分水岭作为边界剪裁光栅数据。我用菲奥娜和Rasterio试过了。 这是我的代码: import fiona import rasterio from rasterio.rio.clip import clip with fiona.open("oreto_bacino2.shp", "r") as shapefile: geoms = [feature["geometry"] for feature in shapefile] with rasterio.open("cn.as
import fiona
import rasterio
from rasterio.rio.clip import clip
with fiona.open("oreto_bacino2.shp", "r") as shapefile:
geoms = [feature["geometry"] for feature in shapefile]
with rasterio.open("cn.asc") as src:
out_image, out_transform = clip (geoms, src, crop=True)
out_meta = src.meta.copy()
out_meta.update({
"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform
})
with rasterio.open("cn_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)
complete_var = '_%s_COMPLETE' % (prog_name.replace('-', '_')).upper()
这是我犯的错误:
import fiona
import rasterio
from rasterio.rio.clip import clip
with fiona.open("oreto_bacino2.shp", "r") as shapefile:
geoms = [feature["geometry"] for feature in shapefile]
with rasterio.open("cn.asc") as src:
out_image, out_transform = clip (geoms, src, crop=True)
out_meta = src.meta.copy()
out_meta.update({
"driver": "GTiff",
"height": out_image.shape[1],
"width": out_image.shape[2],
"transform": out_transform
})
with rasterio.open("cn_masked.tif", "w", **out_meta) as dest:
dest.write(out_image)
complete_var = '_%s_COMPLETE' % (prog_name.replace('-', '_')).upper()
这会引发此错误:
AttributeError:“rasterio.\u io.RasterReader”对象没有属性“replace”
有人知道执行我的操作的正确方法吗?像
rasterio.rio.clip.clip
这样的rio
函数不能被称为Python函数,因为它们已经附加了一个命令行界面(所有这些装饰器),并且经过优化,可以从中获取参数
相反,您可以查看它是如何实现剪裁的,并模仿它。它基本上从几何体的边界创建一个窗口,并使用该窗口从源光栅读取(窗口读取)
或者,您可以按照使用光栅化.mask.mask(…,crop=True)
的操作,不仅剪裁到边界,而且还遮罩几何体外部的像素,这可能更接近您想要执行的操作