-
Notifications
You must be signed in to change notification settings - Fork 4
/
qgsAzimuth.py
1070 lines (921 loc) · 39.5 KB
/
qgsAzimuth.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ---------------------------------------------------------------------
#
# licensed under the terms of GNU GPL 2
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# ---------------------------------------------------------------------
import math
import os
from qgis.PyQt.QtCore import Qt, QFileInfo, QSettings, QSize, QPoint, QCoreApplication
from qgis.PyQt.QtWidgets import QAction, QTableWidgetItem, QFileDialog
from qgis.PyQt.QtGui import QIcon, QColor
from qgis.core import *
from .ui_control import Dock
from . import resources_rc
from math import *
from .getcoordtool import *
from .maptool import LineTool
from . import utils
class qgsazimuth(object):
"""
Base class for the qgsAzimuth plugin
- Provides a means to draw a feature by specifying the angle and distance beetween points.
- Supports angles in either the conventional 0.0 - 360.0 clockwise from North
or the surveyor's 'Easting' system with bearings plus or minus 90 deg. from North or South
- Supports magnetic declination as degrees plus or minus for East or West respectively
- supports inputs in feet or the current CRS units
"""
# just a test to see if mods are taking
def __init__(self, iface):
self.iface = iface
self.canvas = iface.mapCanvas()
self.fPath = (
""
) # set default working directory, updated from config file & by Import/Export
self.bands = []
def log(self, message):
QgsMessageLog.logMessage(str(message), "Plugin")
self.iface.messageBar().pushCritical("Error", str(message))
def initGui(self):
# create action that will start plugin configuration
self.action = QAction(
QIcon(":icons/qgsazimuth.png"),
"Azimuth and distance",
self.iface.mainWindow(),
)
self.action.setWhatsThis("Azimuth and distance")
self.action.triggered.connect(self.run)
self.bandpoint = QgsRubberBand(self.canvas, QgsWkbTypes.PointGeometry)
self.bandpoint.setIcon(QgsRubberBand.ICON_CROSS)
self.bandpoint.setColor(QColor.fromRgb(255, 50, 255))
self.bandpoint.setWidth(3)
self.bandpoint.setIconSize(20)
# add toolbar button and menu item
self.iface.addPluginToMenu("&Topography", self.action)
self.iface.addToolBarIcon(self.action)
self.dock = Dock(self.iface.mainWindow())
self.iface.addDockWidget(Qt.BottomDockWidgetArea, self.dock)
self.dock.hide()
self.pluginGui = self.dock.widget()
self.dock.closed.connect(self.cleanup)
self.pluginGui.pushButton_vertexAdd.clicked.connect(self.addRow)
self.pluginGui.pushButton_vertexInsert.clicked.connect(self.insertRow)
self.pluginGui.pushButton_segListRowDel.clicked.connect(self.delRow)
self.pluginGui.pushButton_segListLoad.clicked.connect(self.loadList)
self.pluginGui.pushButton_segListClear.clicked.connect(self.clearList)
self.pluginGui.pushButton_objectDraw.clicked.connect(self.addgeometry)
self.pluginGui.pushButton_startCapture.clicked.connect(self.startgetpoint)
self.pluginGui.pushButton_segListSave.clicked.connect(self.saveList)
self.pluginGui.pushButton_useLast.clicked.connect(self.use_last_vertex)
self.pluginGui.pickAngle1_button.clicked.connect(self.select_angle1)
self.pluginGui.pickAngle2_button.clicked.connect(self.select_angle2)
self.pluginGui.clearMarkers_button.clicked.connect(self.clear_markers)
self.pluginGui.copyDiff_button.clicked.connect(self.copy_diff_offset)
# self.pluginGui.table_segmentList.cellChanged.connect(self.render_temp_band)
self.pluginGui.table_segmentList.setCurrentCell(0, 0)
self.tool = GetCoordTool(self.canvas)
self.tool.finished.connect(self.getpoint)
self.tool.locationChanged.connect(self.pluginGui.update_startpoint)
self.tool.locationChanged.connect(self.update_marker_location)
self.angletool = LineTool(self.canvas)
self.angletool.geometryComplete.connect(self.update_angle1)
self.angletool.locationChanged.connect(self.update_marker_location)
self.angletool2 = LineTool(self.canvas)
self.angletool2.geometryComplete.connect(self.update_angle2)
self.angletool2.locationChanged.connect(self.update_marker_location)
self.pluginGui.azimuth1_edit.textChanged.connect(self.update_angle_calc)
self.pluginGui.azimuth2_edit.textChanged.connect(self.update_angle_calc)
self.pluginGui.lineEdit_magNorth.textChanged.connect(self.update_offsetlabel)
self.pluginGui.radioButton_defaultNorth.toggled.connect(self.update_offsetlabel)
self.pluginGui.radioButton_azimuthAngle.toggled.connect(self.update_angle_label)
self.pluginGui.radioButton_bearingAngle.toggled.connect(self.update_angle_label)
def update_offsetlabel(self, *args):
mag = self.mag_dev
self.pluginGui.offsetLabel.setText(str(mag))
def update_angle_label(self, *args):
if self.angletype == "azimuth":
item = self.pluginGui.table_segmentList.horizontalHeaderItem(0)
item.setText(QCoreApplication.translate("Form", "Azimuth"))
self.pluginGui.label_angle.setText(
QCoreApplication.translate("Form", "Azimuth:")
)
else:
item = self.pluginGui.table_segmentList.horizontalHeaderItem(0)
item.setText(QCoreApplication.translate("Form", "Bearing"))
self.pluginGui.label_angle.setText(
QCoreApplication.translate("Form", "Bearing:")
)
def copy_diff_offset(self):
diff = self.pluginGui.azimuthDiff_edit.text()
self.pluginGui.lineEdit_magNorth.setText(diff)
def clear_markers(self):
self.angletool2.reset()
self.angletool.reset()
self.pluginGui.azimuth1_edit.setText(str(0))
self.pluginGui.azimuth2_edit.setText(str(0))
def update_angle_calc(self):
a1 = self.pluginGui.azimuth1_edit.text()
a2 = self.pluginGui.azimuth2_edit.text()
a1 = utils.dmsToDd(a1)
a2 = utils.dmsToDd(a2)
try:
a1 = float(a1)
a2 = float(a2)
except ValueError:
self.pluginGui.azimuthDiff_edit.setText("")
return
diff = a2 - a1
self.pluginGui.azimuthDiff_edit.setText(str(diff))
def select_angle1(self):
self.canvas.setMapTool(self.angletool)
def update_angle1(self, geometry):
az = utils.azimuth_from_line(geometry)
az = str(az)
self.pluginGui.azimuth1_edit.setText(az)
def update_angle2(self, geometry):
az = utils.azimuth_from_line(geometry)
az = str(az)
self.pluginGui.azimuth2_edit.setText(az)
def select_angle2(self):
self.canvas.setMapTool(self.angletool2)
def update_marker_location(self, point):
self.bandpoint.setToGeometry(QgsGeometry.fromPointXY(point), None)
def unload(self):
# remove the plugin menu item and icon
self.saveConf()
self.iface.removeDockWidget(self.dock)
self.iface.removePluginMenu("&Topography", self.action)
self.iface.removeToolBarIcon(self.action)
self.bandpoint.reset()
self.tool.cleanup()
self.clear_markers()
del self.angletool2
del self.angletool
del self.tool
del self.bandpoint
def run(self):
# misc init
self.loadConf() # get config data
self.clearList()
self.setStartAt("0;0;90") # remove previous StartAt point
self.pluginGui.lineEdit_crs.setText(
self.iface.mapCanvas().mapSettings().destinationCrs().description()
)
if self.iface.activeLayer():
self.updatelayertext(self.iface.activeLayer())
self.pluginGui.radioButton_useActiveLayer.setChecked(True)
else:
self.pluginGui.radioButton_useActiveLayer.setEnabled(False)
self.pluginGui.radioButton_useMemoryLayer.setChecked(True)
self.iface.currentLayerChanged.connect(self.updatelayertext)
if not self.dock.isVisible():
self.dock.show()
# for debugging convenience
self.notes = self.pluginGui.plainTextEdit_note
def cleanup(self):
self.tool.cleanup()
self.clear_bands()
self.saveConf()
def updatelayertext(self, layer):
if not layer:
self.pluginGui.radioButton_useActiveLayer.setEnabled(False)
else:
self.pluginGui.radioButton_useActiveLayer.setEnabled(True)
self.pluginGui.radioButton_useActiveLayer.setText(
"Active Layer ({0})".format(layer.name())
)
@property
def useactivelayer(self):
return self.pluginGui.radioButton_useActiveLayer.isChecked()
def render_temp_band(self, *args):
"""
Render a temp rubber band for showing the user the results
"""
self.clear_bands()
featurelist, vectorlayer = self.create_feature()
if not featurelist or not vectorlayer:
return
for feature in featurelist:
band = QgsRubberBand(self.iface.mapCanvas())
if hasattr(band, "setLineStyle"):
band.setLineStyle(Qt.DotLine)
band.setWidth(4)
band.setColor(Qt.darkMagenta)
band.setToGeometry(feature.geometry(), vectorlayer)
band.show()
self.bands.append(band)
pass
@property
def should_open_form(self):
return self.pluginGui.checkBox_openForm.isChecked()
@should_open_form.setter
def should_open_form(self, value):
return self.pluginGui.checkBox_openForm.setChecked(value)
def addgeometry(self):
featurelist, vectorlayer = self.create_feature()
if not featurelist or not vectorlayer:
return
if vectorlayer.sourceCrs().isGeographic():
self.log(
"Selected layer uses a geographic CRS, i.e., uses lat/lon "
"coordinates, but only projected CRSes are supported by plugin!"
)
return
if not self.useactivelayer:
QgsProject.instance().addMapLayer(vectorlayer)
vectorlayer.startEditing()
for feature in featurelist:
if self.should_open_form:
form = self.iface.getFeatureForm(vectorlayer, feature)
form.setMode(QgsAttributeEditorContext.AddFeatureMode)
if not form.exec_():
continue
else:
print(feature.isValid())
print(feature.geometry().asWkt())
result = vectorlayer.addFeature(feature)
print("Error adding feature", feature)
if result == False:
self.log("Error in adding feature")
self.iface.mapCanvas().refresh()
self.clear_bands()
def clear_bands(self):
for band in self.bands:
band.reset()
self.bands = []
def update_draw_button_state(self):
x, y, z = self.starting_point()
enabled = True
if (x == 0 and y == 0 and z == 90) or (
self.pluginGui.table_segmentList.rowCount() == 0
):
enabled = False
self.pluginGui.pushButton_objectDraw.setEnabled(enabled)
def starting_point(self):
# Get starting point coordinates
X = float(str(self.pluginGui.lineEdit_vertexX0.text()))
Y = float(str(self.pluginGui.lineEdit_vertexY0.text()))
Z = float(str(self.pluginGui.lineEdit_vertexZ0.text()))
return X, Y, Z
@property
def angletype(self):
if self.pluginGui.radioButton_azimuthAngle.isChecked():
return "azimuth"
elif self.pluginGui.radioButton_bearingAngle.isChecked():
return "bearing"
elif self.pluginGui.radioButton_polarCoordAngle.isChecked():
return "polor"
@angletype.setter
def angletype(self, value):
if value == "azimuth":
self.pluginGui.radioButton_azimuthAngle.setChecked(True)
elif value == "bearing":
self.pluginGui.radioButton_bearingAngle.setChecked(True)
elif value == "polor":
self.pluginGui.radioButton_polarCoordAngle.setChecked(True)
else:
self.pluginGui.radioButton_azimuthAngle.setChecked(True)
@property
def distanceunits(self):
if self.pluginGui.radioButton_defaultUnits.isChecked():
return "default"
elif self.pluginGui.radioButton_englishUnits.isChecked():
return "feet"
@distanceunits.setter
def distanceunits(self, value):
if value == "default":
self.pluginGui.radioButton_defaultUnits.setChecked(True)
elif value == "feet":
self.pluginGui.radioButton_englishUnits.setChecked(True)
else:
self.pluginGui.radioButton_defaultUnits.setChecked(True)
@property
def angleunit(self):
if self.pluginGui.radioButton_degreeUnit.isChecked():
return "degree"
elif self.pluginGui.radioButton_gradianUnit.isChecked():
return "gradian"
@angleunit.setter
def angleunit(self, value):
if value == "degree":
self.pluginGui.radioButton_degreeUnit.setChecked(True)
elif value == "gradian":
self.pluginGui.radioButton_gradianUnit.setChecked(True)
else:
self.pluginGui.radioButton_degreeUnit.setChecked(True)
@property
def northtype(self):
if self.pluginGui.radioButton_magNorth.isChecked():
return "magnetic"
else:
return "default"
@northtype.setter
def northtype(self, value):
if value == "magnetic":
self.pluginGui.radioButton_magNorth.setChecked(True)
else:
self.pluginGui.radioButton_defaultNorth.setChecked(True)
@property
def mag_dev(self):
if self.pluginGui.radioButton_magNorth.isChecked():
value = str(self.pluginGui.lineEdit_magNorth.text())
try:
return float(value)
except ValueError:
try:
if self.pluginGui.radioButton_gradianUnit.isChecked():
value = utils.gradianToDd(value)
return float(utils.dmsToDd(value))
except IndexError:
return 0.0
elif self.pluginGui.radioButton_defaultNorth.isChecked():
return 0.0
else:
return 0.0
@mag_dev.setter
def mag_dev(self, value):
self.pluginGui.lineEdit_magNorth.setText(str(value))
@property
def surveytype(self):
if self.pluginGui.radioButton_radialSurvey.isChecked():
surveytype = "radial"
elif self.pluginGui.radioButton_boundarySurvey.isChecked():
surveytype = "polygonal"
return surveytype
@surveytype.setter
def surveytype(self, value):
if value == "radial":
self.pluginGui.radioButton_radialSurvey.setChecked(True)
elif value == "polygonal":
self.pluginGui.radioButton_boundarySurvey.setChecked(True)
else:
self.pluginGui.radioButton_boundarySurvey.setChecked(True)
def use_last_vertex(self):
# Get the last point from the last band
x, y, z = 0, 0, 90
arcpoint_count = self.arc_count
points = self.get_points(self.surveytype, arcpoint_count)
try:
point = points[-1]
x, y, z = point.x, point.y, point.z
except IndexError:
# Don't do anything if there is no last point
return
point = QgsPointXY(x, y)
self.pluginGui.update_startpoint(point, z)
self.update_marker_location(point)
self.clearList()
def table_entries(self):
"""
Return the entries for each row in the table
"""
rows = self.pluginGui.table_segmentList.rowCount()
for row in range(rows):
az = self.pluginGui.table_segmentList.item(row, 0).text()
dis = float(str(self.pluginGui.table_segmentList.item(row, 1).text()))
zen = self.pluginGui.table_segmentList.item(row, 2).text()
direction = self.pluginGui.table_segmentList.item(row, 4).text()
direction = utils.Direction.resolve(direction)
try:
radius = float(self.pluginGui.table_segmentList.item(row, 3).text())
except ValueError:
radius = None
yield az, dis, zen, direction, radius
def get_points(self, surveytype, arcpoint_count):
"""
Return a list of calculated points for the full run.
:param surveytype:
:return:
"""
X, Y, Z = self.starting_point()
if (X == 0 and Y == 0 and Z == 90) or (
self.pluginGui.table_segmentList.rowCount() == 0
):
return []
vlist = []
vlist.append(utils.Point(X, Y, Z))
# convert segment list to set of vertice
for az, dis, zen, direction, radius in self.table_entries():
if self.pluginGui.radioButton_englishUnits.isChecked():
# adjust for input in feet, not meters
dis = float(dis) * 0.3048
# checking degree input
if self.pluginGui.radioButton_azimuthAngle.isChecked():
if self.pluginGui.radioButton_gradianUnit.isChecked():
az = utils.gradianToDd(az)
zen = utils.gradianToDd(zen)
else:
az = float(utils.dmsToDd(az))
zen = float(utils.dmsToDd(zen))
elif self.pluginGui.radioButton_bearingAngle.isChecked():
az = float(self.bearingToDd(az))
zen = float(self.bearingToDd(zen))
# correct for magnetic compass headings if necessary
self.magDev = self.mag_dev
az = float(az) + float(self.magDev)
# correct for angles outside of 0.0-360.0
while az > 360.0:
az = az - 360.0
while az < 0.0:
az = az + 360.0
# checking survey type
if surveytype == "radial":
reference_point = vlist[0] # reference first vertex
if surveytype == "polygonal":
reference_point = vlist[-1] # reference previous vertex
nextpoint = utils.nextvertex(reference_point, dis, az, zen)
if radius:
# If there is a radius then we are drawing a arc.
if self.pluginGui.radioButton_englishUnits.isChecked():
# adjust for input in feet, not meters
radius = float(radius) * 0.3048
# Make sure distance <= diameter
if dis > 2 * radius:
self.log("Invalid arc: distance can't be greater than diameter")
return []
# Calculate the arc points.
points = list(
utils.arc_points(
reference_point,
nextpoint,
dis,
radius,
point_count=arcpoint_count,
direction=direction,
zenith_angle=zen,
)
)
if direction == utils.Direction.ANTICLOCKWISE:
points = reversed(points)
# Append them to the final points list.
vlist.extend(points)
vlist.append(nextpoint)
return vlist
@property
def drawing_layer(self):
if self.useactivelayer:
vectorlayer = self.iface.activeLayer()
else:
code = self.iface.mapCanvas().mapSettings().destinationCrs().authid()
vectorlayer = QgsVectorLayer(
"LineString?crs={}".format(code), "tmp_plot", "memory"
)
return vectorlayer
@property
def arc_count(self):
"""
The number of points to use when drawing arcs
"""
return self.pluginGui.spin_arclines.value()
@arc_count.setter
def arc_count(self, value):
"""
The number of points to use when drawing arcs
"""
self.pluginGui.spin_arclines.setValue(value)
def create_feature(self):
vectorlayer = self.drawing_layer
# reprojecting to projects SRS
arcpoint_count = self.arc_count
points = self.get_points(self.surveytype, arcpoint_count)
if not points:
return None, None
vlist = self.reproject(points, vectorlayer)
as_segments = self.pluginGui.checkBox_asSegments.isChecked()
featurelist = []
geometrytype = vectorlayer.geometryType()
if geometrytype == QgsWkbTypes.PointGeometry:
points = utils.to_qgspoints(vlist)
features = utils.createpoints(points)
featurelist.extend(features)
elif geometrytype == QgsWkbTypes.LineGeometry:
if as_segments:
# If the line is to be draw as segments then we loop the pairs and create a line for each one.
points_to_join = []
in_arc = False
for pair in utils.pairs(
vlist, matchtail=self.surveytype == "polygonal"
):
start, end = pair[0], pair[1]
# If we are not drawing the arc then just add the pair to get a single line
if not start.arc_point and not end.arc_point:
points_to_join = pair
else:
# If we are in a arc we need to handle drawing it as one line
# which means grabbing each pair until we are finished the arc
if not start.arc_point and end.arc_point:
points_to_join = []
in_arc = True
if start.arc_point and not end.arc_point:
points_to_join.append(start)
points_to_join.append(end)
in_arc = False
if in_arc:
points_to_join.append(start)
points_to_join.append(end)
continue
pointlist = utils.to_qgspoints(
points_to_join, repeatfirst=self.surveytype == "radial"
)
feature = utils.createline(pointlist)
featurelist.append(feature)
else:
pointlist = utils.to_qgspoints(
vlist, repeatfirst=self.surveytype == "radial"
)
feature = utils.createline(pointlist)
featurelist.append(feature)
elif geometrytype == QgsWkbTypes.PolygonGeometry:
polygon = utils.to_qgspoints(vlist)
feature = utils.createpolygon([polygon])
if feature:
featurelist.append(feature)
# Add the fields for the current layer
for feature in featurelist:
feature.setFields(vectorlayer.fields())
return featurelist, vectorlayer
def bearingToDd(self, dms):
# allow survey bearings in form: - N 25d 34' 40" E
# where minus ('-') sign allows handling bearings given in reverse direction
dms = dms.strip()
if dms[0] == "-":
rev = True
dms = dms[1:].strip()
else:
rev = False
baseDir = dms[0].upper()
if baseDir in ["N", "S"]:
adjDir = dms[-1].upper()
bearing = True
if baseDir == "N":
if adjDir == "E":
base = 0.0
adj = "add"
elif adjDir == "W":
base = 360.0
adj = "sub"
else:
return 0
elif baseDir == "S":
base = 180.0
if adjDir == "E":
adj = "sub"
elif adjDir == "W":
adj = "add"
else:
return 0
else:
bearing = False
if self.pluginGui.radioButton_gradianUnit.isChecked():
dd = utils.gradianToDd(dms)
else:
dd = utils.dmsToDd(dms)
if rev:
dd = float(dd) + 180.0
if bearing == True:
if adj == "add":
dd = float(base) + float(dd)
elif adj == "sub":
dd = float(base) - float(dd)
return dd
def clearList(self):
self.pluginGui.table_segmentList.clearContents()
self.pluginGui.table_segmentList.setSortingEnabled(False)
self.pluginGui.table_segmentList.setRowCount(0)
self.pluginGui.table_segmentList.setCurrentCell(
0, 0
) # substitute for missing setCurrentRow()
self.render_temp_band()
# retranslateUi
def newVertex(self):
# adds a vertex from the gui
self.addrow(
self.pluginGui.lineEdit_nextAzimuth.text(),
self.pluginGui.lineEdit_nextDistance.value(),
self.pluginGui.lineEdit_nextVertical.text(),
self.pluginGui.spin_radius.value(),
)
def addRow(self):
# this and following must be split to handle both GUI & FILE inputs
az = self.pluginGui.lineEdit_nextAzimuth.text()
dist = self.pluginGui.lineEdit_nextDistance.value()
zen = self.pluginGui.lineEdit_nextVertical.text()
radius = self.pluginGui.spin_radius.value()
if radius == 0:
radius = None
direction = None
else:
if self.pluginGui.radio_anticlockwise.isChecked():
direction = "anticlockwise"
else:
direction = "clockwise"
self.addrow(az, dist, zen, radius, direction)
def addrow(self, az=0, dist=0, zen=90, radius=None, direction=None):
# add the vertex to the end of the table
row = self.pluginGui.table_segmentList.rowCount()
self.pluginGui.table_segmentList.insertRow(row)
self.pluginGui.table_segmentList.setItem(
row, 0, QTableWidgetItem(str(az).upper())
)
self.pluginGui.table_segmentList.setItem(row, 1, QTableWidgetItem(str(dist)))
self.pluginGui.table_segmentList.setItem(row, 2, QTableWidgetItem(str(zen)))
self.pluginGui.table_segmentList.setItem(row, 3, QTableWidgetItem(str(radius)))
self.pluginGui.table_segmentList.setItem(
row, 4, QTableWidgetItem(str(direction))
)
self.render_temp_band()
def insertRow(self):
az = self.pluginGui.lineEdit_nextAzimuth.text()
dist = self.pluginGui.lineEdit_nextDistance.value()
zen = self.pluginGui.lineEdit_nextVertical.text()
radius = self.pluginGui.spin_radius.value()
if radius == 0:
radius = None
direction = None
else:
if self.pluginGui.radio_anticlockwise.isChecked():
direction = "anticlockwise"
else:
direction = "clockwise"
# insert the vertext into the table at the current position
row = self.pluginGui.table_segmentList.currentRow()
self.pluginGui.table_segmentList.insertRow(row)
self.pluginGui.table_segmentList.setItem(
row, 0, QTableWidgetItem(str(az).upper())
)
self.pluginGui.table_segmentList.setItem(row, 1, QTableWidgetItem(str(dist)))
self.pluginGui.table_segmentList.setItem(row, 2, QTableWidgetItem(str(zen)))
self.pluginGui.table_segmentList.setItem(row, 3, QTableWidgetItem(str(radius)))
self.pluginGui.table_segmentList.setItem(
row, 4, QTableWidgetItem(str(direction))
)
self.render_temp_band()
def delRow(self):
self.pluginGui.table_segmentList.removeRow(
self.pluginGui.table_segmentList.currentRow()
)
self.render_temp_band()
def moveup(self):
self.render_temp_band()
pass
def movedown(self):
self.render_temp_band()
pass
def startgetpoint(self):
# point capture tool
self.saveTool = self.canvas.mapTool()
self.canvas.setMapTool(self.tool)
def getpoint(self, pt):
self.clear_markers()
self.pluginGui.update_startpoint(pt)
self.canvas.setMapTool(self.saveTool)
def reproject(self, vlist, vectorlayer):
renderer = self.canvas.mapSettings()
for row, point in enumerate(vlist):
new_point = renderer.layerToMapCoordinates(
vectorlayer, QgsPointXY(point[0], point[1])
)
# Translate it into our new point with arc_point info
new_point = utils.Point(
new_point.x(), new_point.y(), arc_point=point.arc_point
)
vlist[row] = new_point
return vlist
def setAngle(self, s):
# self.say('processing angleType='+s)
if s == "azimuth":
self.pluginGui.radioButton_azimuthAngle.setChecked(True)
elif s == "bearing":
self.pluginGui.radioButton_bearingAngle.setChecked(True)
elif s == "polar":
self.pluginGui.radioButton_polorCoordAngle.setChecked(True)
else:
self.say("invalid angle type: " + s)
def setHeading(self, s):
# self.say('processing headingType='+s)
if s == "coordinate_system":
self.pluginGui.radioButton_defaultNorth.setChecked(True)
elif s == "magnetic":
self.pluginGui.radioButton_magNorth.setChecked(True)
else:
self.say("invalid heading type: " + s)
def setDeclination(self, s):
# self.say('processing declination='+s)
self.pluginGui.lineEdit_magNorth.setText(s)
self.magDev = float(s)
def setDistanceUnits(self, s):
# self.say('processing distance units='+s)
if s == "feet":
self.pluginGui.radioButton_englishUnits.setChecked(True)
else:
self.pluginGui.radioButton_defaultUnits.setChecked(True)
def setAngleUnit(self, s):
if s == "gradian":
self.pluginGui.radioButton_gradianUnit.setChecked(True)
else:
self.pluginGui.radioButton_degreeUnit.setChecked(True)
def setStartAt(self, s):
# self.say('processing startAt='+s)
coords = [float(v) for v in s.split(";")]
point = QgsPointXY(coords[0], coords[1])
self.pluginGui.update_startpoint(point, coords[2])
def setSurvey(self, s):
# self.say('processing surveyType='+s)
if s == "polygonal":
self.pluginGui.radioButton_boundarySurvey.setChecked(True)
elif s == "radial":
self.pluginGui.radioButton_radialSurvey.setChecked(True)
else:
self.say("invalid survey type: " + s)
def say(self, txt):
# present a message box on screen
warn = QgsMessageViewer()
warn.setMessageAsPlainText(txt)
warn.showMessage()
def tell(self, txt):
# write to bottom of Note area at top of screen
self.notes.appendPlainText(txt)
# ---------------------------------------------------------------------------------------------------------------------------------
# File handling
# This section deals with saving the user data to disk, and loading it
#
# format:
# line 1: angle=Azimuth|Bearing|Polar
# line 2: heading=Coordinate System|Magnetic
# line 3: declination=[- ]x.xxd[ xx.x'] [E|W]
# line 4: distunits=Default|Feet
# line 5: startAt=xxxxx.xxxxx, xxxxxx.xxxxx
# line 6: survey=Polygonal|Radial
# line 7: [data]
# line 8 through end: Azimuth; dist; zen
#
# note: lines 1 through 5 are optional if hand entered, but will always be generated when 'saved'
# ---------------------------------------------------------------------------------------------------------------------------------
def loadList(self):
self.fileName, _ = QFileDialog.getOpenFileName(
None, "Load data separated by ';'", self.fPath, ""
)
if not os.path.exists(self.fileName):
return 0
# update selected file's folder
fInfo = QFileInfo(self.fileName)
self.fPath = fInfo.absolutePath()
self.saveConf()
self.render_temp_band()
# get saved data
try:
f = open(self.fileName)
lines = f.readlines()
f.close()
self.clearList()
for line in lines:
# remove trailing 'new lines', etc and break into parts
parts = ((line.strip()).lower()).split("=")
if len(parts) > 1:
# self.say("line="+line+'\nparts[0]='+parts[0]+'\nparts[1]='+parts[1])
if parts[0].lower() == "angle":
self.setAngle(parts[1].lower())
elif parts[0].lower() == "heading":
self.setHeading(parts[1].lower())
elif parts[0].lower() == "declination":
self.setDeclination(parts[1].lower())
elif parts[0].lower() == "dist_units":
self.setDistanceUnits(parts[1].lower())
elif parts[0].lower() == "angle_unit":
self.setAngleUnit(parts[1].lower())
elif parts[0].lower() == "startat":
self.setStartAt(parts[1].lower())
elif parts[0].lower() == "survey":
self.setSurvey(parts[1].lower())
else:
coords = tuple((line.strip()).split(";"))
if coords[0].lower() == "[data]":
pass
else:
self.addrow(*coords)
except:
self.say("Invalid input")
def saveList(self):
file, _ = QFileDialog.getSaveFileName(
None, "Save segment list to file.", self.fileName, ""
)
if file == "":
return
f = open(file, "w")
# update selected file's folder
fInfo = QFileInfo(file)
self.fPath = fInfo.absolutePath()
self.saveConf()
if self.pluginGui.radioButton_azimuthAngle.isChecked():
s = "Azimuth"
elif self.pluginGui.radioButton_bearingAngle.isChecked():
s = "Bearing"
f.write("angle=" + s + "\n")
if self.pluginGui.radioButton_defaultNorth.isChecked():
s = "Coordinate_System"
elif self.pluginGui.radioButton_magNorth.isChecked():
s = "Magnetic"
f.write("heading=" + s + "\n")
if hasattr(self, "magDev") and self.magDev != 0.0:
f.write("declination=" + str(self.magDev) + "\n")
if self.pluginGui.radioButton_defaultUnits.isChecked():
s = "Default"
elif self.pluginGui.radioButton_englishUnits.isChecked():
s = "Feet"
f.write("dist_units=" + s + "\n")
if self.pluginGui.radioButton_degreeUnit.isChecked():
s = "degree"
elif self.pluginGui.radioButton_gradianUnit.isChecked():
s = "gradian"
f.write("angle_unit=" + s + "\n")
f.write(
"startAt="
+ str(self.pluginGui.lineEdit_vertexX0.text())
+ ";"
+ str(self.pluginGui.lineEdit_vertexY0.text())
+ ";"
+ str(self.pluginGui.lineEdit_vertexZ0.text())
+ "\n"
)
if self.pluginGui.radioButton_boundarySurvey.isChecked():
s = "Polygonal"
elif self.pluginGui.radioButton_radialSurvey.isChecked():
s = "Radial"
f.write("survey=" + s + "\n")