/
ymactools2.py
239 lines (208 loc) · 10.3 KB
/
ymactools2.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
YMACTools2
A QGIS plugin
YMAC Tools for QGIS v 2.x
-------------------
begin : 2015-08-12
copyright : (C) 2014 by GIS Apps, Dept of Parks and Wildlife
email : Patrick.Maslen@dpaw.wa.gov.au
edited : Cameron Poole (cjpoole@ymac.org.au)
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources_rc
# Import the code for the dialog
from ymactools2dialog import YMACTools2Dialog
from os import makedirs, path
import time
import win32com.client # Used to obtain V: drive address on starting QGIS (see QGISTools2.__init__)
import shutil # Used to copy project_default.qgs from resources folder to new user's .qgis2 folder
from ymac.gis.qgis.ui.dockablewindow import DockableWindow
from ymac.gis.qgis.tools import Tools
from ymac.gis.qgis.mapproduction.mapproduction import MapProduction
from ymac.gis.qgis.ui.showProjectTemplateDialog import ShowProjectTemplateDialog
# import sys
# sys.path.append("C:\\Users\\patrickm\\Desktop\\GISTools\\eclipseNEW\\plugins\\org.python.pydev_3.8.0.201409251235\\pysrc\\")
# from pydevd import*
class YMACTools2:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
Tools.iface = iface
self.templateDialog = None
# initialize plugin directory
self.plugin_dir = path.normpath(path.join(path.dirname(__file__), ("..")))
#Tools.debug(self.plugin_dir)
#Tools.debug(str(QgsApplication.pluginPath()).replace("\\", "/").rsplit("/", 1)[0] + "/python/plugins/qgistools")
if not QFileInfo(self.plugin_dir).exists():
self.plugin_dir = str(QgsApplication.pluginPath()).replace("\\", "/").rsplit("/", 1)[0] + "/python/plugins/ymactools2"
# Check whether this user has a .qgis2 folder containing project_default.qgs
userProjDefaultLocation = "C:/Users/" + Tools.username + "/.qgis2/project_default.qgs"
pluginProjDefaultLocation = path.dirname(__file__) + "/resources/project_default_YMAC.qgs"
if not path.exists(userProjDefaultLocation):
shutil.copyfile(pluginProjDefaultLocation, userProjDefaultLocation)
# initialize locale
locale = QSettings().value("locale/userLocale")[0:2]
localePath = path.join(self.plugin_dir, 'i18n', 'ymactools2_{}.qm'.format(locale))
if path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = YMACTools2Dialog() #commented out PWM
# Upload local WMS counter data to central counter
# Check if local WMS log file exists; if not then create it.
try:
makedirs(Tools.localLogFolder)
except:
pass
localWmsFilename = path.normpath(Tools.localLogFolder) + Tools.localWMSLogFilename
if not path.exists(localWmsFilename):
fromFile = open(localWmsFilename, "w+") # Creates local log file if it does not exist"
else:
fromFile = open(localWmsFilename, "r+") # Allows us to overwrite (to empty string)
text = fromFile.read()
if text != "":
# check for central log file for this month
try:
centralWMSFilename = str(Tools.centralLogFolder) + "\wms_" + time.strftime("%Y%m") + ".log"
if not path.exists(centralWMSFilename):
toFile = open(centralWMSFilename, "w+")
else:
toFile = open(centralWMSFilename, "a")
toFile.write(text)
toFile.close()
fromFile.seek(0, 0) # Return cursor to start of file
fromFile.truncate() # Remove contents
except:
pass
fromFile.close()
# Upload local CDDP counter data to central counter
# Check if local log file exists; if not then create it.
localFilename = path.normpath(Tools.localLogFolder) + Tools.localLogFilename
if not path.exists(localFilename):
fromFile = open(localFilename, "w+") # Creates file if it does not exist"
else:
fromFile = open(localFilename, "r+") # Allows us to overwrite (to empty string)
text = fromFile.read()
if text != "":
# check for central log file for this month
try:
centralFilename = str(Tools.centralLogFolder) + "\cddp_" + time.strftime("%Y%m") + ".log"
if not path.exists(centralFilename):
toFile = open(centralFilename, "w+")
else:
toFile = open(centralFilename, "a")
toFile.write(text)
toFile.close()
fromFile.seek(0, 0) # Return cursor to start of file
fromFile.truncate() # Remove contents
except:
pass
fromFile.close()
# Get V: drive address if it exists - see http://www.thescriptlibrary.com/Default.asp?Action=Display&Level=Category3&ScriptLanguage=Python&Category1=Storage&Category2=Disk%20Drives%20and%20Volumes&Title=List%20Mapped%20Network%20Drives
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
mappedDrives = objSWbemServices.ExecQuery("Select * from Win32_MappedLogicalDisk")
for drive in mappedDrives:
if drive.Name == "V:":
Tools.corporateDataDrive = drive.ProviderName
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(
QIcon(":/plugins/ymactools2/YMAC_icon.png"),
u"YMAC Tools for YMAC v 2.x", self.iface.mainWindow())
# connect the action to the run method
self.action.triggered.connect(self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu(u"&YMAC Tools 2", self.action)
self.iface.newProjectCreated.connect(self.newProject)
self.iface.projectRead.connect(self.loadProject)
QgsMapLayerRegistry.instance().layersAdded.connect(self.layersAdded)
Tools.loadWMSPresets()
Tools.loadWFSPresets()
DockableWindow.getDockable(True)
def unload(self):
# Remove the plugin menu item, icon and interface
self.iface.removePluginMenu(u"&YMAC Tools 2", self.action)
self.iface.removeToolBarIcon(self.action)
mw = self.iface.mainWindow()
dock = mw.findChild(QDockWidget, Tools.applicationName)
mw.removeDockWidget(dock)
def run(self):
DockableWindow.getDockable()
def newProject(self):
Tools.isNewProject = True
Tools.projectRead = False
sb = Tools.iface.mainWindow().statusBar()
sb.messageChanged.connect(self.showProjectTemplateDialog)
def showProjectTemplateDialog(self):
setting = Tools.getSetting("showDefaultMapDialog")
if setting != "false":
sb = Tools.iface.mainWindow().statusBar()
if sb.currentMessage() == "Project loaded":
sb.messageChanged.disconnect()
templateDialog = ShowProjectTemplateDialog()
#self.previousStatusMessage = "Project loaded"
#elif sb.currentMessage() == "":
# if self.previousStatusMessage == "Project loaded":
#dialogue = ShowProjectTemplateDialog()
#self.previousStatusMessage == ""
else:
pass
#self.previousStatusMessage == ""
def loadProject(self):
Tools.isNewProject = False
project = QgsProject.instance()
Tools.lastProject = project.fileName()
def layersAdded(self, layers):
layer = layers[-1]
if Tools.isNewProject:
crs = layer.crs()
settings = self.iface.mapCanvas().mapSettings()
settings.setDestinationCrs(crs)
settings.setCrsTransformEnabled(True)
Tools.isNewProject = False
source = layer.source().replace("\\", "/")
dataLocation = Tools.dataLocation1().replace("\\", "/")
if source.startswith(dataLocation): # i.e. if starts with "V:/GIS1-Corporate/Data/"
start = len(dataLocation)
cddpLayer = source[start:]
Tools.updateCounter(cddpLayer)
if layer.type() == QgsMapLayer.RasterLayer and layer.dataProvider().name() == "wms":
url = ""
layers = ""
src = layer.source()
srcParams = src.split("&")
for param in srcParams:
if param[:4] == "url=":
url = param[4:]
elif param[:7] == "layers=":
layers = param[7:]
wmsParams = (url, layers)
wmsInCDDP = "start value"
if Tools.cddpTechnique == "menu":
wmsInCDDP = ""
else:
if wmsParams in Tools.WMSinCDDP:
wmsInCDDP = " - CDDP layer"
else:
wmsInCDDP = " - Non-CDDP layer"
Tools.updateWMSCounter(layer.name(), url, wmsInCDDP)