本文整理汇总了Python中sextante.core.Sextante.Sextante.getObject方法的典型用法代码示例。如果您正苦于以下问题:Python Sextante.getObject方法的具体用法?Python Sextante.getObject怎么用?Python Sextante.getObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sextante.core.Sextante.Sextante
的用法示例。
在下文中一共展示了Sextante.getObject方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
# 需要导入模块: from sextante.core.Sextante import Sextante [as 别名]
# 或者: from sextante.core.Sextante.Sextante import getObject [as 别名]
def processAlgorithm(self, progress):
'''Here is where the processing itself takes place'''
#the first thing to do is retrieve the values of the parameters
#entered by the user
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputValue(self.OUTPUT_LAYER)
#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the Sextante.getObject() method
vectorLayer = Sextante.getObject(inputFilename)
#And now we can process
#First we create the output layer.
#The output value entered by the user is a string containing a filename,
#so we can use it directly
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )
#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
for feat in selection:
writer.addFeature(feat)
del writer
示例2: createTest
# 需要导入模块: from sextante.core.Sextante import Sextante [as 别名]
# 或者: from sextante.core.Sextante.Sextante import getObject [as 别名]
def createTest(text):
s = ""
tokens = text[len("sextante.runalg("):-1].split(",")
cmdname = tokens[0][1:-1];
methodname = "test_" + cmdname.replace(":","")
s += "def " + methodname + "(self):\n"
alg = Sextante.getAlgorithm(cmdname)
execcommand = "sextante.runalg("
i = 0
for token in tokens:
if i < alg.getVisibleParametersCount() + 1:
if os.path.exists(token[1:-1]):
token = os.path.basename(token[1:-1])[:-4]
execcommand += token + "(),"
else:
execcommand += "None,"
i+=1
s += "\toutputs=" + execcommand[:-1] + ")\n"
i = -1 * len(alg.outputs)
for out in alg.outputs:
filename = tokens[i][1:-1]
if (filename == str(None)):
raise Exception("Cannot create unit test for that algorithm execution.\nThe output cannot be a temporary file")
s+="\toutput=outputs['" + out.name + "']\n"
if isinstance(out, (OutputNumber, OutputString)):
s+="self.assertTrue(" + str(out) + ", output)\n"
if isinstance(out, OutputRaster):
dataset = gdal.Open(filename, GA_ReadOnly)
array = dataset.ReadAsArray(1)
s+="\tself.assertTrue(os.path.isfile(output))\n"
s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
if isinstance(out, OutputVector):
layer = Sextante.getObject(filename)
fields = layer.pendingFields()
s+="\tlayer=QGisLayers.getObjectFromUri(output, True)\n"
s+="\tfields=layer.pendingFields()\n"
s+="\texpectednames=[" + ",".join(["'" + str(f.name()) + "'" for f in fields]) + "]\n"
s+="\texpectedtypes=[" + ",".join(["'" + str(f.typeName()) +"'" for f in fields]) + "]\n"
s+="\tnames=[str(f.name()) for f in fields]\n"
s+="\ttypes=[str(f.typeName()) for f in fields]\n"
s+="\tself.assertEqual(expectednames, names)\n"
s+="\tself.assertEqual(expectedtypes, types)\n"
features = QGisLayers.features(layer)
numfeat = len(features)
s+="\tfeatures=sextante.getfeatures(layer)\n"
s+="\tself.assertEqual(" + str(numfeat) + ", len(features))\n"
if numfeat > 0:
feature = features.next()
attrs = feature.attributes()
s+="\tfeature=features.next()\n"
s+="\tattrs=feature.attributes()\n"
s+="\texpectedvalues=[" + ",".join(['"' + str(attr.toString()) + '"' for attr in attrs]) + "]\n"
s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
s+="\tself.assertEqual(expectedvalues, values)\n"
dlg = ShowTestDialog(s)
dlg.exec_()
示例3: createTest
# 需要导入模块: from sextante.core.Sextante import Sextante [as 别名]
# 或者: from sextante.core.Sextante.Sextante import getObject [as 别名]
def createTest(item):
s = ""
tokens = item.entry.text[len("sextante.runalg("):-1].split(",")
cmdname = tokens[0][1:-1];
methodname = "test_" + cmdname.replace(":","")
s += "def " + methodname + "():\n"
alg = Sextante.getAlgorithm(cmdname)
execcommand = "sextante.runalg("
i = 0
for token in tokens:
if i < alg.getVisibleParametersCount():
execcommand+=token + ","
else:
execcommand+="None,"
i+=1
s += "\toutputs=" + execcommand[:-1] + ")\n"
i = -1 * len(alg.outputs)
for out in alg.outputs:
filename = tokens[i][1:-1]
s+="\toutput=outputs['" + out.name + "']\n"
if isinstance(out, (OutputNumber, OutputString)):
s+="self.assertTrue(" + str(out) + ", output)\n"
if isinstance(out, OutputRaster):
dataset = gdal.Open(filename, GA_ReadOnly)
array = dataset.ReadAsArray(1)
s+="\tself.assertTrue(os.path.isfile(output))\n"
s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
if isinstance(out, OutputVector):
layer = Sextante.getObject(filename)
fields = layer.pendingFields()
s+="\tlayer=sextante.getobject(output)\n"
s+="\tfields=layer.pendingFields()\n"
s+="\texpectednames=[" + ",".join([str(f.name()) for f in fields]) + "]\n"
s+="\texpectedtypes=[" + ",".join([str(f.typeName()) for f in fields]) + "]\n"
s+="\tnames=[str(f.name()) for f in fields]\n"
s+="\ttypes=[str(f.typeName()) for f in fields]\n"
s+="\tself.assertEqual(exceptednames, names)\n"
s+="\tself.assertEqual(exceptedtypes, types)\n"
features = QGisLayers.features(layer)
numfeat = len(features)
s+="\tfeatures=sextante.getfeatures(layer))\n"
s+="\tself.assertEqual(" + str(numfeat) + ", len(features)\n"
if numfeat > 0:
feature = features.next()
attrs = feature.attributes()
s+="\tfeature=features.next()\n"
s+="\tattrs=feature.attributes()\n"
s+="\texpectedvalues=[" + ",".join([str(attr.toString()) for attr in attrs]) + "]\n"
s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
s+="\tself.assertEqual(exceptedtypes, types)\n"
dlg = ShowTestDialog(s)
dlg.exec_()
示例4: processAlgorithm
# 需要导入模块: from sextante.core.Sextante import Sextante [as 别名]
# 或者: from sextante.core.Sextante.Sextante import getObject [as 别名]
def processAlgorithm(self, progress):
# get the lib
liblwgeom = self.getLwgeomLibrary()
# retrieve the values of the parameters entered by the user
inputFilename = self.getParameterValue(self.INPUT_LAYER)
output = self.getOutputValue(self.OUTPUT_LAYER)
# input layers vales are always a string with its location.
# That string can be converted into a QGIS object (a QgsVectorLayer in this case))
# using the Sextante.getObject() method
inputLayer = Sextante.getObject(inputFilename)
# create the output layer
provider = inputLayer.dataProvider()
encoding = provider.encoding()
geomType = self.inputToOutputGeomType(inputLayer)
writer = QgsVectorFileWriter( output, encoding, provider.fields(), geomType, provider.crs() )
# Now we take the features and add them to the output layer,
# first check for selected features
selection = inputLayer.selectedFeatures()
if len(selection) > 0:
count = len(selection)
idx = 0
for feat in selection:
# run lwgeom algorithm on the feature geometry
if not self.runLwgeom( feat.geometry(), lib=liblwgeom ):
SextanteLog.addToLog( SextanteLog.LOG_ERROR, u"FAILURE: previous failure info: layer %s, feature #%s" % (inputLayer.source(), feat.id()) )
writer.addFeature(feat)
progress.setPercentage( idx*100/count )
idx += 1
else:
count = inputLayer.featureCount()
idx = 0
# no features selected on the layer, process all the features
inputLayer.select( inputLayer.pendingAllAttributesList(), QgsRectangle(), True )
feat = QgsFeature()
while inputLayer.nextFeature( feat ):
# run lwgeom algorithm on the feature geometry
if not self.runLwgeom( feat.geometry(), lib=liblwgeom ):
SextanteLog.addToLog( SextanteLog.LOG_ERROR, u"FAILURE: previous failure info: layer %s, feature #%s" % (inputLayer.source(), feat.id()) )
writer.addFeature(feat)
progress.setPercentage( idx*100/count )
idx += 1
del writer
progress.setPercentage( 100 )