Skip to content

Commit ae4a144

Browse files
committedNov 1, 2014
[processing] add Regular points algorithm (addresses #5953)
1 parent b862420 commit ae4a144

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
 

‎python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from PyQt4.QtGui import *
3232

3333
from processing.core.AlgorithmProvider import AlgorithmProvider
34+
from ftools.RegularPoints import RegularPoints
3435

3536
from RandomExtract import RandomExtract
3637
from RandomExtractWithinSubsets import RandomExtractWithinSubsets
@@ -145,7 +146,8 @@ def __init__(self):
145146
RandomSelection(), RandomSelectionWithinSubsets(),
146147
SelectByLocation(), RandomExtract(),
147148
RandomExtractWithinSubsets(), ExtractByLocation(),
148-
SpatialJoin(), DeleteColumn(),
149+
SpatialJoin(), RegularPoints(),
150+
DeleteColumn(),
149151
DeleteDuplicateGeometries(), TextToFloat(),
150152
ExtractByAttribute(), SelectByAttribute(), Grid(),
151153
Gridify(), HubDistance(), HubLines(), Merge(),
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
RegularPoints.py
6+
---------------------
7+
Date : September 2014
8+
Copyright : (C) 2014 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Alexander Bruy'
21+
__date__ = 'September 2014'
22+
__copyright__ = '(C) 2014, Alexander Bruy'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from random import *
29+
30+
from PyQt4.QtCore import *
31+
from qgis.core import *
32+
from qgis.utils import iface
33+
34+
from processing.core.GeoAlgorithm import GeoAlgorithm
35+
from processing.core.parameters import ParameterExtent
36+
from processing.core.parameters import ParameterNumber
37+
from processing.core.parameters import ParameterBoolean
38+
from processing.core.outputs import OutputVector
39+
from processing.tools import vector
40+
41+
42+
class RegularPoints(GeoAlgorithm):
43+
44+
EXTENT = 'EXTENT'
45+
SPACING = 'SPACING'
46+
INSET = 'INSET'
47+
RANDOMIZE = 'RANDOMIZE'
48+
IS_SPACING = 'IS_SPACING'
49+
OUTPUT = 'OUTPUT'
50+
51+
def defineCharacteristics(self):
52+
self.name = 'Regular points'
53+
self.group = 'Vector creation tools'
54+
55+
self.addParameter(ParameterExtent(self.EXTENT, 'Input extent'))
56+
self.addParameter(ParameterNumber(self.SPACING,
57+
'Point spacing/count', 0.0001, 999999999.999999999, 0.0001))
58+
self.addParameter(ParameterNumber(self.INSET,
59+
'Initial inset from corner (LH side)', 0.0, 9999.9999, 0.0))
60+
self.addParameter(ParameterBoolean(self.RANDOMIZE,
61+
'Apply random offset to point spacing', False))
62+
self.addParameter(ParameterBoolean(self.IS_SPACING,
63+
'Use point spacing', True))
64+
self.addOutput(OutputVector(self.OUTPUT, 'Regular points'))
65+
66+
def processAlgorithm(self, progress):
67+
extent = str(self.getParameterValue(self.EXTENT)).split(',')
68+
69+
spacing = float(self.getParameterValue(self.SPACING))
70+
inset = float(self.getParameterValue(self.INSET))
71+
randomize = self.getParameterValue(self.RANDOMIZE)
72+
isSpacing = self.getParameterValue(self.IS_SPACING)
73+
74+
extent = QgsRectangle(float(extent[0]), float(extent[2]),
75+
float(extent[1]), float(extent[3]))
76+
77+
fields = QgsFields()
78+
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
79+
mapCRS = iface.mapCanvas().mapSettings().destinationCrs()
80+
81+
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
82+
fields, QGis.WKBPoint, mapCRS)
83+
84+
if randomize:
85+
seed()
86+
87+
area = extent.width() * extent.height()
88+
if isSpacing:
89+
pSpacing = spacing
90+
else:
91+
pSpacing = sqrt(area / value)
92+
93+
f = QgsFeature()
94+
f.initAttributes(1)
95+
f.setFields(fields)
96+
97+
count = 0
98+
total = 100.00 / (area / pSpacing)
99+
y = extent.yMaximum() - inset
100+
while y >= extent.yMinimum():
101+
x = extent.xMinimum() + inset
102+
while x <= extent.xMaximum():
103+
if randomize:
104+
geom = QgsGeometry().fromPoint(QgsPoint(
105+
uniform(x - (pSpacing / 2.0), x + (pSpacing / 2.0)),
106+
uniform(y - (pSpacing / 2.0), y + (pSpacing / 2.0))))
107+
else:
108+
geom = QgsGeometry().fromPoint(QgsPoint(x, y))
109+
110+
if geom.intersects(extent):
111+
f.setAttribute('id', count)
112+
f.setGeometry(geom)
113+
writer.addFeature(f)
114+
x += pSpacing
115+
count += 1
116+
progress.setPercentage(int(count* total))
117+
y = y - pSpacing
118+
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.