Skip to content

Commit

Permalink
[Processing] Add precision parameter to by location algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-morvan committed Sep 29, 2015
1 parent a4151d7 commit fa12502
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 22 deletions.
13 changes: 10 additions & 3 deletions python/plugins/processing/algs/qgis/ExtractByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterGeometryPredicate
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector

Expand All @@ -38,6 +39,7 @@ class ExtractByLocation(GeoAlgorithm):
INPUT = 'INPUT'
INTERSECT = 'INTERSECT'
PREDICATE = 'PREDICATE'
PRECISION = 'PRECISION'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
Expand All @@ -52,6 +54,9 @@ def defineCharacteristics(self):
self.addParameter(ParameterGeometryPredicate(self.PREDICATE,
self.tr('Geometric predicate'),
left=self.INPUT, right=self.INTERSECT))
self.addParameter(ParameterNumber(self.PRECISION,
self.tr('Precision'),
0.0, None, 0.0))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Extracted (location)')))

def processAlgorithm(self, progress):
Expand All @@ -60,6 +65,7 @@ def processAlgorithm(self, progress):
filename = self.getParameterValue(self.INTERSECT)
selectLayer = dataobjects.getObjectFromUri(filename)
predicates = self.getParameterValue(self.PREDICATE)
precision = self.getParameterValue(self.PRECISION)

index = vector.spatialindex(layer)

Expand All @@ -79,12 +85,13 @@ def processAlgorithm(self, progress):
featureCount = len(features)
total = 100.0 / float(len(features))
for current, f in enumerate(features):
geom = QgsGeometry(f.geometry())
intersects = index.intersects(geom.boundingBox())
geom = vector.snapToPrecision(f.geometry(), precision)
bbox = vector.bufferedBoundingBox(geom.boundingBox(), 0.51 * precision)
intersects = index.intersects(bbox)
for i in intersects:
request = QgsFeatureRequest().setFilterFid(i)
feat = layer.getFeatures(request).next()
tmpGeom = QgsGeometry(feat.geometry())
tmpGeom = vector.snapToPrecision(feat.geometry(), precision)
res = False
for predicate in predicates:
if predicate == 'disjoint':
Expand Down
14 changes: 11 additions & 3 deletions python/plugins/processing/algs/qgis/SelectByLocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterGeometryPredicate
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector

Expand All @@ -39,6 +40,7 @@ class SelectByLocation(GeoAlgorithm):
INPUT = 'INPUT'
INTERSECT = 'INTERSECT'
PREDICATE = 'PREDICATE'
PRECISION = 'PRECISION'
METHOD = 'METHOD'
OUTPUT = 'OUTPUT'

Expand All @@ -59,6 +61,9 @@ def defineCharacteristics(self):
self.addParameter(ParameterGeometryPredicate(self.PREDICATE,
self.tr('Geometric predicate'),
left=self.INPUT, right=self.INTERSECT))
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))
Expand All @@ -71,6 +76,7 @@ def processAlgorithm(self, progress):
filename2 = self.getParameterValue(self.INTERSECT)
selectLayer = dataobjects.getObjectFromUri(filename2)
predicates = self.getParameterValue(self.PREDICATE)
precision = self.getParameterValue(self.PRECISION)

oldSelection = set(inputLayer.selectedFeaturesIds())
inputLayer.removeSelection()
Expand All @@ -87,13 +93,15 @@ def processAlgorithm(self, progress):
features = vector.features(selectLayer)
total = 100.0 / float(len(features))
for f in features:
geom = QgsGeometry(f.geometry())
geom = vector.snapToPrecision(f.geometry(), precision)
bbox = vector.bufferedBoundingBox(geom.boundingBox(), 0.51 * precision)
intersects = index.intersects(bbox)

intersects = index.intersects(geom.boundingBox())
for i in intersects:
request = QgsFeatureRequest().setFilterFid(i)
feat = inputLayer.getFeatures(request).next()
tmpGeom = QgsGeometry(feat.geometry())
tmpGeom = vector.snapToPrecision(feat.geometry(), precision)

res = False
for predicate in predicates:
if predicate == 'disjoint':
Expand Down
29 changes: 14 additions & 15 deletions python/plugins/processing/algs/qgis/SpatialJoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterGeometryPredicate
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterString
from processing.core.outputs import OutputVector
Expand All @@ -39,6 +40,7 @@ class SpatialJoin(GeoAlgorithm):
TARGET = "TARGET"
JOIN = "JOIN"
PREDICATE = "PREDICATE"
PRECISION = 'PRECISION'
SUMMARY = "SUMMARY"
STATS = "STATS"
KEEP = "KEEP"
Expand Down Expand Up @@ -70,6 +72,9 @@ def defineCharacteristics(self):
self.tr('Geometric predicate'),
left=self.TARGET, right=self.JOIN,
enabledPredicates=predicates))
self.addParameter(ParameterNumber(self.PRECISION,
self.tr('Precision'),
0.0, None, 0.0))
self.addParameter(ParameterSelection(self.SUMMARY,
self.tr('Attribute summary'), self.summarys))
self.addParameter(ParameterString(self.STATS,
Expand All @@ -85,6 +90,7 @@ def processAlgorithm(self, progress):
join = dataobjects.getObjectFromUri(
self.getParameterValue(self.JOIN))
predicates = self.getParameterValue(self.PREDICATE)
precision = self.getParameterValue(self.PRECISION)

summary = self.getParameterValue(self.SUMMARY) == 1
keep = self.getParameterValue(self.KEEP) == 1
Expand Down Expand Up @@ -140,29 +146,22 @@ def processAlgorithm(self, progress):
features = vector.features(target)
total = 100.0 / len(features)
for c, f in enumerate(features):
inGeom = f.geometry()
atMap1 = f.attributes()
outFeat.setGeometry(inGeom)
outFeat.setGeometry(f.geometry())
inGeom = vector.snapToPrecision(f.geometry(), precision)
none = True
joinList = []
if inGeom.type() == QGis.Point:
joinList = index.intersects(inGeom.buffer(10, 2).boundingBox())
if len(joinList) > 0:
check = 0
else:
check = 1
bbox = inGeom.buffer(10, 2).boundingBox()
else:
joinList = index.intersects(inGeom.boundingBox())
if len(joinList) > 0:
check = 0
else:
check = 1

if check == 0:
bbox = inGeom.boundingBox()
bufferedBox = vector.bufferedBoundingBox(bbox, 0.51 * precision)
joinList = index.intersects(bufferedBox)
if len(joinList) > 0:
count = 0
for i in joinList:
inFeatB = mapP2[i]
inGeomB = inFeatB.geometry()
inGeomB = vector.snapToPrecision(inFeatB.geometry(), precision)

res = False
for predicate in predicates:
Expand Down
29 changes: 28 additions & 1 deletion python/plugins/processing/tools/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import cStringIO

from PyQt4.QtCore import QVariant, QSettings
from qgis.core import QGis, QgsFields, QgsField, QgsSpatialIndex, QgsMapLayerRegistry, QgsMapLayer, QgsVectorLayer, QgsVectorFileWriter, QgsDistanceArea
from qgis.core import QGis, QgsFields, QgsField, QgsGeometry, QgsRectangle, QgsSpatialIndex, QgsMapLayerRegistry, QgsMapLayer, QgsVectorLayer, QgsVectorFileWriter, QgsDistanceArea
from processing.core.ProcessingConfig import ProcessingConfig


Expand Down Expand Up @@ -381,6 +381,33 @@ def _toQgsField(f):
return QgsField(f[0], TYPE_MAP.get(f[1], QVariant.String))


def snapToPrecision(geom, precision):
snapped = QgsGeometry(geom)
if precision == 0.0:
return snapped

i = 0
p = snapped.vertexAt(i)
while p.x() != 0.0 and p.y() != 0.0:
x = round(p.x() / precision,0) * precision
y = round(p.y() / precision,0) * precision
snapped.moveVertex(x,y,i)
i = i + 1
p = snapped.vertexAt(i)
return snapped


def bufferedBoundingBox(bbox, buffer_size):
if buffer_size == 0.0:
return QgsRectangle(bbox)

return QgsRectangle(
bbox.xMinimum() - buffer_size,
bbox.yMinimum() - buffer_size,
bbox.xMaximum() + buffer_size,
bbox.yMaximum() + buffer_size)


class VectorWriter:

MEMORY_LAYER_PREFIX = 'memory:'
Expand Down

0 comments on commit fa12502

Please sign in to comment.