1

I asked this on the general stack overflow but not the GIS one. If that is against the rules I apologize:

I am writing a script to reproject rasters using rasterio and get the following error:

Attempt to create new tiff file 'E:/MapsForTesting/Reprojected' failed: Permission denied

Here is my code bloc:

import os
import rasterio
from rasterio.warp import calculate_default_transform , reproject, Resampling 

#get working directory
os.chdir('E:\MapsForTesting')
#open unprojected raster
dstCrs = 'EPSG:32620' # the EPSG code for UTM Zone 20

def reproject(infile, outfilepath):
    #kill = rasterio.open(infile) """Attempt to solve issue does not work"""
    with rasterio.open(infile) as srs:  
        srs_crs = srs.crs
        transform, width, height = calculate_default_transform(
            srs.crs, dstCrs, srs.width, srs.height, *srs.bounds) #calculate transform
        kwargs = srs.meta.copy()
        kwargs.update({
            'crs': dstCrs,
            'transform': transform,
            'width': width,
            'height': height
        })
    #kill.close()# close the rasterio dataset """Attempt to solve issue does not work"""
    #os.remove(infile)# delete the file       """Attempt to solve issue does not work"""

        with rasterio.open(outfilepath.format(), 'w', **kwargs) as dst: 
            for i in range(1, srs.count + 1):
                reproject(
                    source = rasterio.band(srs, i),
                    destination = rasterio.band(dst, i),
                    srs_transform = srs.transform,
                    srs_crs = src.crs,
                    dst_transform = transform,
                    dst_crs = dst_crs,
                    resampling = Resampling.nearest)
            dst.write(reproject)
    return(outfilepath)

1 Answer 1

2

You have many issues with your code:

  • Your path should be a raw path r'E:\MapsForTesting' not 'E:\MapsForTesting' notice the r.
  • srs_crs = src.crs, should be srs_crs = srs.crs, notice the typos src which should be srs.
  • dst_crs = dst_crs, should be dst_crs = dst.crs, notice the typos _ should be .

With all the above issues, I still could not make it work. So I started fresh from the examples written in the documentation. You can adapt the examples for your needs as follows:

import os
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling

path = r'E:\MapsForTesting'

#change in_raster.tif to the name input raster file
inras = os.path.join(path, 'in_raster.tif')

#change out_raster.tif to the name output raster file
outras = os.path.join(path, 'out_raster.tif')

dst_crs = 'EPSG:32620'

def reproject_ras(infile, outfile):

    with rasterio.open(infile) as src:
        transform, width, height = calculate_default_transform(
            src.crs, dst_crs, src.width, src.height, *src.bounds)
        kwargs = src.meta.copy()
        kwargs.update({
            'crs': dst_crs,
            'transform': transform,
            'width': width,
            'height': height
        })
    
        with rasterio.open(outfile, 'w', **kwargs) as dst:
            for i in range(1, src.count + 1):
                reproject(
                    source=rasterio.band(src, i),
                    destination=rasterio.band(dst, i),
                    src_transform=src.transform,
                    src_crs=src.crs,
                    dst_transform=transform,
                    dst_crs=dst_crs,
                    resampling=Resampling.nearest)
    return(outfile)

reproject_ras(inras, outras)

I believe using the above code will do the job.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.