forked from NationalSecurityAgency/qgis-shapetools-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createRadialLines.py
254 lines (227 loc) · 9.44 KB
/
createRadialLines.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
import os
from geographiclib.geodesic import Geodesic
from qgis.core import (
QgsPoint, QgsFeature, QgsGeometry, QgsLineString, QgsMultiLineString, QgsField,
QgsProject, QgsWkbTypes, QgsCoordinateTransform)
from qgis.core import (
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterBoolean,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFeatureSink)
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant, QUrl
import traceback
from .settings import settings, epsg4326, geod
from .utils import tr, conversionToMeters, DISTANCE_LABELS
class CreateRadialLinesAlgorithm(QgsProcessingAlgorithm):
"""
Algorithm to create a donut shape.
"""
PrmInputLayer = 'InputLayer'
PrmOutputLayer = 'OutputLayer'
PrmOuterRadiusField = 'OuterRadiusField'
PrmInnerRadiusField = 'InnerRadiusField'
PrmNumberOfLinesField = 'NumberOfLinesField'
PrmDefaultOuterRadius = 'DefaultOuterRadius'
PrmDefaultInnerRadius = 'DefaultInnerRadius'
PrmUnitsOfMeasure = 'UnitsOfMeasure'
PrmDefaultNumberOfLines = 'DefaultNumberOfLines'
PrmExportInputGeometry = 'ExportInputGeometry'
def initAlgorithm(self, config):
self.addParameter(
QgsProcessingParameterFeatureSource(
self.PrmInputLayer,
tr('Input point layer'),
[QgsProcessing.TypeVectorPoint])
)
self.addParameter(
QgsProcessingParameterField(
self.PrmOuterRadiusField,
tr('Outer radius field'),
parentLayerParameterName=self.PrmInputLayer,
type=QgsProcessingParameterField.Any,
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.PrmInnerRadiusField,
tr('Inner radius field'),
parentLayerParameterName=self.PrmInputLayer,
type=QgsProcessingParameterField.Any,
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.PrmNumberOfLinesField,
tr('Number of radial lines field'),
parentLayerParameterName=self.PrmInputLayer,
type=QgsProcessingParameterField.Any,
optional=True
)
)
self.addParameter(
QgsProcessingParameterNumber(
self.PrmDefaultOuterRadius,
tr('Default outer radius'),
QgsProcessingParameterNumber.Double,
defaultValue=20.0,
minValue=0,
optional=True)
)
self.addParameter(
QgsProcessingParameterNumber(
self.PrmDefaultInnerRadius,
tr('Default inner radius'),
QgsProcessingParameterNumber.Double,
defaultValue=10.0,
minValue=0,
optional=True)
)
self.addParameter(
QgsProcessingParameterEnum(
self.PrmUnitsOfMeasure,
tr('Radius units'),
options=DISTANCE_LABELS,
defaultValue=0,
optional=False)
)
self.addParameter(
QgsProcessingParameterNumber(
self.PrmDefaultNumberOfLines,
tr('Default number of radial lines'),
QgsProcessingParameterNumber.Integer,
defaultValue=5,
minValue=1,
optional=True)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.PrmExportInputGeometry,
tr('Add input geometry fields to output table'),
False,
optional=True)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.PrmOutputLayer,
tr('Output layer'))
)
def processAlgorithm(self, parameters, context, feedback):
source = self.parameterAsSource(parameters, self.PrmInputLayer, context)
outer_col = self.parameterAsString(parameters, self.PrmOuterRadiusField, context)
inner_col = self.parameterAsString(parameters, self.PrmInnerRadiusField, context)
lines_col = self.parameterAsString(parameters, self.PrmNumberOfLinesField, context)
def_outer_radius = self.parameterAsDouble(parameters, self.PrmDefaultOuterRadius, context)
def_inner_radius = self.parameterAsDouble(parameters, self.PrmDefaultInnerRadius, context)
def_lines = self.parameterAsDouble(parameters, self.PrmDefaultNumberOfLines, context)
units = self.parameterAsInt(parameters, self.PrmUnitsOfMeasure, context)
export_geom = self.parameterAsBool(parameters, self.PrmExportInputGeometry, context)
measure_factor = conversionToMeters(units)
def_inner_radius *= measure_factor
def_outer_radius *= measure_factor
src_crs = source.sourceCrs()
fields = source.fields()
if export_geom:
names = fields.names()
name_x, name_y = settings.getGeomNames(names)
fields.append(QgsField(name_x, QVariant.Double))
fields.append(QgsField(name_y, QVariant.Double))
(sink, dest_id) = self.parameterAsSink(
parameters, self.PrmOutputLayer, context, fields,
QgsWkbTypes.MultiLineString, src_crs)
if src_crs != epsg4326:
geom_to_4326 = QgsCoordinateTransform(src_crs, epsg4326, QgsProject.instance())
to_sink_crs = QgsCoordinateTransform(epsg4326, src_crs, QgsProject.instance())
feature_count = source.featureCount()
total = 100.0 / feature_count if feature_count else 0
iterator = source.getFeatures()
num_bad = 0
for cnt, feature in enumerate(iterator):
if feedback.isCanceled():
break
try:
line_strings = []
pt = feature.geometry().asPoint()
pt_orig = QgsPoint(pt)
# make sure the coordinates are in EPSG:4326
if src_crs != epsg4326:
pt = geom_to_4326.transform(pt.x(), pt.y())
lat = pt.y()
lon = pt.x()
if inner_col:
inner_radius = float(feature[inner_col]) * measure_factor
else:
inner_radius = def_inner_radius
if outer_col:
outer_radius = float(feature[outer_col]) * measure_factor
else:
outer_radius = def_outer_radius
if lines_col:
num_lines = int(feature[lines_col])
else:
num_lines = def_lines
if num_lines <= 0:
num_bad += 1
continue
angle = 0
angle_step = 360.0 / num_lines
while angle < 360:
if inner_radius == 0:
pt_start = pt_orig
else:
g = geod.Direct(lat, lon, angle, inner_radius, Geodesic.LATITUDE | Geodesic.LONGITUDE)
pt_start = QgsPoint(g['lon2'], g['lat2'])
if src_crs != epsg4326:
pt_start = to_sink_crs.transform(pt_start)
g = geod.Direct(lat, lon, angle, outer_radius, Geodesic.LATITUDE | Geodesic.LONGITUDE)
pt_end = QgsPoint(g['lon2'], g['lat2'])
if src_crs != epsg4326:
pt_end = to_sink_crs.transform(pt_end)
line_str = QgsLineString([pt_start, pt_end])
line_strings.append(line_str)
angle += angle_step
f = QgsFeature()
if len(line_strings) == 1:
f.setGeometry(QgsGeometry(line_strings[0]))
else:
g = QgsMultiLineString()
for line_str in line_strings:
g.addGeometry(line_str)
f.setGeometry(QgsGeometry(g))
attr = feature.attributes()
if export_geom:
attr.append(pt_orig.x())
attr.append(pt_orig.y())
f.setAttributes(attr)
sink.addFeature(f)
except Exception:
s = traceback.format_exc()
feedback.pushInfo(s)
num_bad += 1
feedback.setProgress(int(cnt * total))
if num_bad > 0:
feedback.pushInfo(tr("{} out of {} features had invalid parameters and were ignored.".format(num_bad, feature_count)))
return {self.PrmOutputLayer: dest_id}
def name(self):
return 'createradiallines'
def icon(self):
return QIcon(os.path.join(os.path.dirname(__file__), 'images/radialLines.png'))
def displayName(self):
return tr('Create radial lines')
def group(self):
return tr('Geodesic vector creation')
def groupId(self):
return 'vectorcreation'
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 createInstance(self):
return CreateRadialLinesAlgorithm()