如何使用 rasterio 更改栅格的 crs?
How to change the crs of a raster with rasterio?
我正在尝试更改光栅 tif 文件的 CRS。当我使用以下代码分配新的 CRS 时:
with rio.open(solar_path, mode='r+') as raster:
raster.crs = rio.crs.CRS({'init': 'epsg:27700'})
show((raster, 1))
print(raster.crs)
打印功能returns'EPSG:27700',但是打印图像后CRS明显没有变化?
与 Geopandas 不同,rasterio 在更改 crs 时需要手动重新投影。
def reproject_raster(in_path, out_path):
"""
"""
# reproject raster to project crs
with rio.open(in_path) as src:
src_crs = src.crs
transform, width, height = calculate_default_transform(src_crs, crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': crs,
'transform': transform,
'width': width,
'height': height})
with rio.open(out_path, 'w', **kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rio.band(src, i),
destination=rio.band(dst, i),
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=crs,
resampling=Resampling.nearest)
return(out_path)
我正在尝试更改光栅 tif 文件的 CRS。当我使用以下代码分配新的 CRS 时:
with rio.open(solar_path, mode='r+') as raster:
raster.crs = rio.crs.CRS({'init': 'epsg:27700'})
show((raster, 1))
print(raster.crs)
打印功能returns'EPSG:27700',但是打印图像后CRS明显没有变化?
与 Geopandas 不同,rasterio 在更改 crs 时需要手动重新投影。
def reproject_raster(in_path, out_path):
"""
"""
# reproject raster to project crs
with rio.open(in_path) as src:
src_crs = src.crs
transform, width, height = calculate_default_transform(src_crs, crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': crs,
'transform': transform,
'width': width,
'height': height})
with rio.open(out_path, 'w', **kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rio.band(src, i),
destination=rio.band(dst, i),
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=crs,
resampling=Resampling.nearest)
return(out_path)