Skip to content

Commit

Permalink
Minor SEXTANTE changes
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Feb 3, 2013
1 parent 9a70fe0 commit 3751337
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 22 deletions.
66 changes: 66 additions & 0 deletions python/plugins/sextante/algs/CreateConstantRaster.py
@@ -0,0 +1,66 @@
from sextante.core.SextanteRasterWriter import SextanteRasterWriter
from sextante.parameters.ParameterRaster import ParameterRaster



# -*- coding: utf-8 -*-

"""
***************************************************************************
AutoincrementalField.py
---------------------
Date : August 2012
Copyright : (C) 2012 by Victor Olaya
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. *
* *
***************************************************************************
"""

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from sextante.core.GeoAlgorithm import GeoAlgorithm
from PyQt4.QtCore import *
from qgis.core import *
from sextante.parameters.ParameterNumber import ParameterNumber
from sextante.outputs.OutputRaster import OutputRaster
from sextante.core.QGisLayers import QGisLayers

class CreateConstantRaster(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"
NUMBER = "NUMBER"

#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")
#===========================================================================

def processAlgorithm(self, progress):
output = self.getOutputFromName(self.OUTPUT)
value = self.getOutputValue(self.NUMBER)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
cellsize = (layer.extent().xMaximum() - layer.extent().xMinimum())/layer.width()
w = SextanteRasterWriter(output.getCompatibleFileName(self), layer.extent().xMinimum(), layer.extent().yMinimum(), layer.extent().xMaximum(),
layer.extent().yMaximum(), cellsize, 1, self.crs)
w.matrix[:] = value
w.close()

def defineCharacteristics(self):
self.name = "Create constant raster layer"
self.group = "Raster tools"
self.addParameter(ParameterRaster(self.INPUT, "Reference layer"))
self.addParameter(ParameterNumber(self.NUMBER, "Constant value", default = 1.0));
self.addOutput(OutputRaster(self.OUTPUT, "Output layer"))


27 changes: 19 additions & 8 deletions python/plugins/sextante/core/SextanteRasterWriter.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
from PyQt4 import QtGui

__author__ = 'Victor Olaya'
__date__ = 'September 2012'
Expand All @@ -26,32 +27,42 @@
from qgis.core import *
import numpy
from PyQt4.QtCore import *
from osgeo import gdal
from osgeo import osr

class SextanteRasterWriter:

NODATA = -99999.0

def __init__(self, fileName, minx, miny, maxx, maxy, cellsize, nbands, crs):
self.fileName = fileName
self.nx = (maxx - minx) / float(cellsize)
self.ny = (maxy - miny) / float(cellsize)
self.matrix = numpy.empty(shape=(self.nx, self.ny, nbands))
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.crs = crs
self.minx = minx
self.maxy = maxy

def setValue(self, value, x, y, band = 0):
try:
self.matrix[x, y, band] = value
self.matrix[y, x] = value
except IndexError:
pass

def getValue(self, x, y, band = 0):
try:
return matrix[x, y, band]
return self.matrix[y, x]
except IndexError:
return self.NODATA

def close(self):
#todo
pass
format = "GTiff"
driver = gdal.GetDriverByName( format )
dst_ds = driver.Create(self.fileName, self.nx, self.ny, 1, gdal.GDT_Float32)
dst_ds.SetProjection(str(self.crs.toWkt()))
dst_ds.SetGeoTransform( [self.minx, self.cellsize, 0, self.maxy, self.cellsize, 0] )
dst_ds.GetRasterBand(1).WriteArray(self.matrix)
dst_ds = None
11 changes: 0 additions & 11 deletions python/plugins/sextante/outputs/OutputRaster.py
Expand Up @@ -53,14 +53,3 @@ def getCompatibleFileName(self, alg):
self.compatible = SextanteUtils.getTempFilename(self.getDefaultFileExtension(alg))
return self.compatible;

def getVectorWriter(self, minx, miny, maxx, maxy, cellsize, nbands, crs):
'''Returns a suitable raster. Use this to transparently handle output
values instead of creating your own method.
It should be called just once, since a new call might
result in previous data being replaced, thus rendering a previously
obtained writer useless
'''

w = SextanteRasterWriter(self.value, minx, miny, maxx, maxy, cellsize, nbands, crs)
return w
2 changes: 1 addition & 1 deletion python/plugins/sextante/parameters/ParameterRaster.py
Expand Up @@ -73,7 +73,7 @@ def setValue(self, obj):
if layer.name() == self.value:
self.value = unicode(layer.dataProvider().dataSourceUri())
return True
return True
return os.path.exists(self.value)

def serialize(self):
return self.__module__.split(".")[-1] + "|" + self.name + "|" + self.description +\
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/sextante/parameters/ParameterTable.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
import os

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -62,7 +63,7 @@ def setValue(self, obj):
val = unicode(obj)
if val.endswith("dbf") or val.endswith("csv"):
self.value = val
return True
return os.path.exists(self.value)
else:
return False

Expand Down
3 changes: 2 additions & 1 deletion python/plugins/sextante/parameters/ParameterVector.py
Expand Up @@ -16,6 +16,7 @@
* *
***************************************************************************
"""
import os

__author__ = 'Victor Olaya'
__date__ = 'August 2012'
Expand Down Expand Up @@ -60,7 +61,7 @@ def setValue(self, obj):
if layer.name() == self.value:
self.value = unicode(layer.source())
return True
return True
return os.path.exists(self.value)

def getSafeExportedLayer(self):
'''Returns not the value entered by the user, but a string with a filename which
Expand Down

0 comments on commit 3751337

Please sign in to comment.