2013-10-07 18:41:07 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ExtractByLocation.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$'
|
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsFeatureRequest,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
|
|
|
QgsProcessingUtils)
|
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 ParameterVector
|
2016-11-06 12:08:29 +01:00
|
|
|
from processing.core.parameters import ParameterSelection
|
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-07 18:41:07 +02:00
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class ExtractByLocation(QgisAlgorithm):
|
2013-10-07 18:41:07 +02:00
|
|
|
|
|
|
|
INPUT = 'INPUT'
|
2013-10-24 15:26:39 +02:00
|
|
|
INTERSECT = 'INTERSECT'
|
2015-01-09 17:23:49 +01:00
|
|
|
PREDICATE = 'PREDICATE'
|
2015-09-29 14:24:17 +02:00
|
|
|
PRECISION = 'PRECISION'
|
2013-10-07 18:41:07 +02:00
|
|
|
OUTPUT = 'OUTPUT'
|
2014-07-02 07:46:03 +02:00
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('extract,filter,location,intersects,contains,within').split(',')
|
|
|
|
|
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:29 +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-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Layer to select from')))
|
2013-10-07 18:41:07 +02: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:29 +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))
|
2015-05-22 16:09:55 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (location)')))
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'extractbylocation'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Extract by location')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2013-10-07 18:41:07 +02:00
|
|
|
filename = self.getParameterValue(self.INPUT)
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(filename, context)
|
2013-10-07 18:41:07 +02:00
|
|
|
filename = self.getParameterValue(self.INTERSECT)
|
2017-05-02 13:40:49 +10:00
|
|
|
selectLayer = QgsProcessingUtils.mapLayerFromString(filename, 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)
|
2014-07-02 07:46:03 +02:00
|
|
|
|
2017-05-02 13:39:36 +10:00
|
|
|
index = QgsProcessingUtils.createSpatialIndex(layer, context)
|
2014-06-13 09:03:15 +02:00
|
|
|
|
2014-05-28 09:14:59 +02:00
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
2017-04-26 14:33:53 +10:00
|
|
|
writer = output.getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(), context)
|
2013-10-24 15:26:39 +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(layer, context):
|
2015-01-09 17:23:49 +01:00
|
|
|
disjoinSet.append(feat.id())
|
|
|
|
|
2013-10-07 18:41:07 +02: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
|
2015-01-09 17:23:49 +01: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)
|
2016-10-17 10:30:55 +10:00
|
|
|
request = QgsFeatureRequest().setFilterFids(intersects).setSubsetOfAttributes([])
|
|
|
|
for feat in layer.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:29 +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))
|
2013-10-07 18:41:07 +02:00
|
|
|
|
2015-01-09 17:23:49 +01:00
|
|
|
if 'disjoint' in predicates:
|
|
|
|
selectedSet = selectedSet + disjoinSet
|
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2014-05-28 09:14:59 +02:00
|
|
|
if f.id() in selectedSet:
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.addFeature(f, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2013-10-07 18:41:07 +02:00
|
|
|
del writer
|