Graphics Reference
In-Depth Information
Since the DEM data is in a raster format, you need to iterate over the individual pixels or
cells to get each height value. The
provider.xSize()
and
provider.ySize()
methods tell us how many cells are there in the DEM, while the
provider.extent()
method gives us the area of the Earth's surface covered by the DEM. Using this informa-
tion, we can extract the individual elevation values from the contents of the DEM in the
following way:
raster_extent = provider.extent()
raster_width = provider.xSize()
raster_height = provider.ySize()
block = provider.block(1, raster_extent, raster_width,
raster_height)
The returned
block
variable is an object of type
QgsRasterBlock
, which is essen-
tially a two-dimensional array of values. Let's iterate over the raster and extract the indi-
vidual elevation values:
for x in range(raster_width):
for y in range(raster_height):
elevation = block.value(x, y)
....
Now that we've loaded the individual elevation values, it's easy to build a histogram out of
those values. Here is the entire program to load the DEM data into memory, and then cal-
culate and display the histogram:
from PyQt4.QtCore import QSettings
QSettings().setValue("/Projections/defaultBehaviour",
"useGlobal")
registry = QgsProviderRegistry.instance()
provider = registry.provider("gdal", "/path/to/e10g")
raster_extent = provider.extent()
raster_width = provider.xSize()
raster_height = provider.ySize()
no_data_value = provider.srcNoDataValue(1)
histogram = {} # Maps elevation to number of occurrences.