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
|
|
|
|
2013-02-23 21:43:17 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-23 21:43:17 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
import struct
|
2014-07-14 14:19:09 +02:00
|
|
|
import numpy
|
2013-02-16 00:23:56 +01:00
|
|
|
from osgeo import gdal
|
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
|
|
|
from osgeo.gdalconst import GA_ReadOnly
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
|
|
|
|
def scanraster(layer, progress):
|
|
|
|
filename = unicode(layer.source())
|
|
|
|
dataset = gdal.Open(filename, GA_ReadOnly)
|
|
|
|
band = dataset.GetRasterBand(1)
|
2013-02-28 22:08:32 +01:00
|
|
|
nodata = band.GetNoDataValue()
|
2015-01-31 21:08:29 +00:00
|
|
|
bandtype = gdal.GetDataTypeName(band.DataType)
|
2013-02-28 22:08:32 +01:00
|
|
|
for y in xrange(band.YSize):
|
|
|
|
progress.setPercentage(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:
|
2015-01-31 21:08:29 +00:00
|
|
|
raise GeoAlgorithmExecutionException('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):
|
2014-09-19 21:11:44 +03:00
|
|
|
(pX, pY) = gdal.ApplyGeoTransform(
|
|
|
|
gdal.InvGeoTransform(geoTransform)[1], 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)
|
2014-07-14 14:19:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RasterWriter:
|
|
|
|
|
|
|
|
NODATA = -99999.0
|
|
|
|
|
|
|
|
def __init__(self, fileName, minx, miny, maxx, maxy, cellsize,
|
|
|
|
nbands, crs):
|
|
|
|
self.fileName = fileName
|
|
|
|
self.nx = int((maxx - minx) / float(cellsize))
|
|
|
|
self.ny = int((maxy - miny) / float(cellsize))
|
|
|
|
self.nbands = nbands
|
|
|
|
self.matrix = numpy.ones(shape=(self.ny, self.nx), dtype=numpy.float32)
|
|
|
|
self.matrix[:] = self.NODATA
|
|
|
|
self.cellsize = cellsize
|
|
|
|
self.crs = crs
|
|
|
|
self.minx = minx
|
|
|
|
self.maxy = maxy
|
|
|
|
|
|
|
|
def setValue(self, value, x, y, band=0):
|
|
|
|
try:
|
|
|
|
self.matrix[y, x] = value
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getValue(self, x, y, band=0):
|
|
|
|
try:
|
|
|
|
return self.matrix[y, x]
|
|
|
|
except IndexError:
|
|
|
|
return self.NODATA
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
format = 'GTiff'
|
|
|
|
driver = gdal.GetDriverByName(format)
|
|
|
|
dst_ds = driver.Create(self.fileName, self.nx, self.ny, 1,
|
|
|
|
gdal.GDT_Float32)
|
2015-08-16 20:57:24 +02:00
|
|
|
dst_ds.SetProjection(unicode(self.crs.toWkt()))
|
2014-07-14 14:19:09 +02:00
|
|
|
dst_ds.SetGeoTransform([self.minx, self.cellsize, 0,
|
|
|
|
self.maxy, self.cellsize, 0])
|
|
|
|
dst_ds.GetRasterBand(1).WriteArray(self.matrix)
|
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
|
|
|
dst_ds = None
|