forked from linkedgeodesy/myfirstqgisplugin
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sparql_unicorn.py
333 lines (282 loc) · 14.1 KB
/
sparql_unicorn.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
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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
SPAQLunicorn
A QGIS plugin
This plugin adds a GeoJSON layer from a Wikidata SPARQL query.
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2019-10-28
git sha : $Format:%H$
copyright : (C) 2019 by SPARQL Unicorn
email : [email protected]
developer(s) : Florian Thiery, Timo Homburg
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.utils import iface
from qgis.core import Qgis
from qgis.core import QgsMessageLog, Qgis,QgsApplication
from qgis.PyQt.QtCore import QSettings, QCoreApplication, QTranslator
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtGui import QStandardItemModel
from qgis.PyQt.QtWidgets import QAction, QFileDialog
from qgis.core import QgsProject
from .resources_rc import *
import os.path
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "dependencies")))
from .util.ui.uiutils import UIUtils
from .util.layerutils import LayerUtils
from .util.export.layer.layerexporter import LayerExporter
from .util.conf.configutils import ConfigUtils
from .tasks.query.util.triplestorereposynctask import TripleStoreRepositorySyncTask
import json
from rdflib import *
# Import the code for the dialog
from .dialogs.util.uploadrdfdialog import UploadRDFDialog
from .dialogs.util.loginwindowdialog import LoginWindowDialog
from .dialogs.sparql_unicorn_dialog import SPARQLunicornDialog
geoconcepts = ""
## The main SPARQL unicorn dialog.
#
class SPARQLunicorn:
enrichedExport = False
exportNameSpace = None
exportIdCol = None
exportSetClass = None
# Triple store configuration map
triplestoreconf = None
enrichLayer = None
qtask = None
originalRowCount = 0
enrichLayerCounter = 0
addVocabConf = None
savedQueriesJSON = {}
exportColConfig = {}
valueconcept = {}
columnvars = {}
prefixes = []
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# 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]
QgsMessageLog.logMessage('Started task "{}"'.format(str(locale)), "SPARQL Unicorn", Qgis.Info)
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'unicorn_{}.qm'.format(locale))
QgsMessageLog.logMessage('Started task "{}"'.format(str(locale_path)), "SPARQL Unicorn", Qgis.Info)
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QgsMessageLog.logMessage('Started task "{}"'.format(str(self.translator)), "SPARQL Unicorn", Qgis.Info)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&SPARQL Unicorn Wikidata Plugin')
# Check if plugin was started the first time in current QGIS session
# Must be set in initGui() to survive plugin reloads
self.first_start = None
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('unicorn', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
# Adds plugin icon to Plugins toolbar
self.iface.addToolBarIcon(action)
if add_to_menu:
self.iface.addPluginToVectorMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# a = str('numpy' in sys.modules)
# iface.messageBar().pushMessage("load libs", a, level=Qgis.Success)
self.add_action(
UIUtils.sparqlunicornicon,
text=self.tr(u'Adds GeoJSON layer from a Wikidata'),
callback=self.run,
parent=self.iface.mainWindow())
# will be set False in run()
self.first_start = True
## Removes the plugin menu item and icon from QGIS GUI.
# @param self The object pointer.
def unload(self):
for action in self.actions:
self.iface.removePluginVectorMenu(
self.tr(u'&SPARQL Unicorn Wikidata Plugin'),
action)
self.iface.removeToolBarIcon(action)
def exportLayer2(self):
self.exportLayer(None, None, None, None, None, None, self.dlg.exportTripleStore_2.isChecked())
## Creates the export layer dialog for exporting layers as TTL.
# @param self The object pointer.
def exportLayer(self, urilist=None, classurilist=None, includelist=None, proptypelist=None, valuemappings=None,
valuequeries=None, exportToTripleStore=False):
layer = self.dlg.chooseLayerInterlink.currentLayer()
if exportToTripleStore:
ttlstring = LayerExporter.layerToTTLString(layer, "".join(self.prefixes[self.dlg.comboBox.currentIndex()]),
urilist, classurilist, includelist, proptypelist, valuemappings,
valuequeries)
uploaddialog = UploadRDFDialog(ttlstring, self.triplestoreconf, self.dlg.comboBox.currentIndex())
uploaddialog.setMinimumSize(450, 250)
uploaddialog.setWindowTitle("Upload interlinked dataset to triple store ")
uploaddialog.exec_()
else:
filename, _filter = QFileDialog.getSaveFileName(
self.dlg, "Select output file ", "", "Linked Data (*.ttl *.n3 *.nt *.graphml)", )
if filename == "":
return
if filename.endswith("graphml"):
ttlstring = LayerExporter.layerToGraphML(layer)
else:
ttlstring = LayerExporter.layerToTTLString(layer, "".join(self.prefixes[self.dlg.comboBox.currentIndex()]),
urilist, classurilist, includelist, proptypelist, valuemappings,
valuequeries)
with open(filename, 'w') as output_file:
output_file.write(ttlstring)
iface.messageBar().pushMessage("export layer successfully!", "OK", level=Qgis.Success)
if not filename.endswith("graphml"):
g = Graph()
g.parse(data=ttlstring, format="ttl")
splitted = filename.split(".")
exportNameSpace = ""
exportSetClass = ""
with open(filename, 'w') as output_file:
output_file.write(g.serialize(format=splitted[len(splitted) - 1]).decode("utf-8"))
iface.messageBar().pushMessage("export layer successfully!", "OK", level=Qgis.Success)
## Saves a personal copy of the triplestore configuration file to disk.
# @param self The object pointer.
def saveTripleStoreConfig(self):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
with open(os.path.join(__location__, 'conf/triplestoreconf_personal.json'), 'w') as myfile:
myfile.write(json.dumps(self.triplestoreconf, indent=2))
def syncTripleStoreConfig(self,combobox):
self.qlayerinstance = TripleStoreRepositorySyncTask(
"Syncing triplestore conf with repository",combobox, self.triplestoreconf)
QgsApplication.taskManager().addTask(self.qlayerinstance)
## Restores the triple store configuration file with the version delivered with the SPARQLUnicorn QGIS plugin.
# @param self The object pointer.
def resetTripleStoreConfig(self):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
with open(os.path.join(__location__, 'conf/triplestoreconf.json'), 'r') as myfile:
data = myfile.read()
self.triplestoreconf = json.loads(data)
with open(os.path.join(__location__, 'conf/triplestoreconf_personal.json'), 'w') as myfile:
myfile.write(json.dumps(self.triplestoreconf, indent=2))
def manageTripleStoreConfFiles(self):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
if not os.path.exists(os.path.join(__location__, "tmp")):
os.mkdir(os.path.join(__location__, "tmp"))
if not os.path.exists(os.path.join(__location__, "tmp/classtree")):
os.mkdir(os.path.join(__location__, "tmp/classtree"))
else:
dir = os.path.join(__location__, "tmp/classtree")
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
if not os.path.exists(os.path.join(__location__, "tmp/geoconcepts")):
os.mkdir(os.path.join(__location__, "tmp/geoconcepts"))
else:
dir = os.path.join(__location__, "tmp/geoconcepts")
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
if os.path.isfile(os.path.join(__location__, 'conf/triplestoreconf_personal.json')):
with open(os.path.join(__location__, 'conf/triplestoreconf_personal.json'), 'r') as myfile:
data = myfile.read()
if ConfigUtils.isOldConfigurationFile(json.loads(data)):
with open(os.path.join(__location__, 'conf/triplestoreconf.json'), 'r') as myfile:
data = myfile.read()
else:
with open(os.path.join(__location__, 'conf/triplestoreconf.json'), 'r') as myfile:
data = myfile.read()
# parse file
with open(os.path.join(__location__, 'owl/addvocabconf.json'), 'r') as myfile:
data2 = myfile.read()
with open(os.path.join(__location__, 'owl/languages.json'), 'r') as myfile:
datalangs = myfile.read()
with open(os.path.join(__location__, 'owl/vocabs.json'), 'r') as myfile:
data3 = myfile.read()
with open(os.path.join(__location__, 'owl/prefixes.json'), 'r') as myfile:
data4 = myfile.read()
if os.path.isfile(os.path.join(__location__, 'conf/savedqueries.json')):
with open(os.path.join(__location__, 'conf/savedqueries.json'), 'r') as myfile:
data5 = myfile.read()
self.savedQueriesJSON = json.loads(data5)
self.triplestoreconf = json.loads(data)
self.addVocabConf = json.loads(data2)
self.languagemap = json.loads(datalangs)
self.autocomplete = json.loads(data3)
self.prefixstore = json.loads(data4)
counter = 0
for store in self.triplestoreconf:
self.prefixes.append("")
for prefix in store["prefixes"]:
self.prefixes[counter] += "PREFIX " + prefix + ":<" + store["prefixes"][prefix] + ">\n"
counter += 1
self.addVocabConf = json.loads(data2)
self.saveTripleStoreConfig()
def run(self):
"""Run method that performs all the real work"""
# Create the dialog with elements (after translation) and keep reference
# Only create GUI ONCE in callback, so that it will only load when the plugin is started
if self.first_start == True:
self.first_start = False
self.manageTripleStoreConfFiles()
self.dlg = SPARQLunicornDialog(self.languagemap,self.triplestoreconf, self.prefixes, self.addVocabConf, self.autocomplete,
self.prefixstore, self.savedQueriesJSON, self)
self.dlg.comboBox.clear()
UIUtils.createLanguageSelectionCBox(self.dlg.queryResultLanguageCBox,self.languagemap)
UIUtils.createTripleStoreCBox(self.dlg.comboBox,self.triplestoreconf)
self.dlg.comboBox.setCurrentIndex(0)
self.syncTripleStoreConfig(self.dlg.comboBox)
self.dlg.oauthTestButton.hide()
self.dlg.oauthTestButton.clicked.connect(lambda: LoginWindowDialog(self).exec())
self.dlg.tabchanged(self.dlg.tabWidget.currentIndex())
self.dlg.endpointselectaction()
else:
self.dlg.show()