Sonny Bailey
Sonny Bailey

Reputation: 31

My code keeps throwing up a CPLE_AppDefinedError when using rasterio

Here is the issue. When I try to delete a temp.tiff file I no longer need, I get an error with this section of code. It is part of a for loop, and there's something in this for loop that is causing the code to not want to delete the temp.tiff file. It throws up a permission denied error because it thinks it is already open elsewhere so it can't then delete it. I can't figure out the issue.


  
    SAR_iDPolRAD_images_filenames_list = []
  
    for slice_index in range(number_of_images_to_examine_per_row**2):
        
        
        SAR_iDPolRAD_images_filenames_list.append(r'C:\Users\baileyj5\Documents\Satellite_data\Image_data\file_index_{0}\SAR_iDPolRAD_image_for_file_index_{0}_for_slice_index_{1}.png'.format(
            file_index,slice_index))
        
        
        if os.path.isfile(r'C:\Users\baileyj5\Documents\Satellite_data\Image_data\file_index_{0}\SAR_iDPolRAD_image_for_file_index_{0}_for_slice_index_{1}.png'.format(file_index,slice_index)):
            continue
        
       
        with rasterio.open(imfile) as dataset_full:
            cutout=from_bounds(merged_x_physical_coord_list_left_side[slice_index], 
                               merged_y_physical_coord_list_left_side[slice_index], 
                               merged_x_physical_coord_list_right_side[slice_index], 
                               merged_y_physical_coord_list_right_side[slice_index], 
                               dataset_full.transform)#To cut out in Earth coordinates
            dataset_w = dataset_full.read(1,window=cutout)#Cut out in georeferenced coordinates - i.e. the bounds are the georeferenced boundaries in metres.
            win_transform = dataset_full.window_transform(cutout)#tells you the Affine matrix of the window
            

        pixelSizeX = win_transform[0]#pixel size of window x metres
        pixelSizeY =-win_transform[4]#pixel size of window y metres
        #print('pixel size of window x metres',pixelSizeX)#comment this out if you want.
        #print('pixel size of window y metres',pixelSizeY)#comment this out if you want.

       
        datasetfromwindow = rasterio.open(
            r'C:\Users\baileyj5\Documents\Satellite_data\Image_data\file_index_{0}\temp.tiff'.format(file_index),
            'w',driver='GTiff',height=dataset_w.shape[0],width=dataset_w.shape[1],count=1,dtype=dataset_w.dtype,crs='EPSG:32640',transform=win_transform)
       
        
        datasetfromwindow.write(dataset_w, 1)
       
        datasetfromwindow.close()

      
        datasetfromwindow_read = rasterio.open(
        r'C:\Users\baileyj5\Documents\Satellite_data\Image_data\file_index_{0}\temp.tiff'.format(file_index))
        

One of the first things I tried was to make a small code in another script that would delete the temp.tiff file using os.remove which worked just fine. So I know something in this big code is throwing the error. And the debugging shows that it occurs within the for loop.

It happens at this section here:

 datasetfromwindow = rasterio.open(
            r'C:\Users\baileyj5\Documents\Satellite_data\Image_data\file_index_{0}\temp.tiff'.format(file_index),
            'w',driver='GTiff',height=dataset_w.shape[0],width=dataset_w.shape[1],count=1,dtype=dataset_w.dtype,crs='EPSG:32640',transform=win_transform)

And this is the error it will throw up:

CPLE_AppDefinedError: Deleting C:/Users/baileyj5/Documents/Satellite_data/Image_data/file_index_0/temp.tiff failed: Permission denied

Any help with this would be really helpful. Cheers.

I've attached a screenshot of the code as well.

Upvotes: 0

Views: 179

Answers (0)

Related Questions