1

I am trying to test whether a value exists in a raster against all the values present. I am using pyqgis (Python 2.7) in in QGIS 2.14. I have found a number of ways to get point location values and even to list all values, but I cannot find a way to return a set of unique values.

closed as off-topic by PolyGeo Apr 30 '18 at 8:09

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3
lyr = iface.activeLayer()
# or
# lyr = QgsRasterLayer("path/to/raster.tif", "raster_data")

rows = lyr.height()
cols = lyr.width()
dpr = lyr.dataProvider()    

bl = dpr.block(1, dpr.extent(), cols, rows) # 1: band no

unique_values = list(set([bl.value(r, c) for r in range(rows) for c in range(cols)]))

unique_values also contains no data value. It may take a long time based on raster size.

  • Thanks, Kadir. This works like a charm. To the moderators who want to put this on hold, I assume because I did not include code - sorry, I thought it would be better to briefly describe the types of code I tried rather than waste space with any of the attempts I made that didn't work. I had no code that came close. – Mike May 1 '18 at 14:19

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