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)
Best Answer
You have many issues with your code:
r'E:\MapsForTesting'
not'E:\MapsForTesting'
notice ther
.srs_crs = src.crs,
should besrs_crs = srs.crs,
notice the typossrc
which should besrs
.dst_crs = dst_crs,
should bedst_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:
I believe using the above code will do the job.