0

I would like to reclassify a DEM raster into bins of 100 metres. Both the GRASS r.reclass and built-in QGIS tools require predefined classes. I would like to have them produced automatically from the data range of the raster. My current solution is via Excel used to create the reclass list for r.reclass. However, I would like to avoid this step.

Any ideas for an easier way?

0

1 Answer 1

0

You can use python to create the classes to use in reclassifybytable:

import math
rlayer = QgsProject.instance().mapLayersByName('nh_67_4')[0]
stats = rlayer.dataProvider().bandStatistics(1, QgsRasterBandStats.All)

MIN, MAX = stats.minimumValue, stats.maximumValue #142.04449462890625   920.567626953125
n = 100
bins = [math.floor(r/100)*100 for r in range(int(MIN), int(MAX)+n, n)] #[100, 200, 300, 400, 500, 600, 700, 800, 900]
bins+=[bins[-1]+n] #[100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
bins = [b for b in zip(bins, bins[1:], bins)] #[(100, 200, 100), (200, 300, 200), (300, 400, 300), (400, 500, 400), (500, 600, 500), (600, 700, 600), (700, 800, 700), (800, 900, 800), (900, 1000, 900)]
bins = [item for sublist in bins for item in sublist] #[100, 200, 100, 200, 300, 200, 300, 400, 300, 400, 500, 400, 500, 600, 500, 600, 700, 600, 700, 800, 700, 800, 900, 800, 900, 1000, 900]

processing.runAndLoadResults("native:reclassifybytable",  {'INPUT_RASTER':rlayer,'RASTER_BAND':1,
    'TABLE':bins,'NO_DATA':-9999,'RANGE_BOUNDARIES':0,
    'NODATA_FOR_MISSING':False,'DATA_TYPE':5,'OUTPUT':'TEMPORARY_OUTPUT'})

enter image description here

0

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.