1

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

2 Answers 2

1

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
0

Use Round Raster with a number of decimal places of -2 to round to multiples of 100:

negative number of decimal places may be used to round values to powers of a base n. QGIS help

Use round up/down options for different types of classes (e.g. 300 to 400, 400 to 500 instead of 350 to 450, 450 to 550 etc.).

Left: original, right: reclassified result: 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.