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