Python 如何使用光栅更改光栅的crs?

Python 如何使用光栅更改光栅的crs?,python,gis,spatial,geopandas,rasterio,Python,Gis,Spatial,Geopandas,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) 打印功能返回“EPSG:27700”,但是在打印图像后,CRS显然没有改变?与Geopandas不同,rasterio在改变CRS时需

我正在尝试更改光栅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)

打印功能返回“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)
欲了解更多详情,请访问