I'm wondering if there is a method to perform a function similar to ArcGIS's Con tool, using open source options in Python.

I have two rasters, one that shows veloticty and one that shows detph. With ArcPy it is easy to get a new raster:

Con(((detph >= 1) | (veloticty >= 1)|(detph*veloticty>=0.5)), 3,(Con(((detph >= 0.4) | (veloticty >= 0.4)|(detph*veloticty>=0.08)), 2,1)))

I would like to do with open source options in Python.


1 Answer 1


Try reading the rasters into numpy arrays using rasterio, numpy where instead of Con and then write the output as raster.

import numpy as np
import rasterio

demfile = '/home/bera/GIS/DEM50m/nh_68_4.tif'

dem = rasterio.open(demfile)
demarr = dem.read()
onezero = np.random.randint(2, size=demarr.shape)

newarr = np.where(onezero==1, demarr, np.where(demarr>500, 1000, 0))
newarr = newarr.astype('float32')

with rasterio.Env():
    profile = dem.profile
    with rasterio.open('/home/bera/Desktop/tempgis/dem_modified.tif', 'w', **profile) as dst:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.