-
Notifications
You must be signed in to change notification settings - Fork 11
/
SectionGrid.py
253 lines (199 loc) · 9 KB
/
SectionGrid.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
# -*- coding: utf-8 -*-
"""
Created on Tue May 21 17:16:49 2019
@author: HillR
"""
from PyQt5.QtCore import QVariant
from qgis.gui import QgsMapToolEmitPoint, QgsMessageBar
from qgis.core import Qgis, QgsWkbTypes, QgsProject
from PyQt5 import QtCore, Qt, QtGui
from .Utils import *
from .SectionManager import *
# The DrillManager class controls all drill related data and methods
class SectionGrid:
def __init__(self, section, crs):
self.section = section
self.crs = crs
def create(self):
layers = []
# These are real world coordinates
dx = self.section.maxX - self.section.minX
dy = self.section.maxY - self.section.minY
# Approximate number of grid lines to display
numGridLines = 3.0
# This is section2D cooridnates
extent = groupExtent(self.section.group)
extent.grow(10)
# Build the X grid
name = self.createSectionGridLayerName(self.section.name) + 'X'
layer = self.section.matchDecorationLayer(name)
usingNewLayer = False
if layer == None:
layer = self.createSectionGridLayer(name, self.crs)
usingNewLayer = True
else:
clearLayer(layer)
intx = 0
if dx != 0:
intx = gridInterval(dx / numGridLines)
x = gridStart(self.section.minX, intx)
# msg = "dx: %f intx: %f" % (dx, intx)
# iface.messageBar().pushMessage("Debug", msg, level=Qgis.Warning)
while x < self.section.maxX:
y = ((x - self.section.startX)/(self.section.endX - self.section.startX)) * (self.section.endY - self.section.startY) + self.section.startY
pt = np.array([x - self.section.startX, y - self.section.startY, 0.0])
rpt = self.section.quat.rotate(pt)
pointList = []
pointList.append(QgsPoint(rpt[0], extent.yMaximum(), 0.0))
pointList.append(QgsPoint(rpt[0], extent.yMinimum(), 0.0))
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPolyline(pointList))
# Set the attributes for the new feature
f.setAttributes([x])
# Add the new feature to the new Trace_ layer
layer.startEditing()
layer.addFeature(f)
layer.commitChanges()
x = x + intx
layers.append(layer)
if usingNewLayer:
self.setStyle(layer, 'E')
QgsProject.instance().addMapLayer(layer, False)
self.section.decorationGroup().addLayer(layer)
if dx < dy*0.25:
QgsProject.instance().layerTreeRoot().findLayer(layer.id()).setItemVisibilityChecked( False )
# Build the Y grid
name = self.createSectionGridLayerName(self.section.name) + 'Y'
layer = self.section.matchDecorationLayer(name)
usingNewLayer = False
if layer == None:
layer = self.createSectionGridLayer(name, self.crs)
usingNewLayer = True
else:
clearLayer(layer)
inty = 0
if dy != 0:
inty = gridInterval(dy / numGridLines)
y = gridStart(self.section.minY, inty)
while y < self.section.maxY:
x = ((y - self.section.startY)/(self.section.endY - self.section.startY)) * (self.section.endX - self.section.startX) + self.section.startX
pt = np.array([x - self.section.startX, y - self.section.startY, 0.0])
rpt = self.section.quat.rotate(pt)
pointList = []
pointList.append(QgsPoint(rpt[0], extent.yMaximum(), 0.0))
pointList.append(QgsPoint(rpt[0], extent.yMinimum(), 0.0))
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPolyline(pointList))
# Set the attributes for the new feature
f.setAttributes([y])
# Add the new feature to the new Trace_ layer
layer.startEditing()
layer.addFeature(f)
layer.commitChanges()
y = y + inty
layers.append(layer)
if usingNewLayer:
self.setStyle(layer, 'N')
QgsProject.instance().addMapLayer(layer, False)
self.section.decorationGroup().addLayer(layer)
if dy < dx*0.25:
QgsProject.instance().layerTreeRoot().findLayer(layer.id()).setItemVisibilityChecked( False )
# Build the Z grid
name = self.createSectionGridLayerName(self.section.name) + 'Z'
layer = self.section.matchDecorationLayer(name)
usingNewLayer = False
if layer == None:
layer = self.createSectionGridLayer(name, self.crs)
usingNewLayer = True
else:
clearLayer(layer)
intz = max(intx, inty)
z = gridStart(extent.yMinimum(), intz)
while z < extent.yMaximum():
pointList = []
pointList.append(QgsPoint(extent.xMinimum(), z, 0.0))
pointList.append(QgsPoint(extent.xMaximum(), z, 0.0))
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPolyline(pointList))
# Set the attributes for the new feature
f.setAttributes([z])
# Add the new feature to the new Trace_ layer
layer.startEditing()
layer.addFeature(f)
layer.commitChanges()
z = z + intz
layers.append(layer)
if usingNewLayer:
self.setStyle(layer, 'RL')
QgsProject.instance().addMapLayer(layer, False)
self.section.decorationGroup().addLayer(layer)
# Build the Border
name = self.createSectionGridLayerName(self.section.name) + 'Border'
layer = self.section.matchDecorationLayer(name)
usingNewLayer = False
if layer == None:
layer = self.createSectionGridLayer(name, self.crs)
usingNewLayer = True
else:
clearLayer(layer)
pointList = []
pointList.append(QgsPoint(extent.xMinimum(), extent.yMinimum(), 0.0))
pointList.append(QgsPoint(extent.xMinimum(), extent.yMaximum(), 0.0))
pointList.append(QgsPoint(extent.xMaximum(), extent.yMaximum(), 0.0))
pointList.append(QgsPoint(extent.xMaximum(), extent.yMinimum(), 0.0))
pointList.append(QgsPoint(extent.xMinimum(), extent.yMinimum(), 0.0))
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPolyline(pointList))
# Set the attributes for the new feature
f.setAttributes([0.0])
# Add the new feature to the new Trace_ layer
layer.startEditing()
layer.addFeature(f)
layer.commitChanges()
layers.append(layer)
if usingNewLayer:
self.setStyle(layer, labels=False)
QgsProject.instance().addMapLayer(layer, False)
self.section.decorationGroup().addLayer(layer)
return layers
def setStyle(self, layer, labelSuffix='', labels=True):
# Set the line style
r = layer.renderer()
r.symbol().setColor(QtGui.QColor('grey'))
r.symbol().setOpacity(0.5)
#Set the label style
if labels:
settings = QgsPalLayerSettings()
textFormat = QgsTextFormat()
textFormat.setFont(QtGui.QFont("Arial", 10))
textFormat.setColor(QtGui.QColor('grey'))
textFormat.setOpacity(0.5)
settings.setFormat(textFormat)
if labelSuffix == '':
settings.fieldName = 'Label'
else:
# settings.fieldName = 'Label'
settings.fieldName = str.format("format( '%%1%%2', format_number(Label, 0), ' %s')" % labelSuffix)
settings.isExpression = True
settings.formatNumbers = True
settings.decimals = 0
settings.placement = QgsPalLayerSettings.Line
labeling = QgsVectorLayerSimpleLabeling(settings)
layer.setLabeling(labeling)
layer.setLabelsEnabled(True)
def createSectionGridLayerName(self, sectionName):
name = "S_" + sectionName + "_Grid"
return name
def createSectionGridLayer(self, name, crs):
#Create a new memory layer
layer = QgsVectorLayer("LineStringZ?crs=EPSG:4326", name, "memory")
layer.setCrs(crs)
atts = []
# Loop through the list of desired field names that the user checked
atts.append(QgsField("Label", QVariant.Double, "double", 9, 3))
# Add all the attributes to the new layer
dp = layer.dataProvider()
dp.addAttributes(atts)
# Tell the vector layer to fetch changes from the provider
layer.updateFields()
return layer