-
Notifications
You must be signed in to change notification settings - Fork 0
/
createStar.py
299 lines (265 loc) · 11.5 KB
/
createStar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
import os
from geographiclib.geodesic import Geodesic
from qgis.core import (
QgsPointXY, QgsGeometry, QgsField, QgsPropertyDefinition,
QgsProject, QgsWkbTypes, QgsCoordinateTransform)
from qgis.core import (
QgsProcessing,
QgsProcessingFeatureBasedAlgorithm,
QgsProcessingParameters,
QgsProcessingParameterBoolean,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum)
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant, QUrl
from .settings import settings, epsg4326, geod
from .utils import tr, conversionToMeters, makeIdlCrossingsPositive, DISTANCE_LABELS
# import traceback
SHAPE_TYPE = [tr("Polygon"), tr("Line")]
class CreateStarAlgorithm(QgsProcessingFeatureBasedAlgorithm):
"""
Algorithm to create a star shape.
"""
PrmShapeType = 'ShapeType'
PrmStarPoints = 'StarPoints'
PrmOuterRadius = 'OuterRadius'
PrmInnerRadius = 'InnerRadius'
PrmStartingAngle = 'StartingAngle'
PrmUnitsOfMeasure = 'UnitsOfMeasure'
PrmExportInputGeometry = 'ExportInputGeometry'
def createInstance(self):
return CreateStarAlgorithm()
def name(self):
return 'createstar'
def icon(self):
return QIcon(os.path.join(os.path.dirname(__file__), 'images/star.png'))
def displayName(self):
return tr('Create star')
def group(self):
return tr('Geodesic shapes')
def groupId(self):
return 'vectorcreation'
def outputName(self):
return tr('Output layer')
def helpUrl(self):
file = os.path.dirname(__file__) + '/index.html'
if not os.path.exists(file):
return ''
return QUrl.fromLocalFile(file).toString(QUrl.FullyEncoded)
def inputLayerTypes(self):
return [QgsProcessing.TypeVectorPoint]
def outputWkbType(self, input_wkb_type):
if self.shape_type == 0:
return (QgsWkbTypes.Polygon)
return (QgsWkbTypes.LineString)
def outputFields(self, input_fields):
if self.export_geom:
name_x, name_y = settings.getGeomNames(input_fields.names())
input_fields.append(QgsField(name_x, QVariant.Double))
input_fields.append(QgsField(name_y, QVariant.Double))
return(input_fields)
def supportInPlaceEdit(self, layer):
return False
def initParameters(self, config=None):
self.shape_type = 0
self.export_geom = False
self.addParameter(
QgsProcessingParameterEnum(
self.PrmShapeType,
tr('Shape type'),
options=SHAPE_TYPE,
defaultValue=0,
optional=False)
)
param = QgsProcessingParameterNumber(
self.PrmStarPoints,
tr('Number of points on the star'),
QgsProcessingParameterNumber.Integer,
defaultValue=5,
minValue=3,
optional=False)
param.setIsDynamic(True)
param.setDynamicPropertyDefinition(QgsPropertyDefinition(
self.PrmStarPoints,
tr('Number of points on the star'),
QgsPropertyDefinition.Integer))
param.setDynamicLayerParameterName('INPUT')
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmOuterRadius,
tr('Outer radius'),
QgsProcessingParameterNumber.Double,
defaultValue=20.0,
minValue=0,
optional=False)
param.setIsDynamic(True)
param.setDynamicPropertyDefinition(QgsPropertyDefinition(
self.PrmOuterRadius,
tr('Outer radius'),
QgsPropertyDefinition.Double))
param.setDynamicLayerParameterName('INPUT')
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmInnerRadius,
tr('Inner radius'),
QgsProcessingParameterNumber.Double,
defaultValue=10.0,
minValue=0,
optional=False)
param.setIsDynamic(True)
param.setDynamicPropertyDefinition(QgsPropertyDefinition(
self.PrmInnerRadius,
tr('Inner radius'),
QgsPropertyDefinition.Double))
param.setDynamicLayerParameterName('INPUT')
self.addParameter(param)
param = QgsProcessingParameterNumber(
self.PrmStartingAngle,
tr('Starting angle'),
QgsProcessingParameterNumber.Double,
defaultValue=0,
optional=False)
param.setIsDynamic(True)
param.setDynamicPropertyDefinition(QgsPropertyDefinition(
self.PrmStartingAngle,
tr('Starting angle'),
QgsPropertyDefinition.Double))
param.setDynamicLayerParameterName('INPUT')
self.addParameter(param)
self.addParameter(
QgsProcessingParameterEnum(
self.PrmUnitsOfMeasure,
tr('Radius units'),
options=DISTANCE_LABELS,
defaultValue=0,
optional=False)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.PrmExportInputGeometry,
tr('Add input geometry fields to output table'),
False,
optional=True)
)
def prepareAlgorithm(self, parameters, context, feedback):
self.shape_type = self.parameterAsInt(parameters, self.PrmShapeType, context)
self.outer_radius = self.parameterAsDouble(parameters, self.PrmOuterRadius, context)
self.outer_radius_dyn = QgsProcessingParameters.isDynamic(parameters, self.PrmOuterRadius)
if self.outer_radius_dyn:
self.outer_radius_property = parameters[self.PrmOuterRadius]
self.inner_radius = self.parameterAsDouble(parameters, self.PrmInnerRadius, context)
self.inner_radius_dyn = QgsProcessingParameters.isDynamic(parameters, self.PrmInnerRadius)
if self.inner_radius_dyn:
self.inner_radius_property = parameters[self.PrmInnerRadius]
self.start_angle = self.parameterAsDouble(parameters, self.PrmStartingAngle, context)
self.start_angle_dyn = QgsProcessingParameters.isDynamic(parameters, self.PrmStartingAngle)
if self.start_angle_dyn:
self.start_angle_property = parameters[self.PrmStartingAngle]
self.numPoints = self.parameterAsInt(parameters, self.PrmStarPoints, context)
if self.numPoints < 3:
feedback.reportError('There must be at least 3 points for a star')
return False
self.numPoints_dyn = QgsProcessingParameters.isDynamic(parameters, self.PrmStarPoints)
if self.numPoints_dyn:
self.numPoints_property = parameters[self.PrmStarPoints]
self.half = (360.0 / self.numPoints) / 2.0
units = self.parameterAsInt(parameters, self.PrmUnitsOfMeasure, context)
self.export_geom = self.parameterAsBool(parameters, self.PrmExportInputGeometry, context)
self.measureFactor = conversionToMeters(units)
self.inner_radius_converted = self.inner_radius * self.measureFactor
self.outer_radius_converted = self.outer_radius * self.measureFactor
source = self.parameterAsSource(parameters, 'INPUT', context)
srcCRS = source.sourceCrs()
self.total_features = source.featureCount()
if srcCRS != epsg4326:
self.geomTo4326 = QgsCoordinateTransform(srcCRS, epsg4326, QgsProject.instance())
self.toSinkCrs = QgsCoordinateTransform(epsg4326, srcCRS, QgsProject.instance())
else:
self.geomTo4326 = None
self.toSinkCrs = None
self.num_bad = 0
return True
def processFeature(self, feature, context, feedback):
try:
pts = []
pt = feature.geometry().asPoint()
pt_orig_x = pt.x()
pt_orig_y = pt.y()
# make sure the coordinates are in EPSG:4326
if self.geomTo4326:
pt = self.geomTo4326.transform(pt.x(), pt.y())
if self.outer_radius_dyn:
oradius, e = self.outer_radius_property.valueAsDouble(context.expressionContext(), self.outer_radius)
if not e or oradius < 0:
self.num_bad += 1
return []
oradius *= self.measureFactor
else:
oradius = self.outer_radius_converted
if self.inner_radius_dyn:
iradius, e = self.inner_radius_property.valueAsDouble(context.expressionContext(), self.inner_radius)
if not e or iradius < 0:
self.num_bad += 1
return []
iradius *= self.measureFactor
else:
iradius = self.inner_radius_converted
if self.start_angle_dyn:
sangle, e = self.start_angle_property.valueAsDouble(context.expressionContext(), self.start_angle)
if not e:
self.num_bad += 1
return []
else:
sangle = self.start_angle
if self.numPoints_dyn:
spoints, e = self.numPoints_property.valueAsInt(context.expressionContext(), self.numPoints)
if not e or spoints < 3:
self.num_bad += 1
return []
shalf = (360.0 / spoints) / 2.0
else:
spoints = self.numPoints
shalf = self.half
i = spoints - 1
while i >= 0:
i -= 1
angle = (i * 360.0 / spoints) + sangle
g = geod.Direct(pt.y(), pt.x(), angle, oradius, Geodesic.LATITUDE | Geodesic.LONGITUDE)
pts.append(QgsPointXY(g['lon2'], g['lat2']))
g = geod.Direct(pt.y(), pt.x(), angle - shalf, iradius, Geodesic.LATITUDE | Geodesic.LONGITUDE)
pts.append(QgsPointXY(g['lon2'], g['lat2']))
makeIdlCrossingsPositive(pts)
# If the Output crs is not 4326 transform the points to the proper crs
if self.toSinkCrs:
for x, ptout in enumerate(pts):
pts[x] = self.toSinkCrs.transform(ptout)
pts.append(pts[0])
if self.shape_type == 0:
feature.setGeometry(QgsGeometry.fromPolygonXY([pts]))
else:
feature.setGeometry(QgsGeometry.fromPolylineXY(pts))
if self.export_geom:
attr = feature.attributes()
attr.append(pt_orig_x)
attr.append(pt_orig_y)
feature.setAttributes(attr)
except Exception:
'''s = traceback.format_exc()
feedback.pushInfo(s)'''
self.num_bad += 1
return []
return [feature]
def postProcessAlgorithm(self, context, feedback):
if self.num_bad:
feedback.pushInfo(tr("{} out of {} features had invalid parameters and were ignored.".format(self.num_bad, self.total_features)))
return {}