def getVectorWriter(self, fields, geomType, crs, options=None): '''Returns a suitable writer to which features can be added as a result of the algorithm. Use this to transparently handle output values instead of creating your own method. Executing this method might modify the object, adding additional information to it, so the writer can be later accessed and processed within QGIS. It should be called just once, since a new call might result in previous data being replaced, thus rendering a previously obtained writer useless @param fields a dict of int-QgsField @param geomType a suitable geometry type, as it would be passed to a QgsVectorFileWriter constructor @param crs the crs of the layer to create @return writer instance of the vectoe writer class ''' if self.encoding is None: settings = QSettings() self.encoding = settings.value("/SextanteQGIS/encoding", "System").toString() w = SextanteVectorWriter(self.value, self.encoding, fields, geomType, crs, options) self.memoryLayer = w.memLayer return w
#Here we define the input and outputs #==================================== ##[Example scripts]=group ##input=vector ##output=output vector #And here is the body of the algorithm #======================================= #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.getObjectFromUri() method vectorLayer = QGisLayers.getObjectFromUri(input) #And now we can process #First we create the output layer. #To do so, we create a SextanteVectorWriter, that we can later use to add features. provider = vectorLayer.dataProvider() writer = SextanteVectorWriter(output, None, 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 #There is nothing more to do here. We do not have to open the layer that we have created. #SEXTANTE will take care of that, or will handle it if this algorithm is executed within #a complex model
##input=vector ##output=output vector from sextante.core.SextanteVectorWriter import SextanteVectorWriter from qgis.core import * from PyQt4.QtCore import * inputLayer = sextante.getobject(input) features = sextante.getfeatures(inputLayer) fields = inputLayer.pendingFields().toList() outputLayer = SextanteVectorWriter(output, None, fields, QGis.WKBPoint, inputLayer.crs()) count = 0 mean = [0 for field in fields] x = 0 y = 0 for ft in features: c = ft.geometry().centroid().asPoint() x += c.x() y += c.y() attrs = ft.attributes() for f in range(len(fields)): try: mean[f] += float(attrs[f].toDouble()[0]) except: pass count += 1 if count != 0: mean = [value / count for value in mean] x /= count y /= count outFeat = QgsFeature()
fields = provider.fields() writers = {} # Fields are defined by their names, but QGIS needs the index for the attributes map class_field_index = layer.fieldNameIndex(class_field) inFeat = QgsFeature() outFeat = QgsFeature() inGeom = QgsGeometry() nElement = 0 writers = {} feats = sextante.getfeatures(layer) nFeat = len(feats) for inFeat in feats: progress.setPercentage(int((100 * nElement) / nFeat)) nElement += 1 atMap = inFeat.attributes() clazz = atMap[class_field_index].toString() if clazz not in writers: outputFile = output + "_" + str(len(writers)) + ".shp" writers[clazz] = SextanteVectorWriter(outputFile, None, fields, provider.geometryType(), layer.crs()) inGeom = inFeat.geometry() outFeat.setGeometry(inGeom) outFeat.setAttributes(atMap) writers[clazz].addFeature(outFeat) for writer in writers.values(): del writer