Skip to content
Permalink
Browse files

[sextante] adding missing files from tools reorganization

  • Loading branch information
volaya committed Apr 11, 2013
1 parent 5a6562a commit dd0a83ba5f44166259bf3cb71775dd1821dd9c60
Showing with 151 additions and 0 deletions.
  1. +37 −0 python/plugins/sextante/tests/GdalTest.py
  2. +114 −0 python/plugins/sextante/tools/general.py
@@ -0,0 +1,37 @@
import sextante
import unittest
from sextante.tests.TestData import points, points2, polygons, polygons2, lines, union,\
table, polygonsGeoJson, raster
from sextante.core.QGisLayers import QGisLayers
import os
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
from sextante.core.SextanteUtils import SextanteUtils

class GdalTest(unittest.TestCase):

def test_gdalogrsieve(self):
outputs=sextante.runalg("gdalogr:sieve",raster(),2,0,None)
output=outputs['dst_filename']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,-1353696889)

def test_gdalogrsieveWithUnsupportedOutputFormat(self):
outputs=sextante.runalg("gdalogr:sieve",raster(),2,0, SextanteUtils.getTempFilename("img"))
output=outputs['dst_filename']
self.assertTrue(os.path.isfile(output))
dataset=gdal.Open(output, GA_ReadOnly)
strhash=hash(str(dataset.ReadAsArray(0).tolist()))
self.assertEqual(strhash,-1353696889)

def suite():
suite = unittest.makeSuite(GdalTest, 'test')
return suite

def runtests():
result = unittest.TestResult()
testsuite = suite()
testsuite.run(result)
return result
@@ -0,0 +1,114 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
general.py
---------------------
Date : April 2013
Copyright : (C) 2013 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__ = 'April 2013'
__copyright__ = '(C) 2013, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

from qgis.core import *
from sextante.core.QGisLayers import QGisLayers
from sextante.core.Sextante import Sextante
from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.gui.SextantePostprocessing import SextantePostprocessing

def alglist(text=None):
s=""
for provider in Sextante.algs.values():
sortedlist = sorted(provider.values(), key= lambda alg: alg.name)
for alg in sortedlist:
if text == None or text.lower() in alg.name.lower():
s+=(alg.name.ljust(50, "-") + "--->" + alg.commandLineName() + "\n")
print s

def algoptions(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
s =""
for param in alg.parameters:
if isinstance(param, ParameterSelection):
s+=param.name + "(" + param.description + ")\n"
i=0
for option in param.options:
s+= "\t" + str(i) + " - " + str(option) + "\n"
i+=1
print(s)
else:
print "Algorithm not found"

def alghelp(name):
alg = Sextante.getAlgorithm(name)
if alg != None:
print(str(alg))
algoptions(name)
else:
print "Algorithm not found"

def runalg(algOrName, *args):
alg = Sextante.runAlgorithm(algOrName, None, *args)
if alg is not None:
return alg.getOutputValuesAsDictionary()

def runandload(name, *args):
return Sextante.runAlgorithm(name, SextantePostprocessing.handleAlgorithmResults, *args)

def extent(layers):
first = True
for layer in layers:
if not isinstance(layer, (QgsRasterLayer, QgsVectorLayer)):
layer = QGisLayers.getObjectFromUri(layer)
if layer is None:
continue
if first:
xmin = layer.extent().xMinimum()
xmax = layer.extent().xMaximum()
ymin = layer.extent().yMinimum()
ymax = layer.extent().yMaximum()
else:
xmin = min(xmin, layer.extent().xMinimum())
xmax = max(xmax, layer.extent().xMaximum())
ymin = min(ymin, layer.extent().yMinimum())
ymax = max(ymax, layer.extent().yMaximum())
first = False
if first:
return "0,0,0,0"
else:
return str(xmin) + "," + str(xmax) + "," + str(ymin) + "," + str(ymax)

def getfromname(name):
layers = QGisLayers.getAllLayers()
for layer in layers:
if layer.name() == name:
return layer

def getfromuri(uri):
return QGisLayers.getObjectFromUri(uri, True)

def getobject(uriorname):
ret = getfromname(uriorname)
if ret is None:
ret = getfromuri(uriorname)
return ret

def load(path):
'''Loads a layer into QGIS'''
return QGisLayers.load(path)

0 comments on commit dd0a83b

Please sign in to comment.
You can’t perform that action at this time.