QGIS/python/plugins/processing/algs/qgis/SelectByLocation.py

155 lines
6.2 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'
2012-10-04 19:33:47 +02:00
# This will get replaced with a git SHA1 when you do a git archive
2012-10-04 19:33:47 +02:00
__revision__ = '$Format:%H$'
import os
2016-04-22 10:38:48 +02:00
from qgis.PyQt.QtGui import QIcon
from qgis.core import QgsGeometry, QgsFeatureRequest, QgsProcessingUtils
2017-06-06 13:41:42 +10:00
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
2017-05-02 14:47:58 +10:00
from processing.tools import vector
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
class SelectByLocation(QgisAlgorithm):
2012-09-15 18:25:25 +03:00
INPUT = 'INPUT'
INTERSECT = 'INTERSECT'
PREDICATE = 'PREDICATE'
PRECISION = 'PRECISION'
METHOD = 'METHOD'
OUTPUT = 'OUTPUT'
2012-09-15 18:25:25 +03:00
def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'select_location.png'))
def group(self):
return self.tr('Vector selection tools')
def __init__(self):
super().__init__()
self.predicates = (
('intersects', self.tr('intersects')),
('contains', self.tr('contains')),
('disjoint', self.tr('disjoint')),
('equals', self.tr('equals')),
('touches', self.tr('touches')),
('overlaps', self.tr('overlaps')),
('within', self.tr('within')),
('crosses', self.tr('crosses')))
self.methods = [self.tr('creating new selection'),
self.tr('adding to current selection'),
self.tr('removing from current selection')]
self.addParameter(ParameterVector(self.INPUT,
self.tr('Layer to select from')))
self.addParameter(ParameterVector(self.INTERSECT,
self.tr('Additional layer (intersection layer)')))
self.addParameter(ParameterSelection(self.PREDICATE,
self.tr('Geometric predicate'),
self.predicates,
multiple=True))
self.addParameter(ParameterNumber(self.PRECISION,
self.tr('Precision'),
0.0, None, 0.0))
self.addParameter(ParameterSelection(self.METHOD,
self.tr('Modify current selection by'),
self.methods, 0))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Selected (location)'), True))
2012-10-03 19:03:39 +03:00
def name(self):
return 'selectbylocation'
def displayName(self):
return self.tr('Select by location')
def processAlgorithm(self, parameters, context, feedback):
2012-10-03 19:03:39 +03:00
filename = self.getParameterValue(self.INPUT)
inputLayer = QgsProcessingUtils.mapLayerFromString(filename, context)
2012-09-15 18:25:25 +03:00
method = self.getParameterValue(self.METHOD)
filename2 = self.getParameterValue(self.INTERSECT)
selectLayer = QgsProcessingUtils.mapLayerFromString(filename2, context)
predicates = self.getParameterValue(self.PREDICATE)
precision = self.getParameterValue(self.PRECISION)
2013-02-03 10:26:43 +01:00
oldSelection = set(inputLayer.selectedFeatureIds())
inputLayer.removeSelection()
index = QgsProcessingUtils.createSpatialIndex(inputLayer, context)
2014-07-02 07:46:03 +02:00
if 'disjoint' in predicates:
disjoinSet = []
for feat in QgsProcessingUtils.getFeatures(inputLayer, context):
disjoinSet.append(feat.id())
2012-09-15 18:25:25 +03:00
geom = QgsGeometry()
2013-02-28 22:08:32 +01:00
selectedSet = []
features = QgsProcessingUtils.getFeatures(selectLayer, context)
total = 100.0 / selectLayer.featureCount() if selectLayer.featureCount() else 0
for current, f in enumerate(features):
geom = vector.snapToPrecision(f.geometry(), precision)
bbox = geom.boundingBox()
bbox.grow(0.51 * precision)
intersects = index.intersects(bbox)
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
for feat in inputLayer.getFeatures(request):
tmpGeom = vector.snapToPrecision(feat.geometry(), precision)
res = False
for predicate in predicates:
if predicate == 'disjoint':
if tmpGeom.intersects(geom):
try:
disjoinSet.remove(feat.id())
except:
pass # already removed
else:
res = getattr(tmpGeom, predicate)(geom)
if res:
selectedSet.append(feat.id())
break
feedback.setProgress(int(current * total))
2012-10-03 19:03:39 +03:00
if 'disjoint' in predicates:
selectedSet = selectedSet + disjoinSet
2012-09-15 18:25:25 +03:00
if method == 1:
2013-02-03 10:26:43 +01:00
selectedSet = list(oldSelection.union(selectedSet))
2012-09-15 18:25:25 +03:00
elif method == 2:
2013-02-03 10:26:43 +01:00
selectedSet = list(oldSelection.difference(selectedSet))
2012-10-03 19:03:39 +03:00
inputLayer.selectByIds(selectedSet)
2012-09-15 18:25:25 +03:00
self.setOutputValue(self.OUTPUT, filename)