Overwrite raster in place


himat15@...
 

I have two rasters, and one of them has one more column than the other. So I want to remove the last column in the larger one.
How can I change the raster width in place without having to do  a with open(f, 'r') and then another with open(f, 'w')?
I tried using r+, but when trying to do ds.width = 890, I got AttributeError: attribute 'width' of 'rasterio._base.DatasetBase' objects is not writable

with rio.open(f, 'r+') as ds:
        print(ds.profile) 
 
        ds_arr = ds.read(1) # Do I need to actually modify the np array here and then overwrite the file later?
        print(ds_arr.shape)
        
        profile = ds.profile
        
        width = profile['width']
        height = profile['height']
        
        if width == 891:
            ds.width = 890 # error
            
Profile: {'driver': 'GTiff', 'dtype': 'int16', 'nodata': -1.0, 'width': 891, 'height': 3081, 'count': 1, 'crs': CRS.from_epsg(32629), 'transform': Affine(15.0, 0.0, 232053.930561042,
       0.0, -15.0, 1438436.08069127), 'tiled': False, 'interleave': 'band'}


Sean Gillies
 

It's not possible to change the shape of a raster dataset, the number of bands, or the data type in place.

On Wed, Mar 18, 2020 at 1:40 PM himat15 via Groups.Io <himat15=yahoo.com@groups.io> wrote:
I have two rasters, and one of them has one more column than the other. So I want to remove the last column in the larger one.
How can I change the raster width in place without having to do  a with open(f, 'r') and then another with open(f, 'w')?
I tried using r+, but when trying to do ds.width = 890, I got AttributeError: attribute 'width' of 'rasterio._base.DatasetBase' objects is not writable

with rio.open(f, 'r+') as ds:
        print(ds.profile) 
 
        ds_arr = ds.read(1) # Do I need to actually modify the np array here and then overwrite the file later?
        print(ds_arr.shape)
        
        profile = ds.profile
        
        width = profile['width']
        height = profile['height']
        
        if width == 891:
            ds.width = 890 # error
            
Profile: {'driver': 'GTiff', 'dtype': 'int16', 'nodata': -1.0, 'width': 891, 'height': 3081, 'count': 1, 'crs': CRS.from_epsg(32629), 'transform': Affine(15.0, 0.0, 232053.930561042,
       0.0, -15.0, 1438436.08069127), 'tiled': False, 'interleave': 'band'}



--
Sean Gillies