0

I am trying to create a function to re-project rasters in a dataset. However I get an error. RasterioIOError: Attempt to create new tiff file 'E:/MapsForTesting/Reprojected' failed: Permission denied

How do I allow my computer or rasterio to write files? I can't seem to find the answer. Here is my code:

###############################################################################################
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:  #if input is halifax.jpg or raw .tif will recieve north up error
        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)

reproject('E:\MapsForTesting\halifaxCRS1.tif','E:\MapsForTesting\Reprojected')
################################################################################################# 

I have validated that my function would work if I overwrite the same file however the output is not desirable and I would like the code to write a new file instead.

JonasV
  • 792
  • 5
  • 16

0 Answers0