Skip to content
Permalink
Browse files

fixed #6221 (single to multiparts)

adapted SEXTANTE native algs to be able to handle all kinds of vector outputs

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@354 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com
volayaf@gmail.com committed Aug 20, 2012
1 parent 13e6dcb commit 17423d4b39a8e7b9e561feabdd8820f055c92f46
@@ -34,16 +34,14 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
fieldtype = self.getParameterValue(self.FIELD_TYPE)
fieldname = self.getParameterValue(self.FIELD_NAME)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, self.TYPES[fieldtype])
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -16,16 +16,14 @@ def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField("AUTO", QVariant.Int)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -19,16 +19,14 @@ def getIcon(self):

def processAlgorithm(self, progress):
field_index = self.getParameterValue(self.FIELD)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField("NUM_FIELD", QVariant.Int)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -17,16 +17,13 @@ def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/../images/toolbox.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
output = self.getOutputValue(self.OUTPUT)
output = self.getOutputFromName(self.OUTPUT)
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( output, systemEncoding,
fields, QGis.WKBLineString, vprovider.crs() )
writer = output.getVectorWriter(fields, QGis.WKBLineString, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -18,7 +18,6 @@ class FieldsPyculator(GeoAlgorithm):
INPUT_LAYER = "INPUT_LAYER"
USE_SELECTED = "USE_SELECTED"
FIELD_NAME = "FIELD_NAME"
#USE_GLOBAL = "USE_GLOBAL"
GLOBAL = "GLOBAL"
FORMULA = "FORMULA"
OUTPUT_LAYER ="OUTPUT_LAYER"
@@ -33,7 +32,6 @@ def defineCharacteristics(self):
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterBoolean(self.USE_SELECTED, "Use only selected features", False))
self.addParameter(ParameterString(self.FIELD_NAME, "Result field name", "NewField"))
#self.addParameter(ParameterBoolean(self.USE_GLOBAL, "Use global expression", False))
self.addParameter(ParameterString(self.GLOBAL, "Global expression", multiline = True))
self.addParameter(ParameterString(self.FORMULA, "Formula", "value = ", multiline = True))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
@@ -43,18 +41,17 @@ def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD_NAME)
code = self.getParameterValue(self.FORMULA)
globalExpression = self.getParameterValue(self.GLOBAL)
#useGlobal = self.getParameterValue(self.USE_GLOBAL)
useSelected = self.getParameterValue(self.USE_SELECTED)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = layer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, QVariant.Double)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
outFeat = QgsFeature()
nFeatures = vprovider.featureCount()
nElement = 0
@@ -32,16 +32,14 @@ def defineCharacteristics(self):
def processAlgorithm(self, progress):
fieldname = self.getParameterValue(self.FIELD_NAME)
formula = self.getParameterValue(self.FORMULA)
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(self.OUTPUT_LAYER)
output = self.getOutputFromName(self.OUTPUT_LAYER)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
fields[len(fields)] = QgsField(fieldname, QVariant.Double)
writer = QgsVectorFileWriter( output, systemEncoding,fields, vprovider.geometryType(), vprovider.crs() )
writer = output.getVectorWriter(fields, vprovider.geometryType(), vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
inGeom = QgsGeometry()
@@ -50,7 +50,7 @@ def processAlgorithm(self, progress):
#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)
output = self.getOutputFromName(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))
@@ -60,12 +60,10 @@ def processAlgorithm(self, progress):
#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()
#To do so, we call the getVectorWriter method in the Output object.
#That will give as a SextanteVectorWriter, that we can later use to add features.
provider = vectorLayer.dataProvider()
writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )
writer = output.getVectorWriter( provider.fields(), provider.geometryType(), provider.crs() )

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
@@ -37,8 +37,6 @@ def processAlgorithm(self, progress):
unique = ftools_utils.getUniqueValues( vprovider, int( index ) )
nFeat = vprovider.featureCount() * len( unique )
nElement = 0
#self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
# self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
if not len( unique ) == vlayer.featureCount():
for i in unique:
vprovider.rewind()
@@ -65,8 +63,26 @@ def processAlgorithm(self, progress):
else:
raise GeoAlgorithmExecutionException("Invalid unique ID Field")

def extractAsMulti( self, geom ):
temp_geom = []
if geom.type() == 0:
if geom.isMultipart():
return geom.asMultiPoint()
else:
return [ geom.asPoint() ]
elif geom.type() == 1:
if geom.isMultipart():
return geom.asMultiPolyline()
else:
return [ geom.asPolyline() ]
else:
if geom.isMultipart():
return geom.asMultiPolygon()
else:
return [ geom.asPolygon() ]

def singleToMultiGeom(self, wkbType):
try:
try:
if wkbType in (QGis.WKBPoint, QGis.WKBMultiPoint,
QGis.WKBPoint25D, QGis.WKBMultiPoint25D):
return QGis.WKBMultiPoint
@@ -81,6 +97,15 @@ def singleToMultiGeom(self, wkbType):
except Exception, err:
print str(err)

def convertGeometry( self, geom_list, vType ):
if vType == 0:
return QgsGeometry().fromMultiPoint( geom_list )
elif vType == 1:
return QgsGeometry().fromMultiPolyline( geom_list )
else:
return QgsGeometry().fromMultiPolygon( geom_list )


def defineCharacteristics(self):
self.name = "Singleparts to multipart"
self.group = "Geometry tools"
@@ -68,9 +68,9 @@ def raiseInternalError(self, error):
def runalg(self):
try:
self.algorithm.execute(self.progress)
except GeoAlgorithmExecutionException as e :
except GeoAlgorithmExecutionException, e :
self.error.emit(e.msg)
except BaseException as e:
except BaseException, e:
self.internalError.emit(e)
# catch *all* errors, because QGIS tries to handle them in the GUI, which is fatal, this
# being a separate thread.

0 comments on commit 17423d4

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