2

I've got a DTM and would like to add 1m to a percentage of the points, so I thought of creating a new raster containing either zeros or 1m and add the two raster together. I didn't find any tools that could do it on SAGA or GRASS.

Do you know a way?

0
1

You can create a numpy Array using random choice, convert to a ascii raster then add to your dtm using raster calculator:

import numpy as np

proportion_1 = 0.1 #Adjust
outfile = r'C:\folder\arr.asc' #Adjust path

rl = QgsProject.instance().mapLayersByName('DSM')[0] #Adjust to match your raster layer name
    
e = rl.extent()

h = rl.height()
w = rl.width()
xres = rl.rasterUnitsPerPixelX()

arr = np.random.choice(2, (w,h), p=[1-proportion_1,proportion_1])

with open(outfile,'w') as file:
    header= ['ncols {}'.format(h),
             'nrows {}'.format(w), 
             'xllcorner {}'.format(e.xMinimum()), 
             'yllcorner {}'.format(e.yMinimum()), 
             'cellsize {}'.format(xres), 
             'nodata_value -9999']
    for row in header:
        file.write(row+'\n')
    for row in arr.tolist():
        file.write(' '.join([str(val) for val in row]))

Then add them together using raster calculator. enter image description here

1
  • 1
    Thanks so much ! I'm going to try it today ! – Peter Muller Jun 25 '20 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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