2013-02-23 21:43:17 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
raster.py
|
|
|
|
---------------------
|
|
|
|
Date : February 2013
|
2013-09-11 19:32:38 +02:00
|
|
|
Copyright : (C) 2013 by Victor Olaya and Alexander Bruy
|
2013-02-23 21:43:17 +01:00
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
2013-09-11 19:32:38 +02:00
|
|
|
__author__ = 'Victor Olaya and Alexander Bruy'
|
2013-02-23 21:43:17 +01:00
|
|
|
__date__ = 'February 2013'
|
2013-09-11 19:32:38 +02:00
|
|
|
__copyright__ = '(C) 2013, Victor Olaya and Alexander Bruy'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
|
|
|
import struct
|
2016-10-06 19:34:17 +03:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
from osgeo import gdal
|
2016-10-06 19:34:17 +03:00
|
|
|
|
2017-07-13 09:01:28 +03:00
|
|
|
from qgis.core import QgsProcessingException
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
|
2017-08-30 07:28:25 +10:00
|
|
|
def scanraster(layer, feedback, band_number=1):
|
2016-09-21 18:24:26 +02:00
|
|
|
filename = str(layer.source())
|
2016-10-06 19:34:17 +03:00
|
|
|
dataset = gdal.Open(filename, gdal.GA_ReadOnly)
|
2017-08-30 07:28:25 +10:00
|
|
|
band = dataset.GetRasterBand(band_number)
|
2013-02-28 22:08:32 +01:00
|
|
|
nodata = band.GetNoDataValue()
|
2015-01-31 21:08:29 +00:00
|
|
|
bandtype = gdal.GetDataTypeName(band.DataType)
|
2016-09-21 18:24:26 +02:00
|
|
|
for y in range(band.YSize):
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(y / float(band.YSize) * 100)
|
2013-10-01 20:52:22 +03:00
|
|
|
scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1,
|
|
|
|
band.DataType)
|
2015-01-31 21:08:29 +00:00
|
|
|
if bandtype == 'Byte':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('B' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'Int16':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('h' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'UInt16':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('H' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'Int32':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('i' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'UInt32':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('I' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'Float32':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('f' * band.XSize, scanline)
|
2015-01-31 21:08:29 +00:00
|
|
|
elif bandtype == 'Float64':
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
values = struct.unpack('d' * band.XSize, scanline)
|
|
|
|
else:
|
2017-07-13 09:01:28 +03:00
|
|
|
raise QgsProcessingException('Raster format not supported')
|
2013-02-16 00:23:56 +01:00
|
|
|
for value in values:
|
|
|
|
if value == nodata:
|
|
|
|
value = None
|
2013-02-28 16:55:41 +01:00
|
|
|
yield value
|
2013-09-11 19:32:38 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-09-11 19:32:38 +02:00
|
|
|
def mapToPixel(mX, mY, geoTransform):
|
2017-02-09 14:15:31 +02:00
|
|
|
(pX, pY) = gdal.ApplyGeoTransform(
|
|
|
|
gdal.InvGeoTransform(geoTransform), mX, mY)
|
2013-10-01 20:52:22 +03:00
|
|
|
return (int(pX), int(pY))
|
|
|
|
|
2013-09-11 19:32:38 +02:00
|
|
|
|
|
|
|
def pixelToMap(pX, pY, geoTransform):
|
2014-09-19 21:11:44 +03:00
|
|
|
return gdal.ApplyGeoTransform(geoTransform, pX + 0.5, pY + 0.5)
|