Skip to main content
split example into simple part, and advanced part
Source Link
Mike T
  • 42.4k
  • 10
  • 129
  • 190

You can use rasteriorasterio to read/writeinterface with NumPy arrays. For example,To read, edit a pixel, then save it back.raster to an array:

import rasterio 

with rasterio.open('/path/to/raster.tif', 'r+''r') as rds:
    arr = rds.read()  # read all raster values
     
print(arr.shape)  # this is a 3D numpy array, with dimensions [band, row, col]

This will read everything into a 3D numpy array arr, with dimensions [band, row, col].


Here is an advanced example to read, edit a pixel, then save it back to the raster:

with rasterio.open('/path/to/raster.tif', 'r+') as ds:
    arr = ds.read()  # read all raster values
    arr[0, 10, 20] = 3  # change a pixel value on band 1, row 11, column 21
    rds.write(arr)
    

This will read everything into a 3D numpy array arr, with dimensions [band, row, col]. As with any "with" statement in Python, theThe raster will be written and closed at the end of the with-block"with" statement.

You can use rasterio to read/write NumPy arrays. For example, read, edit a pixel, then save it back.

import rasterio
with rasterio.open('/path/to/raster.tif', 'r+') as r:
    arr = r.read()  # read all raster values
    print(arr.shape)  # this is a numpy array, with dimensions [band, row, col]
    arr[0, 10, 20] = 3  # change a pixel value on band 1, row 11, column 21
    r.write(arr)
    

This will read everything into a 3D numpy array arr, with dimensions [band, row, col]. As with any "with" statement in Python, the raster will be written and closed at the end of the with-block.

You can use rasterio to interface with NumPy arrays. To read a raster to an array:

import rasterio 

with rasterio.open('/path/to/raster.tif', 'r') as ds:
    arr = ds.read()  # read all raster values
 
print(arr.shape)  # this is a 3D numpy array, with dimensions [band, row, col]

This will read everything into a 3D numpy array arr, with dimensions [band, row, col].


Here is an advanced example to read, edit a pixel, then save it back to the raster:

with rasterio.open('/path/to/raster.tif', 'r+') as ds:
    arr = ds.read()  # read all raster values
    arr[0, 10, 20] = 3  # change a pixel value on band 1, row 11, column 21
    ds.write(arr)

The raster will be written and closed at the end of the "with" statement.

Source Link
Mike T
  • 42.4k
  • 10
  • 129
  • 190

You can use rasterio to read/write NumPy arrays. For example, read, edit a pixel, then save it back.

import rasterio
with rasterio.open('/path/to/raster.tif', 'r+') as r:
    arr = r.read()  # read all raster values
    print(arr.shape)  # this is a numpy array, with dimensions [band, row, col]
    arr[0, 10, 20] = 3  # change a pixel value on band 1, row 11, column 21
    r.write(arr)
    

This will read everything into a 3D numpy array arr, with dimensions [band, row, col]. As with any "with" statement in Python, the raster will be written and closed at the end of the with-block.