-
Notifications
You must be signed in to change notification settings - Fork 11
/
geoscience.py
297 lines (241 loc) · 10.1 KB
/
geoscience.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Geoscience
A QGIS plugin
Tools for Geoscience & Exploration
-------------------
begin : 2018-04-13
git sha : $Format:%H$
copyright : (C) 2018 by Roland Hill / MMG
email : [email protected]
***************************************************************************/
"""
from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication, QVariant, QUrl
from PyQt5.QtGui import QIcon, QDesktopServices
from PyQt5.QtWidgets import QAction, QMenu, QDialog
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
# Initialize Qt resources from file resources.py
from .resources import *
# from .ChangeDriveLetter_dialog import ChangeDriveLetterDialog
from .DrillManager import *
from .localgrid_dialog import LocalGridDialog
import os
class Geoscience:
"""
Main class for the Geoscience plugin.
"""
def __init__(self, iface):
"""
Constructor for Geoscience class.
Args:
iface: QGIS interface object.
"""
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(self.plugin_dir, 'i18n', 'Geoscience_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Initiate the DrillManager to handle all drill related operations
self.drillManager = DrillManager()
# Read all saved Geoscience parameters from the QGIS project file
self.readProjectData()
# Declare instance attributes
self.actions = []
self.toolbar = self.iface.addToolBar(u'Geoscience')
self.toolbar.setObjectName(u'Geoscience')
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
Args:
param message: String for translation.
type message: str, QString
Returns:
Translated version of message.
rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('Geoscience', message)
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# Respond to signal so that we read saved parameters every time a Project is loaded
QgsProject.instance().readProject.connect(self.onReadProject)
actions = self.iface.mainWindow().menuBar().actions()
# Create main menu.
lastAction = actions[-1]
self.menu = QMenu( u'&Geoscience', self.iface.mainWindow().menuBar() )
self.iface.mainWindow().menuBar().insertMenu( lastAction, self.menu )
# Create Drill menu.
self.menuDrill = self.menu.addMenu("Drilling")
action = self.menuDrill.addAction(QIcon(self.plugin_dir + "/icon/Desurvey.png"), "Desurvey Holes...")
action.triggered.connect(self.drillManager.onDesurveyHole)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
action = self.menuDrill.addAction(QIcon(self.plugin_dir + "/icon/DrillPlan.png"), "Downhole Data...")
action.triggered.connect(self.drillManager.onDownholeData)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
action = self.menuDrill.addAction(QIcon(self.plugin_dir + "/icon/DrillPoints.png"), "Downhole Points...")
action.triggered.connect(self.drillManager.onDownholePoints)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
action = self.menuDrill.addAction(QIcon(self.plugin_dir + "/icon/DrillStructure.png"), "Downhole Structure...")
action.triggered.connect(self.drillManager.onDownholeStructure)
action.setEnabled(True)
self.actions.append(action)
action = self.menuDrill.addAction(QIcon(self.plugin_dir + "/icon/DrillSection.png"), "Section Manager...")
action.triggered.connect(self.drillManager.onDrillSectionManager)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
self.toolbar.addSeparator()
# Create Vector menu.
self.menuVector = self.menu.addMenu("Vector")
action = self.menuVector.addAction(QIcon(self.plugin_dir + "/icon/ReverseLine.png"), "Reverse Line Direction")
action.triggered.connect(self.onReverseLine)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
# Create Raster menu.
self.menuRaster = self.menu.addMenu("Raster")
action = self.menuRaster.addAction(QIcon(self.plugin_dir + "/icon/WhiteTransparent.png"), "Transparent White")
action.triggered.connect(self.onRasterTransparentWhite)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
action = self.menuRaster.addAction(QIcon(self.plugin_dir + "/icon/BlackTransparent.png"), "Transparent Black")
action.triggered.connect(self.onRasterTransparentBlack)
action.setEnabled(True)
self.toolbar.addAction(action)
self.actions.append(action)
# Other choices.
action = self.menu.addAction(QIcon(self.plugin_dir + "/icon/LocalGrid.png"), "Create Local CRS...")
action.triggered.connect(self.onCreateLocalCRS)
action.setEnabled(True)
self.actions.append(action)
# Create Help menu.
action = self.menu.addAction(QIcon(self.plugin_dir + "/icon/Help.png"), "Help")
action.triggered.connect(self.onHelp)
action.setEnabled(True)
self.actions.append(action)
def onHelp(self):
"""
Open the help documentation in a web browser.
"""
docDir = "https://www.spatialintegration.com/"
QDesktopServices.openUrl(QUrl(docDir))
def onReadProject(self):
"""
Read project data.
"""
self.drillManager.readProjectData()
def onReverseLine(self):
"""
Reverse the node sequence in all selected lines on the current layer.
Useful if you are using a non-symmetric line style (eg Normal Fault).
"""
# Get the currently selected layer
layer = self.iface.mapCanvas().currentLayer()
# Loop through all selected features
for feature in layer.selectedFeatures():
geom = feature.geometry()
wkbType = geom.wkbType()
if wkbType == QgsWkbTypes.MultiLineString:
for nodes in geom.asMultiPolygon():
nodes.reverse()
else:
# Get the geometry as a Polyline. This is 2D only
# ToDo: Convert to 3D
nodes = geom.asPolyline()
nodes.reverse()
newgeom = QgsGeometry.fromPolylineXY(nodes)
# Put the new geometry into the feature
layer.changeGeometry(feature.id(),newgeom)
# Refresh the screen
layer.triggerRepaint()
def onRasterTransparentWhite(self):
"""
Set the white pixels of all selected raster layers to be transparent.
"""
self.rasterTransparent(255, 255, 255)
def onRasterTransparentBlack(self):
"""
Set the black pixels of all selected raster layers to be transparent.
"""
self.rasterTransparent(0, 0, 0)
def rasterTransparent(self, r, g, b):
"""
Set the pixels of the supplied color of all selected raster layers to be transparent.
Args:
r (int): Red value.
g (int): Green value.
b (int): Blue value.
"""
tr_list = []
# Create a transparent pixel value and add it to the list
ltr = QgsRasterTransparency.TransparentThreeValuePixel()
ltr.red = r
ltr.green = g
ltr.blue = b
ltr.percentTransparent = 100
tr_list.append(ltr)
# Get all selected layers
if Qgis.QGIS_VERSION_INT < 30000 :
sl = self.iface.legendInterface().selectedLayers(True)
else:
sl = self.iface.layerTreeView().selectedLayers()
# Loop through the layers
for layer in sl:
# Get the renderer and set the transparent pixel list to ours
raster_transparency = layer.renderer().rasterTransparency()
raster_transparency.setTransparentThreeValuePixelList(tr_list)
# Repaint the screen
layer.triggerRepaint()
def onCreateLocalCRS(self):
"""
Create a local Coordinate Reference System (CRS).
"""
self.createLocalCRS()
def createLocalCRS(self):
"""
Create a local Coordinate Reference System (CRS).
"""
dlg = LocalGridDialog(self)
dlg.exec_()
dlg.close()
def readProjectData(self):
"""
Read project data.
"""
pass # Placeholder for the method
def writeProjectData(self):
"""
Write project data.
"""
pass # Placeholder for the method
def unload(self):
"""
Remove interface items when the plugin closes.
"""
self.iface.mainWindow().menuBar().removeAction(self.menu.menuAction())
del self.menu
# remove the toolbar
del self.toolbar
def run(self):
"""
Default method called to do something.
Wait for signals from the menus and toolbar
"""
pass # Placeholder for the method