-
Notifications
You must be signed in to change notification settings - Fork 0
/
midv_tolkn.py
executable file
·265 lines (226 loc) · 12.4 KB
/
midv_tolkn.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
This is the main part of the midv_tolkn plugin.
Mainly controlling user interaction and calling for other classes.
-------------------
begin : 2016-07-30
copyright : (C) 2016 by Josef Källgården
email : groundwatergis [at] gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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 datetime
# Import some general python modules
import os.path
import sys
import zipfile
import qgis.utils
# Import the PyQt and QGIS libraries
from qgis.PyQt.QtCore import QCoreApplication, QSettings, Qt, QFile
from qgis.PyQt.QtGui import QCursor, QIcon
from qgis.PyQt.QtWidgets import QAction, QApplication, QFileDialog, QMenu
try:
import zlib
compression = zipfile.ZIP_DEFLATED
except:
compression = zipfile.ZIP_STORED
#add midv_tolkn plugin directory to pythonpath (needed here to allow importing modules from subfolders)
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/tools'))
# Import midv_tolkn tools and modules
from .load_tolkn_layers import LoadLayers
from . import midv_tolkn_utils as utils
class midv_tolkn:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(QFile.decodeName(__file__))
self.db = None
def initGui(self):
# Create actions
icon = QIcon(os.path.join(self.plugin_dir, "icons", "midv_tolkn.png"))
self.actionloadthelayers = QAction(QIcon(os.path.join(self.plugin_dir, 'icons', 'load_layers_domains.png')), "Ladda tolkningslager t QGIS", self.iface.mainWindow())
self.actionloadthelayers.setWhatsThis("Laddar tolkningslager för gvmagasin m.m. till QGIS")
self.actionloadthelayers.triggered.connect(lambda x: self.load_the_layers())
self.actionNewDB = QAction(QIcon(os.path.join(self.plugin_dir, 'icons', 'create_new.png')), "Skapa en ny tolkningsdatabas", self.iface.mainWindow())
self.actionNewDB.triggered.connect(lambda x: self.new_db())
self.actionVacuumDB = QAction(QIcon(os.path.join(self.plugin_dir, 'icons', 'vacuum.png')), "Packa (vacuum) tolkn-db", self.iface.mainWindow())
self.actionVacuumDB.setWhatsThis("Perform database vacuuming")
self.actionVacuumDB.triggered.connect(lambda x: self.vacuum_db())
self.actionZipDB = QAction(QIcon(os.path.join(self.plugin_dir, 'icons', 'zip.png')), "Backup av tolknings-databas", self.iface.mainWindow())
self.actionZipDB.setWhatsThis("En komprimerad zip-fil kommer att skapas i samma dir som tolknings-databasen.")
self.actionZipDB.triggered.connect(lambda x: self.zip_db())
self.actionUpgradeDB = QAction(QIcon(os.path.join(self.plugin_dir, 'icons', 'create_new.png')), "Uppgradera tolknings-databas", self.iface.mainWindow())
self.actionUpgradeDB.setWhatsThis("Uppgradera en befintlig tolknings-databas till ny databas-struktur.")
self.actionUpgradeDB.triggered.connect(lambda x: self.upgrade_db())
#self.actionabout = QAction(QIcon(":/plugins/midv_tolkn/icons/about.png"), "Information", self.iface.mainWindow())
#self.actionabout.triggered.connect(lambda x: self.about)
# Add button
self.iface.addToolBarIcon(self.actionloadthelayers)
# Add plugins menu items
self.midv_menu = None # Midvatten-plugin-menyn
self.menu = None # sub-menyn "Tolkningar"
# Check if Midvatten-menyn existerar och get it
for child in self.iface.mainWindow().menuBar().children():
if isinstance(child,QMenu):
if child.title() == "Midvatten": # Put here your menu name
self.midv_menu = child
# If the Midvatten menu does not exist, create it!
self.owns_midv_menu = False #indicator that this plugin must not clean up the midvatten menu
if not self.midv_menu:
self.owns_midv_menu = True #indicator that this plugin must clean up the midvatten menu
self.midv_menu = QMenu( "Midvatten", self.iface.mainWindow().menuBar() )
menuBar = self.iface.mainWindow().menuBar()
menuBar.addMenu(self.midv_menu)
# check if there is a sub-menu Tolkningar
for childchild in self.midv_menu.children():
if isinstance(childchild,QMenu):
if childchild.title() == "&Tolkningar": # Put here your menu name
print('found sub-menu Tolkningar')
self.menu = childchild
# If the tolkning submenu does not exist, create it
if not self.menu:
print('will add Tolkningar submenu')
self.menu_separator1 = self.midv_menu.addSeparator()
self.menu = QMenu(QCoreApplication.translate("Midvatten", "&Tolkningar"))
self.midv_menu.addMenu(self.menu)
self.menu_separator2 = self.midv_menu.addSeparator()
self.menu.addAction(self.actionloadthelayers)
self.menu.addAction(self.actionNewDB)
self.menu.addAction(self.actionVacuumDB)
self.menu.addAction(self.actionZipDB)
self.menu.addAction(self.actionUpgradeDB)
#self.menu.addAction(self.actionabout)
def unload(self):
# remove tool bar button
self.iface.removeToolBarIcon(self.actionloadthelayers)
# Remove the plugin menu items and icons
try:
self.midv_menu.removeAction(self.menu.menuAction())
self.midv_menu.removeAction(self.menu_separator1)
self.midv_menu.removeAction(self.menu_separator2)
except:
pass
if self.owns_midv_menu: #indicator that this plugin must clean up the midvatten menu
menubar = self.midv_menu.parentWidget()
menubar.removeAction(self.midv_menu.menuAction())
self.midv_menu.deleteLater()
def about(self):
utils.pop_up_info(msg='This feature is not yet implemented',title='Hold on...')
def load_data_domains(self):
#utils.pop_up_info(msg='This feature is not yet implemented',title='Hold on...')
#return
QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
err_flag = utils.verify_msettings_loaded_and_layer_edit_mode(qgis.utils.iface, self.ms)#verify midv settings are loaded
if err_flag == 0:
conn_ok, dd_tables = utils.sql_load_fr_db("select name from sqlite_master where name like 'zz_%'")
if not conn_ok:
return
d_domain_tables = [str(dd_table[0]) for dd_table in dd_tables]
err_flag = utils.verify_msettings_loaded_and_layer_edit_mode(qgis.utils.iface, self.ms, d_domain_tables)#verify none of the tables are already loaded and in edit mode
if err_flag == 0:
LoadLayers(qgis.utils.iface, self.ms.settingsdict,'Midvatten_data_domains')
QApplication.restoreOverrideCursor()#now this long process is done and the cursor is back as normal
def load_the_layers(self):
LoadLayers(qgis.utils.iface,self.db)
def new_db(self, set_locale=False):
if not set_locale:
set_locale = utils.getcurrentlocale()
filenamepath = os.path.join(os.path.dirname(__file__),"metadata.txt" )
iniText = QSettings(filenamepath , QSettings.IniFormat)
verno = str(iniText.value('version'))
from .create_tolkn_db import newdb
newdbinstance = newdb(verno, set_locale=set_locale)
if not newdbinstance.dbpath=='':
self.db = newdbinstance.dbpath
def upgrade_db(self, set_locale=False):
from_db = None
#get full path to the original db to be updated
if self.db:
use_current_db = utils.Askuser("YesNo","""Do you want to upgrade %s?"""%self.db,'Current database?')
if use_current_db.result == 0:
from_db = None
elif use_current_db.result == 1:
from_db = self.db
elif use_current_db.result == '':
return
if not from_db:
from_db = QFileDialog.getOpenFileName(None, 'Ange tolknings-db som ska exporteras','',"Spatialite (*.sqlite)")[0]
if not from_db:
QApplication.restoreOverrideCursor()
return None
#get EPSG in the original db
EPSG = utils.sql_load_fr_db("""SELECT srid FROM geom_cols_ref_sys WHERE Lower(f_table_name) = Lower('gvmag') AND Lower(f_geometry_column) = Lower('geometry')""",from_db)
#preparations to create new db of new design
if not set_locale:
set_locale = utils.getcurrentlocale()
filenamepath = os.path.join(os.path.dirname(__file__),"metadata.txt" )
iniText = QSettings(filenamepath , QSettings.IniFormat)
verno = str(iniText.value('version'))
#now create database of the updated design
from .create_tolkn_db import newdb
newdbinstance = newdb(verno, user_select_CRS=False, EPSG_code = EPSG[1][0][0], set_locale=set_locale)
if not newdbinstance.dbpath:
QApplication.restoreOverrideCursor()
return None
#transfer data to the new database
foo = utils.UpgradeDatabase(from_db,newdbinstance.dbpath, EPSG)
#set new database as the current db and load these layers
if not newdbinstance.dbpath=='':
self.db = newdbinstance.dbpath
self.load_the_layers()
def vacuum_db(self):
force_another_db = False
if self.db:
use_current_db = utils.Askuser("YesNo","""Vill du packa %s?"""%self.db,'Which database?')
if use_current_db.result == 1:
dbpath = self.db
force_another_db = True
elif use_current_db.result == 0:
force_another_db = True
elif use_current_db.result == '':
return
if not self.db or force_another_db:
dbpath = QFileDialog.getOpenFileName(None, 'Ange db som ska packas','',"Spatialite (*.sqlite)")[0]
QApplication.setOverrideCursor(Qt.WaitCursor)
utils.sql_alter_db(dbpath,'vacuum')
QApplication.restoreOverrideCursor()
def zip_db(self):
force_another_db = False
dbpath=None
if self.db:
use_current_db = utils.Askuser("YesNo",'Vill du göra backup av %s?'%self.db,'Which database?')
if use_current_db.result == 1:
dbpath = self.db
force_another_db = False
elif use_current_db.result == 0:
force_another_db = True
elif use_current_db.result == '':
return
if not self.db or force_another_db:
dbpath = QFileDialog.getOpenFileName(None, 'Ange db som du vill skapa backup utav','',"Spatialite (*.sqlite)")[0]
if dbpath:
QApplication.setOverrideCursor(Qt.WaitCursor)
connection = utils.dbconnection(dbpath)
connection.connect2db()
connection.conn.cursor().execute("begin immediate")
file_path = os.path.realpath(dbpath)
dir_path = os.path.dirname(file_path)
current_dir = dir_path.split(os.sep)[-1]
bkupname = dbpath + datetime.datetime.now().strftime('%Y%m%dT%H%M') + '.zip'
zf = zipfile.ZipFile(bkupname, mode='w')
zf.write(dbpath,os.path.basename(dbpath), compress_type=compression) #compression will depend on if zlib is found or not
zf.close()
connection.conn.rollback()
connection.closedb()
self.iface.messageBar().pushMessage("Information","Database backup was written to " + bkupname, 1,duration=15)
QApplication.restoreOverrideCursor()