2013-08-12 20:44:27 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
__init__.py
|
|
|
|
---------------------
|
|
|
|
Date : July 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__ = 'July 2013'
|
|
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-08-12 20:44:27 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
__revision__ = '$Format:%H$'
|
2013-08-12 20:44:27 +02:00
|
|
|
|
|
|
|
from PyQt4.QtCore import *
|
|
|
|
from PyQt4.QtGui import *
|
|
|
|
from qgis.core import *
|
|
|
|
|
|
|
|
from processing.core.Processing import Processing
|
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
|
|
|
from processing.parameters.ParameterVector import ParameterVector
|
|
|
|
from processing.outputs.OutputVector import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
2013-08-12 20:44:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ExampleAlgorithm(GeoAlgorithm):
|
2013-10-01 20:52:22 +03:00
|
|
|
"""This is an example algorithm that takes a vector layer and
|
|
|
|
creates a new one just with just those features of the input
|
|
|
|
layer that are selected.
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-08-25 10:45:52 +02:00
|
|
|
It is meant to be used as an example of how to create your own
|
2013-10-01 20:52:22 +03:00
|
|
|
algorithms and explain methods and variables used to do it. An
|
|
|
|
algorithm like this will be available in all elements, and there
|
|
|
|
is not need for additional work.
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
All Processing algorithms should extend the GeoAlgorithm class.
|
|
|
|
"""
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# Constants used to refer to parameters and outputs. They will be
|
|
|
|
# used when calling the algorithm from another algorithm, or when
|
|
|
|
# calling from the QGIS console.
|
|
|
|
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
2013-08-12 20:44:27 +02:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
"""Here we define the inputs and output of the algorithm, along
|
|
|
|
with some other properties.
|
|
|
|
"""
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# The name that the user will see in the toolbox
|
|
|
|
self.name = 'Create copy of layer'
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# The branch of the toolbox under which the algorithm will appear
|
|
|
|
self.group = 'Algorithms for vector layers'
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# We add the input vector layer. It can have any kind of geometry
|
2013-08-12 20:44:27 +02:00
|
|
|
# It is a mandatory (not optional) one, hence the False argument
|
2013-10-01 20:52:22 +03:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER, 'Input layer',
|
|
|
|
[ParameterVector.VECTOR_TYPE_ANY], False))
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# We add a vector layer as output
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER,
|
|
|
|
'Output layer with selected features'))
|
2013-08-12 20:44:27 +02:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
"""Here is where the processing itself takes place."""
|
2013-08-12 20:44:27 +02:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# The first thing to do is retrieve the values of the parameters
|
2013-08-12 20:44:27 +02:00
|
|
|
# entered by the user
|
|
|
|
inputFilename = self.getParameterValue(self.INPUT_LAYER)
|
|
|
|
output = self.getOutputValue(self.OUTPUT_LAYER)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# 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
|
|
|
|
# processing.getObjectFromUri() method.
|
2013-09-12 13:19:00 +02:00
|
|
|
vectorLayer = dataobjects.getObjectFromUri(inputFilename)
|
2013-08-12 20:44:27 +02:00
|
|
|
|
|
|
|
# And now we can process
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# 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
|
2013-08-12 20:44:27 +02:00
|
|
|
settings = QSettings()
|
2013-10-01 20:52:22 +03:00
|
|
|
systemEncoding = settings.value('/UI/encoding', 'System')
|
2013-08-12 20:44:27 +02:00
|
|
|
provider = vectorLayer.dataProvider()
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = QgsVectorFileWriter(output, systemEncoding,
|
2013-08-12 20:44:27 +02:00
|
|
|
provider.fields(),
|
2013-10-01 20:52:22 +03:00
|
|
|
provider.geometryType(), provider.crs())
|
|
|
|
|
|
|
|
# Now we take the features from input layer and add them to the
|
|
|
|
# output. Method features() returns an iterator, considering the
|
|
|
|
# selection that might exist in layer and the configuration that
|
|
|
|
# indicates should algorithm use only selected features or all
|
|
|
|
# of them
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(vectorLayer)
|
2013-08-12 20:44:27 +02:00
|
|
|
for f in features:
|
|
|
|
writer.addFeature(f)
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# There is nothing more to do here. We do not have to open the
|
|
|
|
# layer that we have created. The framework will take care of
|
|
|
|
# that, or will handle it if this algorithm is executed within
|
|
|
|
# a complex model
|