forked from havatv/qgislinedirectionhistogramplugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinedirectionhistogram_dialog.py
More file actions
1149 lines (1078 loc) · 52.6 KB
/
linedirectionhistogram_dialog.py
File metadata and controls
1149 lines (1078 loc) · 52.6 KB
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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
linedirectionhistogramDialog
A QGIS plugin
Create histogram of line directions
-------------------
begin : 2015-01-10
git sha : $Format:%H$
copyright : (C) 2015 by Håvard Tveite, NMBU
email : havard.tveite@nmbu.no
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# User interface input components:
# histogramGraphicsView: The GraphicsView that contains the histogram
# setupGraphicsView: The GraphicsView that shows the setup (bins and angles)
# binsSpinBox
# offsetAngleSpinBox
# directionNeutralCheckBox
# selectedFeaturesCheckBox
# noWeightingCheckBox
# dirTrendCheckBox
# lineDirCB
# drawCirclesCB
# colorB
# inputLayer
import os
import csv
import math
import tempfile # rose diagram SVG files for rose layer rendering
import uuid # for generating unique file names (QGIS bug #13565)
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QObject, QThread, QCoreApplication, QUrl
# from qgis.PyQt.QtCore import SIGNAL
from qgis.PyQt.QtCore import QPointF, QLineF, QRectF, QPoint, QSettings
from qgis.PyQt.QtCore import QSizeF, QSize, QRect, Qt
from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtWidgets import QWidget
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox
from qgis.PyQt.QtWidgets import QFileDialog
from qgis.PyQt.QtWidgets import QGraphicsLineItem, QGraphicsEllipseItem
from qgis.PyQt.QtWidgets import QGraphicsScene, QGraphicsView
from qgis.PyQt.QtWidgets import QApplication
from qgis.PyQt.QtGui import QBrush, QPen, QColor
from qgis.PyQt.QtGui import QPainter, QImage, QPixmap
from qgis.PyQt.QtGui import QDesktopServices
from qgis.PyQt.QtPrintSupport import QPrinter
from qgis.PyQt.QtSvg import QSvgGenerator
# +from qgis.PyQt.QtGui import QStandardItem, QStandardItemModel
# +from qgis.core import QgsWkbTypes
from qgis.core import QgsProject
from qgis.core import QgsMessageLog, QgsMapLayer
# from qgis.core import QgsMapLayerRegistry
from qgis.core import Qgis
from qgis.core import QgsVectorLayer
from qgis.core import QgsField, QgsFeature
from qgis.core import QgsCategorizedSymbolRenderer, QgsSymbol
from qgis.core import QgsSvgMarkerSymbolLayer, QgsRendererCategory
# from qgis.gui import QgsMessageBar
# from qgis.utils import showPluginHelp
from .linedirectionhistogram_engine import Worker
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'linedirectionhistogram_dialog_base.ui'))
# Angles:
# Real world angles (and QGIS azimuth) are measured clockwise from
# the 12 o'clock position (north).
# QT angles are measured counter clockwise from the 3 o'clock
# position.
class linedirectionhistogramDialog(QDialog, FORM_CLASS):
def __init__(self, iface, parent=None):
self.iface = iface
self.plugin_dir = os.path.dirname(__file__)
# Some constants
self.LINEDIRECTIONHISTOGRAM = self.tr('LineDirectionHistogram')
self.BROWSE = self.tr('Browse')
self.CANCEL = self.tr('Cancel')
self.HELP = self.tr('Help')
self.CLOSE = self.tr('Close')
self.OK = self.tr('OK')
self.NUMBEROFRINGS = 10 # Number of concentric rings in the histogram
"""Constructor."""
super(linedirectionhistogramDialog, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
okButton = self.button_box.button(QDialogButtonBox.Ok)
okButton.setText(self.OK)
cancelButton = self.button_box.button(QDialogButtonBox.Cancel)
cancelButton.setText(self.CANCEL)
helpButton = self.helpButton
helpButton.setText(self.HELP)
browseButtonCSV = self.browseButtonCSV
browseButtonCSV.setText(self.BROWSE)
browseButtonTile = self.browseButtonTile
browseButtonTile.setText(self.BROWSE)
closeButton = self.button_box.button(QDialogButtonBox.Close)
closeButton.setText(self.CLOSE)
self.colorButton = self.colorB
self.meanvectorcolour = QColor(153, 0, 0)
self.colorButton.setColor(self.meanvectorcolour)
# Connect signals
okButton.clicked.connect(self.startWorker)
cancelButton.clicked.connect(self.killWorker)
helpButton.clicked.connect(self.giveHelp)
closeButton.clicked.connect(self.reject)
browseButtonCSV.clicked.connect(self.browse)
browseButtonTile.clicked.connect(self.browseTile)
dirNeutralCBCh = self.directionNeutralCheckBox.stateChanged
dirNeutralCBCh.connect(self.updateBins)
noWeightingCBCh = self.noWeightingCheckBox.stateChanged
noWeightingCBCh.connect(self.noWeighting)
logaritmicCBCh = self.logaritmicCheckBox.stateChanged
logaritmicCBCh.connect(self.logaritmic)
noAreaProportionalCBCh = self.proportionalAreaCheckBox.stateChanged
noAreaProportionalCBCh.connect(self.proportionalArea)
binsSBCh = self.binsSpinBox.valueChanged[str]
binsSBCh.connect(self.updateBins)
offsetAngleSBCh = self.offsetAngleSpinBox.valueChanged[str]
offsetAngleSBCh.connect(self.updateBins)
dirTrendCBCh = self.dirTrendCheckBox.stateChanged
dirTrendCBCh.connect(self.trend)
self.saveAsPDFButton.clicked.connect(self.saveAsPDF)
self.saveAsSVGButton.clicked.connect(self.saveAsSVG)
self.copyToClipboardButton.clicked.connect(self.copyToClipboard)
self.InputLayer.currentIndexChanged.connect(self.inputLayerChanged)
self.useTilingCheckBox.toggled.connect(self.tilingToggled)
self.saveAsPDFButton.setEnabled(False)
self.saveAsSVGButton.setEnabled(False)
self.copyToClipboardButton.setEnabled(False)
cancelButton.setEnabled(True)
# self.iface.legendInterface().itemAdded.connect(
# self.layerlistchanged)
# self.iface.legendInterface().itemRemoved.connect(
# self.layerlistchanged)
# QObject.disconnect(self.button_box, SIGNAL("rejected()"),
# self.reject)
self.button_box.rejected.disconnect(self.reject)
# Set instance variables
self.worker = None
self.inputlayerid = None
# self.layerlistchanging = False
self.bins = 8
self.binsSpinBox.setValue(self.bins)
# Direction neutrality is the default
self.directionneutral = True
self.directionNeutralCheckBox.setChecked(self.directionneutral)
# Weighting by line segment length is the default
self.noWeightingCheckBox.setChecked(False)
self.proportionalAreaCheckBox.setChecked(False)
self.selectedFeaturesCheckBox.setChecked(True)
self.setupScene = QGraphicsScene(self)
self.setupGraphicsView.setScene(self.setupScene)
self.histscene = QGraphicsScene(self)
self.histogramGraphicsView.setScene(self.histscene)
maxoffsetangle = int(360 / self.bins)
if self.directionneutral:
maxoffsetangle = int(maxoffsetangle / 2)
self.offsetAngleSpinBox.setMaximum(maxoffsetangle)
self.offsetAngleSpinBox.setMinimum(-maxoffsetangle)
# Layer for the rose diagrams based on the input polygon tiles
self.roseLayer = None
# ID field name - constant
self.idfieldname = 'ID'
self.svgfiles = [] # Array of SVG files - first element is None
self.result = None
self.ringcolour = QColor(153, 153, 255)
self.sectorcolour = QColor(240, 240, 240)
self.sectorcolourtrans = QColor(240, 240, 240, 0)
self.meandirections = []
self.strengths = []
def startWorker(self):
# self.showInfo('Ready to start worker')
# Get the input layer
layerindex = self.InputLayer.currentIndex()
layerId = self.InputLayer.itemData(layerindex)
inputlayer = QgsProject.instance().mapLayer(layerId)
if inputlayer is None:
self.showError(self.tr('No input layer defined'))
return
if inputlayer.featureCount() == 0:
self.showError(self.tr('No features in input layer'))
self.histscene.clear()
return
self.bins = self.binsSpinBox.value()
self.outputfilename = self.outputFile.text()
# self.showInfo("Outputfilename: " + str(self.outputfilename))
self.directionneutral = False
if self.directionNeutralCheckBox.isChecked():
self.directionneutral = True
self.offsetangle = self.offsetAngleSpinBox.value()
tilelayer = None
# If a tiling layer is used, create rose diagram layer
if self.useTilingCheckBox.isChecked():
layerindex = self.TilingLayer.currentIndex()
layerId = self.TilingLayer.itemData(layerindex)
tilelayer = QgsProject.instance().mapLayer(layerId)
if tilelayer is None:
self.showError(self.tr('No tile layer defined'))
self.histscene.clear()
return
if tilelayer.featureCount() == 0:
self.showError(self.tr('No features in tile layer'))
self.histscene.clear()
return
self.roseLayer = QgsVectorLayer('Point?crs=EPSG:4326',
"RoseDiagrams", "memory")
self.roseLayer.setCrs(tilelayer.crs())
# Add the ID field / attribute
self.roseLayer.dataProvider().addAttributes(
[QgsField(self.idfieldname, QVariant.Int)])
self.roseLayer.dataProvider().addAttributes(
[QgsField("meandir", QVariant.Double)])
self.roseLayer.dataProvider().addAttributes(
[QgsField("strength", QVariant.Double)])
self.roseLayer.updateFields()
# Add the IDs [1..number of "tiles"]
id = 1
for feature in tilelayer.getFeatures():
newfeature = QgsFeature()
centroid = feature.geometry().pointOnSurface()
newfeature.setGeometry(centroid)
newfeature.setAttributes([id, 0.0, 0.0])
self.roseLayer.dataProvider().addFeatures([newfeature])
id = id + 1
# Check the coordinate systems
# Different CRSs? - give a warning!
if (inputlayer is not None and tilelayer is not None and
inputlayer.crs() != tilelayer.crs()):
self.showWarning(
'Layers have different CRS! - Input CRS authid: ' +
str(inputlayer.crs().authid()) +
' - Tile CRS authid: ' +
str(tilelayer.crs().authid()))
# create a new worker instance
worker = Worker(inputlayer, self.bins, self.directionneutral,
self.offsetangle,
self.selectedFeaturesCheckBox.isChecked(),
tilelayer)
# # configure the QgsMessageBar
# msgBar = self.iface.messageBar().createMessage(self.tr('Joining'),
# '')
# self.aprogressBar = QProgressBar()
# self.aprogressBar.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
# acancelButton = QPushButton()
# acancelButton.setText(self.CANCEL)
# acancelButton.clicked.connect(self.killWorker)
# msgBar.layout().addWidget(self.aprogressBar)
# msgBar.layout().addWidget(acancelButton)
# # Has to be popped after the thread has finished (in
# # workerFinished).
# self.iface.messageBar().pushWidget(msgBar,
# self.iface.messageBar().INFO)
# self.messageBar = msgBar
# start the worker in a new thread
thread = QThread(self)
worker.moveToThread(thread)
worker.finished.connect(self.workerFinished)
worker.error.connect(self.workerError)
worker.status.connect(self.workerInfo)
worker.progress.connect(self.progressBar.setValue)
# worker.progress.connect(self.aprogressBar.setValue)
thread.started.connect(worker.run)
thread.start()
self.thread = thread
self.worker = worker
self.button_box.button(QDialogButtonBox.Ok).setEnabled(False)
self.button_box.button(QDialogButtonBox.Close).setEnabled(False)
self.button_box.button(QDialogButtonBox.Cancel).setEnabled(True)
self.saveAsPDFButton.setEnabled(False)
self.saveAsSVGButton.setEnabled(False)
self.copyToClipboardButton.setEnabled(False)
def workerFinished(self, ok, ret):
"""Handles the output from the worker and cleans up after the
worker has finished.
ok: indicates if the result is sensible
ret: array of bin arrays. The first bin array is for
the over all data. Each bin array contains the
statistics for a sector.
The first sector starts at (or close to) north (the
y-axis), and extends clockwise. If the user has
specified an offset, the sector starts either east
(negative offset) or west (positive offset) of
north."""
# clean up the worker and thread
self.worker.deleteLater()
self.thread.quit()
self.thread.wait()
self.thread.deleteLater()
# remove widget from the message bar (pop)
# self.iface.messageBar().popWidget(self.messageBar)
# Three outcomes:
# 1) No data returned
# 2) A vector with only one element: Draw the rose diagram
# 3) A vector with several elements: Create a layer
# with rose diagrams as symbols
if ok and ret is not None:
self.meandirections = []
self.strengths = []
self.showInfo("ret: " + str(ret))
# The first element is always the over all histogram
self.result = ret[0]
if len(ret) > 1: # Several elements - create SVG files for layer
# Initialise the vector of SVG files
self.svgfiles = [None] * len(ret)
# Determine the directory for storing the SVG files
tmpdir = tempfile.gettempdir()
if self.tileDirectory.text():
tmpdir = self.tileDirectory.text()
# Set the prefix for the SVG files
# tempfilepathprefix = tmpdir + '/qgisLDH_'
tempfilepathprefix = (tmpdir + '/qgisLDH_rose_' +
str(uuid.uuid4()))
categories = [] # Renderer categories
self.meandirstats = [] # For later saving to a CSV file
# Create the SVG files and symbols for the tiles
for i in range(len(ret) - 1):
# Set the global result variable to be used for
# drawing the histogram
self.result = ret[i + 1]
# Get the mean direction and strength for the tiles
if self.directionneutral:
(maxbin, strength) = self.semiCircMean()
angledeg = (maxbin + 0.5) * 180.0 / self.bins + self.offsetangle
if angledeg >= 180:
angledeg = angledeg - 180
if strength == 0:
angledeg = 0
self.meandirections.append(angledeg)
self.strengths.append(strength)
else:
(xvalue, yvalue) = self.circMean()
anglerad = math.atan2(xvalue, yvalue)
angledeg = math.degrees(anglerad)
if angledeg < 0:
angledeg = angledeg + 360
#if yvalue != 0:
# anglerad = math.atan2(xvalue, yvalue)
# angledeg = math.degrees(anglerad)
# if angledeg < 0:
# angledeg = 360 + angledeg
#elif xvalue != 0:
# if xvalue > 0:
# angledeg = 90
# else:
# angledeg = 270
#else:
# angledeg = 0.0
self.meandirections.append(angledeg)
strength = math.sqrt(xvalue*xvalue + yvalue*yvalue)
self.strengths.append(strength)
self.drawHistogram()
# Set the file name (and directory) for the SVG file
filename = (tempfilepathprefix + str(i + 1) + '.svg')
self.saveAsSVG(filename)
self.svgfiles[i + 1] = filename
# Create the symbol for this ID value
# initialize with the default symbol for this geom type
symbol = QgsSymbol.defaultSymbol(
self.roseLayer.geometryType())
# configure an (SVG) symbol layer
layer_style = {}
layer_style['fill'] = '#ffffff'
layer_style['name'] = filename
layer_style['outline'] = '#000000'
layer_style['outline-width'] = '6.8'
layer_style['size'] = '20'
sym_layer = QgsSvgMarkerSymbolLayer.create(layer_style)
# replace default symbol layer with the configured one
if sym_layer is not None:
symbol.changeSymbolLayer(0, sym_layer)
# create renderer category
category = QgsRendererCategory(i + 1, symbol,
str(i + 1))
categories.append(category)
# Calculate the mean directions (angle and strength)
strength = None
meandir = None
# Handle the direction neutral case (mean sector)
if (self.dirTrendCheckBox.isChecked() and
self.directionneutral):
(maxbin, strength) = self.semiCircMean()
meandir = maxbin
# Handle the non-direction neutral case (mean vector)
if (self.dirTrendCheckBox.isChecked() and
not self.directionneutral):
(circmeanx, circmeany) = self.circMean()
if circmeanx is not None:
strength = math.sqrt(circmeanx * circmeanx +
circmeany * circmeany)
if circmeany != 0:
meandir = math.degrees(math.atan(circmeanx /
circmeany))
if circmeanx > 0 and circmeany < 0:
meandir = 180.0 + meandir
elif circmeanx < 0 and circmeany < 0:
meandir = 180.0 + meandir
elif circmeanx < 0 and circmeany > 0:
meandir = 360.0 + meandir
#if meandir < 0:
# meandir = 360 + meandir
else:
meandir = math.degrees(math.pi * 0.5)
if circmeanx < 0:
meandir = math.degrees(math.pi * 1.5)
if meandir < 0:
meandir = 360 + meandir
self.meandirstats.append([i + 1, meandir, strength])
# update the rose layer
self.roseLayer.startEditing()
features = self.roseLayer.getFeatures()
dp = self.roseLayer.dataProvider()
mean_index = dp.fieldNameIndex("meandir")
strength_index = dp.fieldNameIndex("strength")
for f in features:
id = f.id()
self.showInfo("FID: " + str(id))
meandir = self.meandirections[id-1]
self.showInfo("meandir: " + str(meandir))
self.roseLayer.changeAttributeValue(id, mean_index, meandir)
strength = self.strengths[id-1]
self.showInfo("strength: " + str(strength))
self.roseLayer.changeAttributeValue(id, strength_index, strength)
#self.roseLayer.updateFeature(f)
self.roseLayer.commitChanges()
#
# create categorized renderer object
renderer = QgsCategorizedSymbolRenderer(self.idfieldname,
categories)
if renderer is not None:
self.roseLayer.setRenderer(renderer)
QgsProject.instance().addMapLayer(self.roseLayer)
# Set the result to the over all histogram
self.result = ret[0]
# Shall the result be reported (as a CSV file):
if self.outputfilename != "":
# Get the mean direction and strength for the tiles
if self.directionneutral:
(maxbin, strength) = self.semiCircMean()
angledeg = (maxbin + 0.5) * 180.0 / self.bins + self.offsetangle
if angledeg >= 180:
angledeg = angledeg - 180
if strength == 0:
angledeg = 0
else:
(xvalue, yvalue) = self.circMean()
anglerad = math.atan2(xvalue, yvalue)
angledeg = math.degrees(anglerad)
if angledeg < 0:
angledeg = angledeg + 360
strength = math.sqrt(xvalue*xvalue + yvalue*yvalue)
try:
with open(self.outputfilename, 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=';',
quotechar='"', quoting=csv.QUOTE_MINIMAL)
if (self.useTilingCheckBox.isChecked() and
self.meanDirectionRB.isChecked()):
# Mean directions for the tiles - write to file
csvwriter.writerow(["Id", "Direction", "Strength"])
for i in range(len(self.meandirstats)):
if (self.directionneutral and
self.meandirstats[i][1] is not None):
angle = ((self.meandirstats[i][1] + 0.5) *
180.0 / self.bins +
self.offsetangle)
else:
angle = self.meandirstats[i][1]
csvwriter.writerow([self.meandirstats[i][0],
angle,
self.meandirstats[i][2]])
with open(self.outputfilename + 't', 'w') as csvtfile:
csvtfile.write('"Integer","Real","Real"')
elif self.histogramRB.isChecked():
csvwriter.writerow(["StartAngle", "EndAngle",
"Length", "Number", "Meandir", "Strength"])
for i in range(len(ret[0])):
if self.directionneutral:
angle = (i * 180.0 / self.bins +
self.offsetangle)
csvwriter.writerow([angle,
angle + 180.0 / self.bins,
ret[0][i][0], ret[0][i][1],
angledeg, strength])
else:
angle = (i * 360.0 / self.bins +
self.offsetangle)
csvwriter.writerow([angle,
angle + 360.0 / self.bins,
ret[0][i][0], ret[0][i][1],
angledeg, strength])
with open(self.outputfilename + 't', 'w') as csvtfile:
csvtfile.write('"Real","Real","Real","Integer","Real","Real"')
except IOError as e:
self.showInfo("Trouble writing the CSV file: " + str(e))
# Draw the histogram
self.drawHistogram()
else: # No data returned
# notify the user that something went wrong
if not ok:
self.showError(self.tr('Aborted') + '!')
else:
self.showError(self.tr('No histogram created') + '!')
# Update the user interface
self.progressBar.setValue(0)
self.button_box.button(QDialogButtonBox.Ok).setEnabled(True)
self.button_box.button(QDialogButtonBox.Close).setEnabled(True)
self.button_box.button(QDialogButtonBox.Cancel).setEnabled(False)
self.saveAsPDFButton.setEnabled(True)
self.saveAsSVGButton.setEnabled(True)
self.copyToClipboardButton.setEnabled(True)
# end of workerFinished(self, ok, ret)
def workerError(self, exception_string):
"""Report an error from the worker."""
# QgsMessageLog.logMessage(self.tr('Worker failed - exception') +
# ': ' + str(exception_string),
# self.LINEDIRECTIONHISTOGRAM,
# Qgis.Critical)
self.showError(self.tr('Worker failed - exception') +
': ' + exception_string)
def workerInfo(self, message_string):
"""Report an info message from the worker."""
# QgsMessageLog.logMessage(self.tr('Worker') + ': ' +
# message_string,
# self.LINEDIRECTIONHISTOGRAM,
# Qgis.Info)
self.showInfo(self.tr('Worker') + ': ' + message_string)
def killWorker(self):
"""Kill the worker thread."""
if self.worker is not None:
QgsMessageLog.logMessage(self.tr('Killing worker'),
self.LINEDIRECTIONHISTOGRAM,
Qgis.Info)
self.worker.kill()
def giveHelp(self):
# self.showInfo('Giving help')
QDesktopServices.openUrl(QUrl.fromLocalFile(
self.plugin_dir + "/help/html/index.html"))
# showPluginHelp(None, "help/html/index")
# end of giveHelp
# Implement the reject method to have the possibility to avoid
# exiting the dialog when cancelling
def reject(self):
"""Reject override."""
# exit the dialog
QDialog.reject(self)
def browse(self):
outpath = saveCSVDialog(self)
self.outputFile.setText(outpath)
def browseTile(self):
outpath = findTileDialog(self)
self.tileDirectory.setText(outpath)
def noWeighting(self):
if self.result is not None:
self.drawHistogram()
def proportionalArea(self):
if self.result is not None:
self.drawHistogram()
def logaritmic(self):
if self.result is not None:
self.drawHistogram()
def drawHistogram(self):
# self.result shall contain the bins. The first bin
# starts at north + offsetangle, continuing clockwise
if self.result is None:
return
# self.showInfo(str(self.result))
# Check which element should be used for the histogram
element = 0
if self.noWeightingCheckBox.isChecked():
element = 1
# Find the maximum direction value for scaling
maxvalue = 0.0
for i in range(len(self.result)):
if self.result[i][element] > maxvalue:
maxvalue = self.result[i][element]
self.histscene.clear()
if maxvalue == 0:
return
if self.logaritmicCheckBox.isChecked():
maxvalue = math.log1p(maxvalue)
if self.proportionalAreaCheckBox.isChecked():
maxvalue = math.sqrt(maxvalue)
viewprect = QRectF(self.histogramGraphicsView.viewport().rect())
self.histogramGraphicsView.setSceneRect(viewprect)
bottom = self.histogramGraphicsView.sceneRect().bottom()
top = self.histogramGraphicsView.sceneRect().top()
left = self.histogramGraphicsView.sceneRect().left()
right = self.histogramGraphicsView.sceneRect().right()
height = bottom - top
width = right - left
size = width
if width > height:
size = height
padding = 3
maxlength = size / 2.0 - padding * 2
center = QPointF(left + width / 2.0, top + height / 2.0)
# The scene geomatry of the center point
start = QPointF(self.histogramGraphicsView.transform().map(center))
# Create some concentric rings as background:
if self.drawCirclesCB.isChecked():
for i in range(self.NUMBEROFRINGS):
step = maxlength / self.NUMBEROFRINGS
radius = step * (i + 1)
circle = QGraphicsEllipseItem(start.x() - radius,
start.y() - radius,
radius * 2.0,
radius * 2.0)
circle.setPen(QPen(self.ringcolour))
self.histscene.addItem(circle)
#else:
# radius = maxlength
# circle = QGraphicsEllipseItem(start.x() - radius,
# start.y() - radius,
# radius * 2.0,
# radius * 2.0)
# circle.setPen(QPen(self.ringcolour))
# self.histscene.addItem(circle)
# Get circular statistics for the direction neutral case
maxbin = -1
strength = -1.0
if (self.dirTrendCheckBox.isChecked() and self.directionneutral):
(maxbin, strength) = self.semiCircMean()
# Create the sectors of the Rose diagram
sectorwidth = 360.0 / self.bins
if self.directionneutral:
sectorwidth = sectorwidth / 2.0
for i in range(self.bins):
# Find the length of the sector
currentLength = self.result[i][element]
if self.logaritmicCheckBox.isChecked():
currentLength = math.log1p(currentLength)
if self.proportionalAreaCheckBox.isChecked():
currentLength = math.sqrt(currentLength)
linelength = maxlength * currentLength / maxvalue
# Start angle for sector i
# Working on Qt angles (0 = east, counter-clockwise)
angle = 90 - i * sectorwidth - self.offsetangle
# Draw the sector
if not self.directionneutral and self.lineDirCB.isChecked():
sector = QGraphicsEllipseItem(start.x() - linelength,
start.y() - linelength,
linelength * 2.0,
linelength * 2.0)
sector.setStartAngle(int(16 * angle))
sector.setSpanAngle(int(16 * (-sectorwidth)))
sector.setBrush(QBrush(self.sectorcolour))
self.histscene.addItem(sector)
else:
# Shall direction trend be indicated
if (self.dirTrendCheckBox.isChecked() and
i == maxbin):
# Show the direction trend for this bin using maxlength:
sector = QGraphicsEllipseItem(start.x() - maxlength,
start.y() - maxlength,
maxlength * 2.0,
maxlength * 2.0)
sector.setStartAngle(int(16 * angle))
sector.setSpanAngle(int(16 * (-sectorwidth)))
basecolourhue = self.colorButton.color().hue()
buttoncolour = QColor.fromHsv(basecolourhue, 255, 255)
self.colorButton.setColor(buttoncolour)
# Use a red tone according to the strength
colourintensity = 255 - (strength * 255)
trendcolour = QColor.fromHsv(basecolourhue,
int(strength * 255), 255)
sector.setBrush(QBrush(trendcolour))
myPen = QPen(QPen(trendcolour))
myPen.setWidth(1)
sector.setPen(myPen)
self.histscene.addItem(sector)
# The sector in the opposite direction
sector = QGraphicsEllipseItem(start.x() - maxlength,
start.y() - maxlength,
maxlength * 2.0,
maxlength * 2.0)
sector.setStartAngle(int(16 * (270.0 - i * sectorwidth -
self.offsetangle)))
sector.setSpanAngle(int(16 * (-sectorwidth)))
sector.setBrush(QBrush(trendcolour))
myPen = QPen(QPen(trendcolour))
myPen.setWidth(1)
sector.setPen(myPen)
self.histscene.addItem(sector)
# Shall the rose diagrams be included
if self.lineDirCB.isChecked():
# Draw the rose diagram sector according to the value
sector = QGraphicsEllipseItem(start.x() - linelength,
start.y() - linelength,
linelength * 2.0,
linelength * 2.0)
sector.setStartAngle(int(16 * angle))
sector.setSpanAngle(int(16 * (-sectorwidth)))
if self.dirTrendCheckBox.isChecked():
sector.setBrush(QBrush(self.sectorcolourtrans))
else:
sector.setBrush(QBrush(self.sectorcolour))
self.histscene.addItem(sector)
# The sector in the opposite direction
sector = QGraphicsEllipseItem(start.x() - linelength,
start.y() - linelength,
linelength * 2.0,
linelength * 2.0)
sector.setStartAngle(int(16 * (270.0 - i * sectorwidth -
self.offsetangle)))
sector.setSpanAngle(int(16 * (-sectorwidth)))
if self.dirTrendCheckBox.isChecked():
sector.setBrush(QBrush(self.sectorcolourtrans))
else:
sector.setBrush(QBrush(self.sectorcolour))
self.histscene.addItem(sector)
if not self.directionneutral and self.dirTrendCheckBox.isChecked():
# Get the mean
(circmeanx, circmeany) = self.circMean()
# Draw a point
radius = 4
ptcircle = QGraphicsEllipseItem(
start.x() + circmeanx * maxlength - radius,
start.y() - circmeany * maxlength - radius,
radius * 2.0, radius * 2.0)
# Draw the line that connects the point to the centre
dirLine = QGraphicsLineItem(start.x(), start.y(),
start.x() + circmeanx *
maxlength,
start.y() - circmeany *
maxlength)
myPen = QPen(self.meanvectorcolour)
myPen.setWidth(5)
myPen.setCapStyle(Qt.FlatCap)
dirLine.setPen(myPen)
ptcircle.setPen(QPen(self.meanvectorcolour))
ptcircle.setBrush(QBrush(self.meanvectorcolour))
self.histscene.addItem(ptcircle)
self.histscene.addItem(dirLine)
# Calculate the circular mean for the current result.
# Returns the normalised vector (x,y) - in QT graphics view
# coordinates (east = 0, counter-clockwise).
# Must not be applied in direction neutral mode.
def circMean(self):
sectorwidth = 360.0 / self.bins
if self.directionneutral: # Should not happen
sectorwidth = sectorwidth / 2.0
element = 0
if self.noWeightingCheckBox.isChecked():
element = 1
sumx = 0 # sum of x values
sumy = 0 # sum of y values
sumlinelength = 0 # sum of line lengths
for i in range(self.bins):
# Get the accumulated line length for the sector
linelength = self.result[i][element]
# Accumulate line length
sumlinelength = sumlinelength + linelength
# Set the start angle for sector i
# Angles are geographic (0 = north, clockwise)
# Working on Qt angles (0 = east, counter-clockwise)
angle = 90 - ((i + 0.5) * sectorwidth + self.offsetangle)
addx = linelength * math.cos(math.radians(angle))
addy = linelength * math.sin(math.radians(angle))
sumx = sumx + addx
sumy = sumy + addy
# Directional statistics
if sumlinelength == 0:
return (0, 0)
else:
normsumx = sumx / sumlinelength
normsumy = sumy / sumlinelength
return (normsumx, normsumy)
# Approximate the direction neutral circular mean for the current
# result by returning the bin / sector number (starting at 0)
# that gives the maximum semi-circular mean together with the
# strength ([0..1]) of the direction trend.
# Must only be applied in direction neutral mode.
def semiCircMean(self):
# oddnumberofbins = self.bins % 2
sectorwidth = 360.0 / self.bins
if self.directionneutral: # Should always be the case
sectorwidth = sectorwidth / 2.0
# Should line length of number of lines be used:
element = 0
if self.noWeightingCheckBox.isChecked():
element = 1
# Calculate the circle reference values for normalisation
# The "border" sectors will have an angle of 90 deg to the
# reference sector for even numbers of sectors, and will
# therefore not contribute.
refangle = (180.0 / self.bins) * (0.5 + self.bins // 2)
totalsum = 0 # sum of all the bin values
totalx = 0 # sum of the horizontal components (unit)
for i in range(self.bins):
angle = (180.0 / self.bins) * (0.5 + i)
xvalue = math.cos(math.radians(angle - refangle))
totalx = totalx + xvalue
binvalue = self.result[i][element]
totalsum = totalsum + binvalue
refmagnitude = totalx / self.bins
# For each bin direction, calculate the semi-circular statistics
maxvalue = 0 # Maximum normalised value
maxbin = 0 # Bin with maximum normalised value
for j in range(self.bins):
sumx = 0 # sum of x values
# Set the mean compass angle for sector j
binangle = (j + 0.5) * sectorwidth + self.offsetangle
for i in range(self.bins):
# Get the accumulated line length for the sector
linelength = self.result[i][element]
# Set the mean compass angle for sector i
angle = (i + 0.5) * sectorwidth + self.offsetangle
anglediff = angle - binangle
# Wrap around?
if (anglediff > 90):
anglediff = 180 - anglediff
elif (anglediff < -90):
anglediff = 180 + anglediff
addx = (linelength * math.cos(math.radians(anglediff)))
sumx = sumx + addx
if sumx > maxvalue:
maxvalue = sumx
maxbin = j
if totalsum == 0:
return (0, 0)
else:
# Normalise to [0..1]
normalmax = maxvalue / totalsum
# Adjust the according to the lowest achievable value
adjustedmax = (normalmax - refmagnitude) / (1 - refmagnitude)
return (maxbin, adjustedmax)
# React to changes to the directional trend checkbox
def trend(self):
if self.dirTrendCheckBox.isChecked():
self.lineDirCB.setEnabled(True)
else:
self.lineDirCB.setEnabled(False)
self.lineDirCB.setChecked(True)
# Update the visualisation of the bin structure,
# update UI components
# and set global variable self.bins
def updateBins(self):
self.directionneutral = False
if self.directionNeutralCheckBox.isChecked():
self.directionneutral = True
self.bins = self.binsSpinBox.value()
if self.bins < 2:
self.bins = 2
self.binsSpinBox.setValue(self.bins)
maxoffsetangle = int(360 / self.bins)
if self.directionneutral:
maxoffsetangle = int(maxoffsetangle / 2)
self.offsetAngleSpinBox.setMaximum(maxoffsetangle)
self.offsetAngleSpinBox.setMinimum(-maxoffsetangle)
self.offsetangle = self.offsetAngleSpinBox.value()
if self.offsetangle > maxoffsetangle:
self.offsetAngleSpinBox.setValue(maxoffsetangle)
self.offsetangle = maxoffsetangle
elif self.offsetangle < -maxoffsetangle:
self.offsetAngleSpinBox.setValue(-maxoffsetangle)
self.offsetangle = -maxoffsetangle
self.setupScene.clear()
self.setupScene.update()
viewprect = QRectF(self.setupGraphicsView.viewport().rect())
self.setupGraphicsView.setSceneRect(viewprect)
bottom = self.setupGraphicsView.sceneRect().bottom()
top = self.setupGraphicsView.sceneRect().top()
left = self.setupGraphicsView.sceneRect().left()
right = self.setupGraphicsView.sceneRect().right()
height = bottom - top
width = right - left
size = width
if width > height:
size = height
padding = 3.0
maxlength = size / 2.0 - padding
center = QPointF(left + width / 2.0, top + height / 2.0)
start = QPointF(self.setupGraphicsView.transform().map(center))
# Create some concentric rings:
setuprings = self.NUMBEROFRINGS // 2
for i in range(setuprings):
step = maxlength / setuprings
radius = step * (i + 1)
circle = QGraphicsEllipseItem(start.x() - radius,
start.y() - radius,
radius * 2.0,
radius * 2.0)
circle.setPen(QPen(self.ringcolour))
self.setupScene.addItem(circle)
for i in range(self.bins):
linelength = maxlength
angle = 90 - i * 360.0 / self.bins - self.offsetangle
if self.directionneutral:
angle = 90.0 - i * 180.0 / self.bins - self.offsetangle
directedline = QLineF.fromPolar(linelength, angle)
topt = center + QPointF(directedline.x2(),
directedline.y2())
end = QPointF(self.setupGraphicsView.transform().map(center))
if self.directionneutral:
otherpt = center - QPointF(directedline.x2(),
directedline.y2())
mirrorpt = QPointF(self.setupGraphicsView.transform().map(center))
self.setupScene.addItem(QGraphicsLineItem(
QLineF(mirrorpt, end)))
else:
self.setupScene.addItem(QGraphicsLineItem(QLineF(start, end)))
# end of updatebins
# def layerlistchanged(self):
# self.layerlistchanging = True
# # Repopulate the input and join layer combo boxes
# # Save the currently selected input layer
# inputlayerid = self.inputlayerid
# self.InputLayer.clear()
# # We are only interested in line and polygon layers
# for alayer in self.iface.legendInterface().layers():
# if alayer.type() == QgsMapLayer.VectorLayer:
# if (alayer.geometryType() == QGis.Line or
# alayer.geometryType() == QgsWkbTypes.PolygonGeometry):
# self.InputLayer.addItem(alayer.name(), alayer.id())
# # Set the previous selection
# for i in range(self.InputLayer.count()):
# if self.InputLayer.itemData(i) == inputlayerid:
# self.InputLayer.setCurrentIndex(i)
# self.layerlistchanging = False
def inputLayerChanged(self):
layerindex = self.InputLayer.currentIndex()
layerId = self.InputLayer.itemData(layerindex)
inputlayer = QgsProject.instance().mapLayer(layerId)
if inputlayer is None:
return
if inputlayer.featureCount() == 0:
self.showInfo(self.tr('No features in input layer'))
return
# If there are no selected features, the "selected features
# only" checkbox should be unchecked
if inputlayer.selectedFeatureCount() == 0:
self.selectedFeaturesCheckBox.setChecked(False)
else:
self.selectedFeaturesCheckBox.setChecked(True)
def showError(self, text):
"""Show an error."""
# self.iface.messageBar().pushMessage(self.tr('Error'), text,
# level=QgsMessageBar.CRITICAL,