rastertest.py
1 |
# Analyzing raster data
|
---|---|
2 |
registry = QgsProviderRegistry.instance() |
3 |
provider = registry.createProvider("gdal", "C:/Users/kimpr/Desktop/QGIS_TrainingTutorial_Data/raster/SRTM/srtm_41_19.tif") |
4 |
|
5 |
extent = provider.extent() |
6 |
width = provider.xSize() |
7 |
height = provider.ySize() |
8 |
|
9 |
no_data_value = provider.sourceHasNoDataValue(1)
|
10 |
|
11 |
histogram = {} # mapping the elevation to number of occurrences
|
12 |
# Read the raster by using the block method
|
13 |
block = provider.block(1, extent, width, height)
|
14 |
# Read all the data (1) from the raster file
|
15 |
|
16 |
for x in range(width): |
17 |
for y in range(height): |
18 |
elevation = block.value(x, y) |
19 |
|
20 |
if elevation == no_data_value: continue |
21 |
|
22 |
try:
|
23 |
histogram[elevation] += 1
|
24 |
except KeyError: |
25 |
histogram[elevation] = 1
|
26 |
|
27 |
for height in sorted(histogram.keys()): |
28 |
print (height, histogram[height])
|