Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] remove Z/M addition from "random" algorithms
  • Loading branch information
alexbruy committed Jul 24, 2017
1 parent c7645a3 commit c440ade
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 121 deletions.
32 changes: 2 additions & 30 deletions python/plugins/processing/algs/qgis/RandomPointsAlongLines.py
Expand Up @@ -34,7 +34,6 @@
QgsFeature,
QgsFields,
QgsGeometry,
QgsPoint,
QgsPointXY,
QgsWkbTypes,
QgsSpatialIndex,
Expand All @@ -44,7 +43,6 @@
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition)
Expand All @@ -58,8 +56,6 @@ class RandomPointsAlongLines(QgisAlgorithm):
INPUT = 'INPUT'
POINTS_NUMBER = 'POINTS_NUMBER'
MIN_DISTANCE = 'MIN_DISTANCE'
ADD_Z = 'ADD_Z'
ADD_M = 'ADD_M'
OUTPUT = 'OUTPUT'

def group(self):
Expand All @@ -80,17 +76,6 @@ def initAlgorithm(self, config=None):
self.tr('Minimum distance between points'),
QgsProcessingParameterNumber.Double,
0, False, 0, 1000000000))
params = []
params.append(QgsProcessingParameterBoolean(self.ADD_Z,
self.tr('Add Z coordinate'),
False))
params.append(QgsProcessingParameterBoolean(self.ADD_M,
self.tr('Add M coordinate'),
False))
for p in params:
p.setFlags(p.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(p)

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Random points'),
type=QgsProcessing.TypeVectorPoint))
Expand All @@ -105,20 +90,12 @@ def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
pointCount = self.parameterAsDouble(parameters, self.POINTS_NUMBER, context)
minDistance = self.parameterAsDouble(parameters, self.MIN_DISTANCE, context)
addZ = self.parameterAsBool(parameters, self.ADD_Z, context)
addM = self.parameterAsBool(parameters, self.ADD_M, context)

fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

wkbType = QgsWkbTypes.Point
if addZ:
wkbType = QgsWkbTypes.addZ(wkbType)
if addM:
wkbType = QgsWkbTypes.addM(wkbType)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkbType, source.sourceCrs())
fields, QgsWkbTypes.Point, source.sourceCrs())

nPoints = 0
nIterations = 0
Expand Down Expand Up @@ -170,13 +147,8 @@ def processAlgorithm(self, parameters, context, feedback):
ry = (startPoint.y() + d * endPoint.y()) / (1 + d)

# generate random point
pnt = QgsPoint(rx, ry)
p = QgsPointXY(rx, ry)
if addZ:
pnt.addZValue(0.0)
if addM:
pnt.addMValue(0.0)
geom = QgsGeometry(pnt)
geom = QgsGeometry.fromPoint(p)
if vector.checkMinDistance(p, index, minDistance, points):
f = QgsFeature(nPoints)
f.initAttributes(1)
Expand Down
33 changes: 2 additions & 31 deletions python/plugins/processing/algs/qgis/RandomPointsExtent.py
Expand Up @@ -36,7 +36,6 @@
QgsFeature,
QgsFields,
QgsGeometry,
QgsPoint,
QgsPointXY,
QgsWkbTypes,
QgsSpatialIndex,
Expand All @@ -45,7 +44,6 @@
QgsProcessingParameterExtent,
QgsProcessingParameterNumber,
QgsProcessingParameterCrs,
QgsProcessingParameterBoolean,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition)

Expand All @@ -61,8 +59,6 @@ class RandomPointsExtent(QgisAlgorithm):
POINTS_NUMBER = 'POINTS_NUMBER'
MIN_DISTANCE = 'MIN_DISTANCE'
TARGET_CRS = 'TARGET_CRS'
ADD_Z = 'ADD_Z'
ADD_M = 'ADD_M'
OUTPUT = 'OUTPUT'

def icon(self):
Expand All @@ -87,18 +83,6 @@ def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterCrs(self.TARGET_CRS,
self.tr('Target CRS'),
'ProjectCrs'))

params = []
params.append(QgsProcessingParameterBoolean(self.ADD_Z,
self.tr('Add Z coordinate'),
False))
params.append(QgsProcessingParameterBoolean(self.ADD_M,
self.tr('Add M coordinate'),
False))
for p in params:
p.setFlags(p.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(p)

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Random points'),
type=QgsProcessing.TypeVectorPoint))
Expand All @@ -114,22 +98,14 @@ def processAlgorithm(self, parameters, context, feedback):
pointCount = self.parameterAsDouble(parameters, self.POINTS_NUMBER, context)
minDistance = self.parameterAsDouble(parameters, self.MIN_DISTANCE, context)
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
addZ = self.parameterAsBool(parameters, self.ADD_Z, context)
addM = self.parameterAsBool(parameters, self.ADD_M, context)

extent = QgsGeometry().fromRect(bbox)

fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

wkbType = QgsWkbTypes.Point
if addZ:
wkbType = QgsWkbTypes.addZ(wkbType)
if addM:
wkbType = QgsWkbTypes.addM(wkbType)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkbType, crs)
fields, QgsWkbTypes.Point, crs)

nPoints = 0
nIterations = 0
Expand All @@ -148,13 +124,8 @@ def processAlgorithm(self, parameters, context, feedback):
rx = bbox.xMinimum() + bbox.width() * random.random()
ry = bbox.yMinimum() + bbox.height() * random.random()

pnt = QgsPoint(rx, ry)
p = QgsPointXY(rx, ry)
if addZ:
pnt.addZValue(0.0)
if addM:
pnt.addMValue(0.0)
geom = QgsGeometry(pnt)
geom = QgsGeometry.fromPoint(p)
if geom.within(extent) and \
vector.checkMinDistance(p, index, minDistance, points):
f = QgsFeature(nPoints)
Expand Down
32 changes: 2 additions & 30 deletions python/plugins/processing/algs/qgis/RandomPointsLayer.py
Expand Up @@ -35,15 +35,13 @@
QgsFeature,
QgsFields,
QgsGeometry,
QgsPoint,
QgsPointXY,
QgsWkbTypes,
QgsSpatialIndex,
QgsFeatureRequest,
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterDefinition)
Expand All @@ -59,8 +57,6 @@ class RandomPointsLayer(QgisAlgorithm):
INPUT = 'INPUT'
POINTS_NUMBER = 'POINTS_NUMBER'
MIN_DISTANCE = 'MIN_DISTANCE'
ADD_Z = 'ADD_Z'
ADD_M = 'ADD_M'
OUTPUT = 'OUTPUT'

def icon(self):
Expand All @@ -84,17 +80,6 @@ def initAlgorithm(self, config=None):
self.tr('Minimum distance between points'),
QgsProcessingParameterNumber.Double,
0, False, 0, 1000000000))
params = []
params.append(QgsProcessingParameterBoolean(self.ADD_Z,
self.tr('Add Z coordinate'),
False))
params.append(QgsProcessingParameterBoolean(self.ADD_M,
self.tr('Add M coordinate'),
False))
for p in params:
p.setFlags(p.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(p)

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Random points'),
type=QgsProcessing.TypeVectorPoint))
Expand All @@ -109,23 +94,15 @@ def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
pointCount = self.parameterAsDouble(parameters, self.POINTS_NUMBER, context)
minDistance = self.parameterAsDouble(parameters, self.MIN_DISTANCE, context)
addZ = self.parameterAsBool(parameters, self.ADD_Z, context)
addM = self.parameterAsBool(parameters, self.ADD_M, context)

bbox = source.sourceExtent()
sourceIndex = QgsSpatialIndex(source, feedback)

fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

wkbType = QgsWkbTypes.Point
if addZ:
wkbType = QgsWkbTypes.addZ(wkbType)
if addM:
wkbType = QgsWkbTypes.addM(wkbType)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkbType, source.sourceCrs())
fields, QgsWkbTypes.Point, source.sourceCrs())

nPoints = 0
nIterations = 0
Expand All @@ -144,13 +121,8 @@ def processAlgorithm(self, parameters, context, feedback):
rx = bbox.xMinimum() + bbox.width() * random.random()
ry = bbox.yMinimum() + bbox.height() * random.random()

pnt = QgsPoint(rx, ry)
p = QgsPointXY(rx, ry)
if addZ:
pnt.addZValue(0.0)
if addM:
pnt.addMValue(0.0)
geom = QgsGeometry(pnt)
geom = QgsGeometry.fromPoint(p)
ids = sourceIndex.intersects(geom.buffer(5, 5).boundingBox())
if len(ids) > 0 and \
vector.checkMinDistance(p, index, minDistance, points):
Expand Down
32 changes: 2 additions & 30 deletions python/plugins/processing/algs/qgis/RandomPointsPolygons.py
Expand Up @@ -35,7 +35,6 @@
QgsFeature,
QgsFields,
QgsGeometry,
QgsPoint,
QgsPointXY,
QgsWkbTypes,
QgsSpatialIndex,
Expand All @@ -46,7 +45,6 @@
QgsProcessing,
QgsProcessingException,
QgsProcessingParameterNumber,
QgsProcessingParameterBoolean,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingParameterExpression,
Expand All @@ -65,8 +63,6 @@ class RandomPointsPolygons(QgisAlgorithm):
EXPRESSION = 'EXPRESSION'
MIN_DISTANCE = 'MIN_DISTANCE'
STRATEGY = 'STRATEGY'
ADD_Z = 'ADD_Z'
ADD_M = 'ADD_M'
OUTPUT = 'OUTPUT'

def icon(self):
Expand Down Expand Up @@ -97,17 +93,6 @@ def initAlgorithm(self, config=None):
self.tr('Minimum distance between points'),
QgsProcessingParameterNumber.Double,
0, False, 0, 1000000000))
params = []
params.append(QgsProcessingParameterBoolean(self.ADD_Z,
self.tr('Add Z coordinate'),
False))
params.append(QgsProcessingParameterBoolean(self.ADD_M,
self.tr('Add M coordinate'),
False))
for p in params:
p.setFlags(p.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
self.addParameter(p)

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT,
self.tr('Random points'),
type=QgsProcessing.TypeVectorPoint))
Expand All @@ -122,8 +107,6 @@ def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.INPUT, context)
strategy = self.parameterAsEnum(parameters, self.STRATEGY, context)
minDistance = self.parameterAsDouble(parameters, self.MIN_DISTANCE, context)
addZ = self.parameterAsBool(parameters, self.ADD_Z, context)
addM = self.parameterAsBool(parameters, self.ADD_M, context)

expression = QgsExpression(self.parameterAsString(parameters, self.EXPRESSION, context))
if expression.hasParserError():
Expand All @@ -137,14 +120,8 @@ def processAlgorithm(self, parameters, context, feedback):
fields = QgsFields()
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

wkbType = QgsWkbTypes.Point
if addZ:
wkbType = QgsWkbTypes.addZ(wkbType)
if addM:
wkbType = QgsWkbTypes.addM(wkbType)

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
fields, wkbType, source.sourceCrs())
fields, QgsWkbTypes.Point, source.sourceCrs())

da = QgsDistanceArea()
da.setSourceCrs(source.sourceCrs())
Expand Down Expand Up @@ -190,13 +167,8 @@ def processAlgorithm(self, parameters, context, feedback):
rx = bbox.xMinimum() + bbox.width() * random.random()
ry = bbox.yMinimum() + bbox.height() * random.random()

pnt = QgsPoint(rx, ry)
p = QgsPointXY(rx, ry)
if addZ:
pnt.addZValue(0.0)
if addM:
pnt.addMValue(0.0)
geom = QgsGeometry(pnt)
geom = QgsGeometry.fromPoint(p)
if geom.within(fGeom) and \
vector.checkMinDistance(p, index, minDistance, points):
f = QgsFeature(nPoints)
Expand Down

0 comments on commit c440ade

Please sign in to comment.