Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3725 from arnaud-morvan/processing_geometry_predi…
…cate

[processing] Remove parameter geometry predicate
  • Loading branch information
volaya committed Dec 13, 2016
2 parents 9f26574 + 322a565 commit 0035c97
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 246 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -426,7 +426,7 @@ def processCommand(self):
command += ' ' + param.name
elif isinstance(param, ParameterSelection):
idx = int(param.value)
command += ' ' + param.name + '=' + str(param.options[idx])
command += ' ' + param.name + '=' + str(param.options[idx][1])
elif isinstance(param, ParameterString):
command += ' ' + param.name + '="' + str(param.value) + '"'
elif isinstance(param, ParameterPoint):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/otb/OTBAlgorithm.py
Expand Up @@ -276,7 +276,7 @@ def processAlgorithm(self, progress):
elif isinstance(param, ParameterSelection):
commands.append(param.name)
idx = int(param.value)
commands.append(str(param.options[idx]))
commands.append(str(param.options[idx][1]))
elif isinstance(param, ParameterBoolean):
if param.value:
commands.append(param.name)
Expand Down
34 changes: 16 additions & 18 deletions python/plugins/processing/algs/qgis/ExtractByLocation.py
Expand Up @@ -29,7 +29,7 @@
from qgis.core import QgsFeatureRequest
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterGeometryPredicate
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
from processing.tools import dataobjects, vector
Expand All @@ -48,13 +48,24 @@ def defineCharacteristics(self):
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')
self.tags = self.tr('extract,filter,location,intersects,contains,within')

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.addParameter(ParameterVector(self.INPUT,
self.tr('Layer to select from')))
self.addParameter(ParameterVector(self.INTERSECT,
self.tr('Additional layer (intersection layer)')))
self.addParameter(ParameterGeometryPredicate(self.PREDICATE,
self.tr('Geometric predicate'),
left=self.INPUT, right=self.INTERSECT))
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))
Expand Down Expand Up @@ -98,20 +109,7 @@ def processAlgorithm(self, progress):
except:
pass # already removed
else:
if predicate == 'intersects':
res = tmpGeom.intersects(geom)
elif predicate == 'contains':
res = tmpGeom.contains(geom)
elif predicate == 'equals':
res = tmpGeom.equals(geom)
elif predicate == 'touches':
res = tmpGeom.touches(geom)
elif predicate == 'overlaps':
res = tmpGeom.overlaps(geom)
elif predicate == 'within':
res = tmpGeom.within(geom)
elif predicate == 'crosses':
res = tmpGeom.crosses(geom)
res = getattr(tmpGeom, predicate)(geom)
if res:
selectedSet.append(feat.id())
break
Expand Down
33 changes: 15 additions & 18 deletions python/plugins/processing/algs/qgis/SelectByLocation.py
Expand Up @@ -35,7 +35,6 @@
from processing.core.GeoAlgorithm import GeoAlgorithm
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 @@ -59,6 +58,16 @@ def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Select by location')
self.group, self.i18n_group = self.trAlgorithm('Vector selection tools')

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')]
Expand All @@ -67,9 +76,10 @@ def defineCharacteristics(self):
self.tr('Layer to select from')))
self.addParameter(ParameterVector(self.INTERSECT,
self.tr('Additional layer (intersection layer)')))
self.addParameter(ParameterGeometryPredicate(self.PREDICATE,
self.tr('Geometric predicate'),
left=self.INPUT, right=self.INTERSECT))
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))
Expand Down Expand Up @@ -118,20 +128,7 @@ def processAlgorithm(self, progress):
except:
pass # already removed
else:
if predicate == 'intersects':
res = tmpGeom.intersects(geom)
elif predicate == 'contains':
res = tmpGeom.contains(geom)
elif predicate == 'equals':
res = tmpGeom.equals(geom)
elif predicate == 'touches':
res = tmpGeom.touches(geom)
elif predicate == 'overlaps':
res = tmpGeom.overlaps(geom)
elif predicate == 'within':
res = tmpGeom.within(geom)
elif predicate == 'crosses':
res = tmpGeom.crosses(geom)
res = getattr(tmpGeom, predicate)(geom)
if res:
selectedSet.append(feat.id())
break
Expand Down
35 changes: 14 additions & 21 deletions python/plugins/processing/algs/qgis/SpatialJoin.py
Expand Up @@ -37,7 +37,6 @@

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
Expand All @@ -64,6 +63,15 @@ def defineCharacteristics(self):
self.name, self.i18n_name = self.trAlgorithm('Join attributes by location')
self.group, self.i18n_group = self.trAlgorithm('Vector general tools')

self.predicates = (
('intersects', self.tr('intersects')),
('contains', self.tr('contains')),
('equals', self.tr('equals')),
('touches', self.tr('touches')),
('overlaps', self.tr('overlaps')),
('within', self.tr('within')),
('crosses', self.tr('crosses')))

self.summarys = [
self.tr('Take attributes of the first located feature'),
self.tr('Take summary of intersecting features')
Expand All @@ -78,12 +86,10 @@ def defineCharacteristics(self):
self.tr('Target vector layer')))
self.addParameter(ParameterVector(self.JOIN,
self.tr('Join vector layer')))
predicates = list(ParameterGeometryPredicate.predicates)
predicates.remove('disjoint')
self.addParameter(ParameterGeometryPredicate(self.PREDICATE,
self.tr('Geometric predicate'),
left=self.TARGET, right=self.JOIN,
enabledPredicates=predicates))
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))
Expand Down Expand Up @@ -174,20 +180,7 @@ def processAlgorithm(self, progress):

res = False
for predicate in predicates:
if predicate == 'intersects':
res = inGeom.intersects(inGeomB)
elif predicate == 'contains':
res = inGeom.contains(inGeomB)
elif predicate == 'equals':
res = inGeom.equals(inGeomB)
elif predicate == 'touches':
res = inGeom.touches(inGeomB)
elif predicate == 'overlaps':
res = inGeom.overlaps(inGeomB)
elif predicate == 'within':
res = inGeom.within(inGeomB)
elif predicate == 'crosses':
res = inGeom.crosses(inGeomB)
res= getattr(inGeom, predicate)(inGeomB)
if res:
break

Expand Down
79 changes: 28 additions & 51 deletions python/plugins/processing/core/parameters.py
Expand Up @@ -1063,41 +1063,58 @@ def __init__(self, name='', description='', options=[], default=None, isSource=F
elif isinstance(self.options, str):
self.options = self.options.split(";")

# compute options as (value, text)
options = []
for i, option in enumerate(self.options):
if option is None or isinstance(option, basestring):
options.append((i, option))
else:
options.append((option[0], option[1]))
self.options = options
self.values = [option[0] for option in options]

self.value = None
if default is not None:
try:
self.default = int(default)
except:
self.default = 0
self.value = self.default
self.setValue(self.default)

def setValue(self, value):
if value is None:
if not self.optional:
return False
self.value = 0
self.value = None
return True

if isinstance(value, list):
if not self.multiple:
return False
values = []
for v in value:
if v in self.values:
values.append(v)
continue
try:
n = int(v)
values.append(n)
v = int(v)
except:
pass
if not v in self.values:
return False
values.append(v)
if not self.optional and len(values) == 0:
return False
self.value = values
return True
else:
try:
n = int(value)
self.value = n
if value in self.values:
self.value = value
return True
try:
value = int(value)
except:
pass
if not value in self.values:
return False
self.value = value
return True

@classmethod
def fromScriptCode(self, line):
Expand Down Expand Up @@ -1510,46 +1527,6 @@ def fromScriptCode(self, line):
[dataobjects.TYPE_VECTOR_POLYGON], isOptional)


class ParameterGeometryPredicate(Parameter):

predicates = ('intersects',
'contains',
'disjoint',
'equals',
'touches',
'overlaps',
'within',
'crosses')

def __init__(self, name='', description='', left=None, right=None,
optional=False, enabledPredicates=None):
Parameter.__init__(self, name, description, None, optional)
self.left = left
self.right = right
self.value = None
self.enabledPredicates = enabledPredicates
if self.enabledPredicates is None:
self.enabledPredicates = self.predicates

def getValueAsCommandLineParameter(self):
return str(self.value)

def setValue(self, value):
if value is None:
if not self.optional:
return False
self.value = None
return True
elif len(value) == 0 and not self.optional:
return False

if isinstance(value, str):
self.value = value.split(';') # relates to ModelerAlgorithm.resolveValue
else:
self.value = value
return True


paramClasses = [c for c in list(sys.modules[__name__].__dict__.values()) if isclass(c) and issubclass(c, Parameter)]


Expand Down
2 changes: 0 additions & 2 deletions python/plugins/processing/gui/BatchPanel.py
Expand Up @@ -38,7 +38,6 @@
from qgis.gui import QgsMessageBar

from processing.gui.BatchOutputSelectionPanel import BatchOutputSelectionPanel
from processing.gui.GeometryPredicateSelectionPanel import GeometryPredicateSelectionPanel

from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterRaster
Expand All @@ -50,7 +49,6 @@
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterFixedTable
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterGeometryPredicate

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
Expand Down

0 comments on commit 0035c97

Please sign in to comment.