|
| 1 | +from sextante.core.GeoAlgorithm import GeoAlgorithm |
| 2 | +from sextante.outputs.OutputVector import OutputVector |
| 3 | +from sextante.parameters.ParameterVector import ParameterVector |
| 4 | +from qgis.core import * |
| 5 | +from PyQt4.QtCore import * |
| 6 | +from PyQt4.QtGui import * |
| 7 | +from sextante.core.QGisLayers import QGisLayers |
| 8 | + |
| 9 | + |
| 10 | +class SaveSelectedFeatures(GeoAlgorithm): |
| 11 | + '''This is an example algorithm that takes a vector layer and creates |
| 12 | + a new one just with just those features of the input layer that are |
| 13 | + selected. |
| 14 | + It is meant to be used as an example of how to create your own SEXTANTE |
| 15 | + algorithms and explain methods and variables used to do it. |
| 16 | + An algorithm like this will be available in all SEXTANTE elements, and |
| 17 | + there is not need for additional work. |
| 18 | +
|
| 19 | + All SEXTANTE algorithms should extend the GeoAlgorithm class''' |
| 20 | + |
| 21 | + #constants used to refer to parameters and outputs. |
| 22 | + #They will be used when calling the algorithm from another algorithm, |
| 23 | + #or when calling SEXTANTE from the QGIS console. |
| 24 | + OUTPUT_LAYER = "OUTPUT_LAYER" |
| 25 | + INPUT_LAYER = "INPUT_LAYER" |
| 26 | + |
| 27 | + def defineCharacteristics(self): |
| 28 | + '''Here we define the inputs and output of the algorithm, along |
| 29 | + with some other properties''' |
| 30 | + |
| 31 | + #the name that the user will see in the toolbox |
| 32 | + self.name = "Create new layer with selected features" |
| 33 | + |
| 34 | + #the branch of the toolbox under which the algorithm will appear |
| 35 | + self.group = "Algorithms for vector layers" |
| 36 | + |
| 37 | + #we add the input vector layer. It can have any kind of geometry |
| 38 | + #It is a mandatory (not optional) one, hence the False argument |
| 39 | + self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_ANY, False)) |
| 40 | + # we add a vector layer as output |
| 41 | + self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer with selected features")) |
| 42 | + |
| 43 | + |
| 44 | + def processAlgorithm(self, progress): |
| 45 | + '''Here is where the processing itself takes place''' |
| 46 | + |
| 47 | + #the first thing to do is retrieve the values of the parameters |
| 48 | + #entered by the user |
| 49 | + inputFilename = self.getParameterValue(self.INPUT_LAYER) |
| 50 | + output = self.getOutputValue(self.OUTPUT_LAYER) |
| 51 | + |
| 52 | + #input layers values are always a string with its location. |
| 53 | + #That string can be converted into a QGIS object (a QgsVectorLayer in this case)) |
| 54 | + #using the Sextante.getObject() method |
| 55 | + vectorLayer = QGisLayers.getObjectFromUri(inputFilename) |
| 56 | + |
| 57 | + #And now we can process |
| 58 | + |
| 59 | + #First we create the output layer. |
| 60 | + #The output value entered by the user is a string containing a filename, |
| 61 | + #so we can use it directly |
| 62 | + settings = QSettings() |
| 63 | + systemEncoding = settings.value( "/UI/encoding", "System" ).toString() |
| 64 | + provider = vectorLayer.dataProvider() |
| 65 | + writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() ) |
| 66 | + |
| 67 | + #Now we take the selected features and add them to the output layer |
| 68 | + selection = vectorLayer.selectedFeatures() |
| 69 | + for feat in selection: |
| 70 | + writer.addFeature(feat) |
| 71 | + del writer |
| 72 | + |
| 73 | + #There is nothing more to do here. We do not have to open the layer that we have created. |
| 74 | + #SEXTANTE will take care of that, or will handle it if this algorithm is executed within |
| 75 | + #a complex model |
0 commit comments