QGIS/python/plugins/sextante/ftools/SelectByLocation.py

127 lines
4.9 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
SelectByLocation.py
---------------------
Date : August 2012
Copyright : (C) 2012 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__ = 'August 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
2012-09-15 18:25:25 +03:00
import os.path
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
from PyQt4 import QtGui
from PyQt4.QtCore import *
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
from qgis.core import *
2012-10-03 19:03:39 +03:00
from sextante.core.GeoAlgorithm import GeoAlgorithm
2012-09-15 18:25:25 +03:00
from sextante.core.QGisLayers import QGisLayers
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
from sextante.parameters.ParameterSelection import ParameterSelection
2012-10-03 19:03:39 +03:00
from sextante.parameters.ParameterVector import ParameterVector
2012-09-15 18:25:25 +03:00
from sextante.parameters.ParameterBoolean import ParameterBoolean
2012-10-03 19:03:39 +03:00
from sextante.outputs.OutputVector import OutputVector
from sextante.ftools import FToolsUtils as utils
2012-09-15 18:25:25 +03:00
class SelectByLocation(GeoAlgorithm):
INPUT = "INPUT"
INTERSECT = "INTERSECT"
METHOD = "METHOD"
USE_SELECTED = "USE_SELECTED"
OUTPUT = "OUTPUT"
2012-10-03 19:03:39 +03:00
METHODS = ["creating new selection",
"adding to current selection",
"removing from current selection"]
2012-09-15 18:25:25 +03:00
def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/select_location.png")
2012-10-03 19:03:39 +03:00
def defineCharacteristics(self):
self.name = "Select by location"
self.group = "Research tools"
self.addParameter(ParameterVector(self.INPUT, "Layer to select from", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(self.INTERSECT, "Additional layer (intersection layer)", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(self.METHOD, "Modify current selection by", self.METHODS, 0))
self.addParameter(ParameterBoolean(self.USE_SELECTED, "Use only selected features", False))
self.addOutput(OutputVector(self.OUTPUT, "Selection", True))
2012-09-15 18:25:25 +03:00
def processAlgorithm(self, progress):
2012-10-03 19:03:39 +03:00
filename = self.getParameterValue(self.INPUT)
inputLayer = QGisLayers.getObjectFromUri(filename)
2012-09-15 18:25:25 +03:00
method = self.getParameterValue(self.METHOD)
selection = self.getParameterValue(self.USE_SELECTED)
2012-10-03 19:03:39 +03:00
filename = self.getParameterValue(self.INTERSECT)
2012-09-15 18:25:25 +03:00
selectLayer = QGisLayers.getObjectFromUri(filename)
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
inputProvider = inputLayer.dataProvider()
selectProvider = selectLayer.dataProvider()
2012-10-03 19:03:39 +03:00
index = utils.createSpatialIndex(inputProvider)
inputProvider.select()
selectProvider.select()
2012-09-15 18:25:25 +03:00
feat = QgsFeature()
infeat = QgsFeature()
geom = QgsGeometry()
selectedSet = []
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
if selection:
features = selectLayer.selectedFeatures()
2012-10-03 19:03:39 +03:00
total = 100.0 / float(len(features))
current = 0
2012-09-15 18:25:25 +03:00
for feat in features:
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
2012-10-03 19:03:39 +03:00
for i in intersects:
inputProvider.featureAtId(i, infeat, True)
2012-09-15 18:25:25 +03:00
tmpGeom = QgsGeometry(infeat.geometry())
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
2012-10-03 19:03:39 +03:00
current += 1
progress.setPercentage(int(current * total))
2012-09-15 18:25:25 +03:00
else:
2012-10-03 19:03:39 +03:00
total = 100.0 / float(selectProvider.featureCount())
current = 0
2012-09-15 18:25:25 +03:00
while selectProvider.nextFeature(feat):
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
2012-10-03 19:03:39 +03:00
for i in intersects:
inputProvider.featureAtId(i, infeat, True)
2012-09-15 18:25:25 +03:00
tmpGeom = QgsGeometry( infeat.geometry() )
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
2012-10-03 19:03:39 +03:00
current += 1
progress.setPercentage(int(current * total))
2012-09-15 18:25:25 +03:00
if method == 1:
selectedSet = list(set(inputLayer.selectedFeaturesIds()).union(selectedSet))
elif method == 2:
selectedSet = list(set(inputLayer.selectedFeaturesIds()).difference(selectedSet))
2012-10-03 19:03:39 +03:00
2012-09-15 18:25:25 +03:00
inputLayer.setSelectedFeatures(selectedSet)
self.setOutputValue(self.OUTPUT, filename)