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
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.