-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparqlLayer.py
216 lines (142 loc) · 6.55 KB
/
SparqlLayer.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
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import urllib2, urllib
import json
class SparqlLayer(object):
def __init__(self, main, name, url, displaySparql, updateSparql ):
self.main = main
self.layerPoints = QgsVectorLayer("MultiPoint", name+' (points)', "memory")
self.layerLines = QgsVectorLayer("MultiLineString", name+' (lines)', "memory")
self.layerPolygons = QgsVectorLayer("MultiPolygon", name+' (polygons)', "memory")
self.layerPoints.beforeCommitChanges.connect( lambda: self.beforeCommitChanges(self.layerPoints) )
self.layerLines.beforeCommitChanges.connect( lambda: self.beforeCommitChanges(self.layerLines) )
self.layerPolygons.beforeCommitChanges.connect( lambda: self.beforeCommitChanges(self.layerPolygons) )
QgsMapLayerRegistry.instance().addMapLayer(self.layerPolygons)
QgsMapLayerRegistry.instance().addMapLayer(self.layerLines)
QgsMapLayerRegistry.instance().addMapLayer(self.layerPoints)
self.layers = {self.layerPoints, self.layerLines, self.layerPolygons}
self.__name = name
self.__url = url
self.__displaySparql = displaySparql
self.__updateSparql = updateSparql
self.needsUpdate = True
@property
def name(self):
return self.__name
@name.setter
def name(self, val):
self.__name = val
self.layerPoints.setLayerName( val +' (points)')
self.layerLines.setLayerName( val +' (lines)')
self.layerPolygons.setLayerName( val +' (polygons)')
@property
def url(self):
return self.__url
@url.setter
def url(self, val):
self.__url = val
self.needsUpdate = True
@property
def displaySparql(self):
return self.__displaySparql
@displaySparql.setter
def displaySparql(self, val):
self.__displaySparql = val
self.needsUpdate = True
@property
def updateSparql(self):
return self.__updateSparql
@updateSparql.setter
def updateSparql(self, val):
self.__updateSparql = val
def unload(self):
self.main.layers.remove( self )
try:
QgsMapLayerRegistry.instance().removeMapLayer(self.layerPoints.id())
except Exception, e:
pass
try:
QgsMapLayerRegistry.instance().removeMapLayer(self.layerLines.id())
except Exception, e:
pass
try:
QgsMapLayerRegistry.instance().removeMapLayer(self.layerPolygons.id())
except Exception, e:
pass
def update(self):
QgsMessageLog.logMessage( self.name+': making the http request...','SparqlLayer' )
# Make the http request
try:
data = urllib.urlencode({'query' : self.displaySparql})
request = urllib2.Request( url=self.url, data=data, headers={'Accept': 'application/json'} )
content = urllib2.urlopen( request ).read()
except Exception, e:
QgsMessageLog.logMessage( 'Error when executing the query !','SparqlLayer' )
QgsMessageLog.logMessage( str(e), 'SparqlLayer' )
return
QgsMessageLog.logMessage( self.name+': parsing the json...','SparqlLayer' )
# Load the JSON
try:
jsonData = json.loads(content)
except Exception, e:
QgsMessageLog.logMessage( 'Error when parsing the query !','SparqlLayer' )
QgsMessageLog.logMessage( str(e), 'SparqlLayer' )
return
# Deleting the features
for layer in self.layers:
layer.dataProvider().deleteFeatures( layer.allFeatureIds() )
QgsMessageLog.logMessage( self.name+': getting the fields...','SparqlLayer' )
# Create the attributes
fields = QgsFields ()
for layer in self.layers:
#provider.deleteAttributes()
for attr in jsonData['head']['vars']:
fields.append( QgsField(attr, QVariant.String) )
layer.dataProvider().addAttributes( fields )
layer.updateFields()
QgsMessageLog.logMessage( self.name+': creating the features...','SparqlLayer' )
# Create the features
for jsonFeature in jsonData['results']['bindings']:
geometry = QgsGeometry.fromWkt( jsonFeature['geom']['value'] )
fet = QgsFeature()
fet.setGeometry( geometry )
fet.setFields( fields )
for attr in jsonFeature:
fet.setAttribute( attr, jsonFeature[attr]['value'] )
if geometry.type() == QGis.Point:
self.layerPoints.dataProvider().addFeatures([fet])
elif geometry.type() == QGis.Line:
self.layerLines.dataProvider().addFeatures([fet])
elif geometry.type() == QGis.Polygon:
self.layerPolygons.dataProvider().addFeatures([fet])
QgsMessageLog.logMessage( self.name+': updating the extents...','SparqlLayer' )
# Update the extents
for layer in self.layers:
layer.updateExtents()
QgsMessageLog.logMessage( self.name+': finished !','SparqlLayer' )
self.needsUpdate = False
def beforeCommitChanges(self, layer):
changedGeometries = layer.editBuffer().changedGeometries()
request = QgsFeatureRequest()
request.setFilterFids( changedGeometries.keys() )
oldFeatures = layer.getFeatures( request )
# DELETING THE OLD TRIPLES
QgsMessageLog.logMessage( self.name+': making the http request...','SparqlLayer' )
for feature in oldFeatures:
sparql = self.updateSparql
sparql = sparql.replace( '!subject', '<'+feature.attribute('subject')+'>' )
sparql = sparql.replace( '!oldgeom', '"'+feature.attribute('geom')+'"^^virtrdf:Geometry' )
sparql = sparql.replace( '!newgeom', '"'+feature.geometry().exportToWkt().upper()+'"^^virtrdf:Geometry' )
QgsMessageLog.logMessage( 'Executing :' +sparql,'SparqlLayer' )
# Make the http request
try:
data = urllib.urlencode({'query' : sparql})
request = urllib2.Request( url=self.url, data=data, headers={'Accept': 'application/json'} )
content = urllib2.urlopen( request ).read()
except Exception, e:
QgsMessageLog.logMessage( 'Error when executing the query !','SparqlLayer' )
QgsMessageLog.logMessage( str(e), 'SparqlLayer' )
return
return