-
Notifications
You must be signed in to change notification settings - Fork 1
/
projectsldplugin.py
294 lines (260 loc) · 13 KB
/
projectsldplugin.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ProjectSldPlugin
SLD Export
Create one combined SLD file for all layers in the current project. Save
the file locally or publish it to B3P Kaartenbalie or Geoserver.
-------------------
begin : 2013-11-25
copyright : (C) 2013 by B3Partners
email : info@b3p.nl
***************************************************************************/
/***************************************************************************
* *
* 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 *
from qgis.gui import *
# Initialize Qt resources from file resources.py
import resources_rc
# Import the code for the dialog
from projectsldplugindialog import ProjectSldPluginDialog
import os
from xml.dom.minidom import parse, parseString
import config
import requests
from requests.auth import HTTPBasicAuth
#http://localhost/cgi-bin/mapserv?map=/usr/lib/cgi-bin/b3p.map&&SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&BBOX=4386.87996086938073859,304945.87847349571529776,286086.88017670798581094,621081.30985224642790854&CRS=EPSG:28992&WIDTH=388&HEIGHT=435&LAYERS=provincies,autowegen,plaatsen&STYLES=&FORMAT=image/png&TRANSPARENT=TRUE&sld=http://localhost/b3p.sld
class ProjectSldPlugin:
def __init__(self, iface):
# 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]
localePath = os.path.join(self.plugin_dir, 'i18n', 'projectsldplugin_{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
self.SETTINGS_SECTION = '/projectsldplugin/'
self.filename = self.getSettingsValue('lastfile')
# Create the dialog (after translation) and keep reference
self.dlg = ProjectSldPluginDialog()
self.dlg.ui.cbx_save_as_file.toggled.connect(self.saveAsFileToggled)
self.dlg.ui.cbx_post_to_server.toggled.connect(self.postToServerToggled)
self.dlg.ui.btn_filename.clicked.connect(self.filenameButtonClicked)
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(
QIcon(":/plugins/projectsldplugin/icon.png"),
u"SLD export", 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"SLD export", self.action)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu(u"SLD export", self.action)
self.iface.removeToolBarIcon(self.action)
# run method that performs all the real work
def run(self):
self.dlg.ui.le_workspace.setText(self.getSettingsValue('default-workspace'))
self.dlg.ui.le_post_url.setText(config.params['post-url'])
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result == 1:
self.createProjectSld()
def debug(self, string):
print "###### DEBUG"
print string
def postToServerToggled(self):
checked = self.dlg.ui.cbx_post_to_server.isChecked()
self.dlg.ui.lbl_workspace.setEnabled(checked)
self.dlg.ui.le_workspace.setEnabled(checked)
def saveAsFileToggled(self):
checked = self.dlg.ui.cbx_save_as_file.isChecked()
# disable or enable the inputs
self.dlg.ui.lbl_filename.setEnabled(checked)
self.dlg.ui.le_filename.setEnabled(checked)
self.dlg.ui.btn_filename.setEnabled(checked)
if checked:
self.dlg.ui.le_filename.setText(self.getSettingsValue('lastfile'))
else:
self.dlg.ui.le_filename.setText('')
def getSettingsValue(self, key):
if QSettings().contains(self.SETTINGS_SECTION + key):
key = self.SETTINGS_SECTION + key
if QGis.QGIS_VERSION_INT < 10900: # qgis <= 1.8
return unicode(QSettings().value(key).toString())
else:
return unicode(QSettings().value(key))
elif config.params.has_key(key):
return config.params[key]
else:
return u''
def setSettingsValue(self, key, value):
key = self.SETTINGS_SECTION + key
QSettings().setValue(key, value)
def filenameButtonClicked(self):
(self.filename, filter) = QFileDialog.getSaveFileNameAndFilter(
self.iface.mainWindow(),
"SLD bestand opslaan als...",
os.path.realpath(self.filename),
"sld files (*.sld)")
fn, fileextension = os.path.splitext(unicode(self.filename))
if len(fn) == 0: # user choose cancel
return
#if fileextension != '.sld':
# self.filename = fn + '.sld'
# save this filename in settings for later
self.dlg.ui.le_filename.setText(self.filename)
def createProjectSld(self):
if self.dlg.ui.cbx_save_as_file.isChecked() == False:
file = QTemporaryFile(QDir.tempPath()+os.sep+'qgis_XXXXXX.sld')
file.open()
self.filename = file.fileName()
else:
try:
file = open(self.filename, "w")
except:
self.iface.messageBar().pushMessage('Fout',
'Kan het bestand "'+
unicode(self.filename) + '" niet opslaan. Is dit een geldige bestandsnaam en schrijfbaar?', level=QgsMessageBar.CRITICAL, duration=5)
return
resultdom = None
# holding the QTemporaryFile handles here, just to be sure they are not
# removed while working on the layerlist
layerslds = []
# for every layer
for i in range (self.iface.mapCanvas().layerCount()-1, -1, -1):
layer = self.iface.mapCanvas().layer(i)
# for now: only vector layers (== type=0) style can be saved to sld
if layer.type() >0:
continue
f = QTemporaryFile(QDir.tempPath()+os.sep+'qgis_XXXXXX.sld')
f.open()
fname = f.fileName()
# create a sld in temporary dir
layer.saveSldStyle(fname)
if resultdom == None:
resultdom = parse(fname) # is now holding a DOM of the first sld
self.addFillElementToGraphicMark(resultdom)
else:
layer_dom = parse(fname)
namedlayer = layer_dom.getElementsByTagName('NamedLayer')[0]
self.addFillElementToGraphicMark(namedlayer)
resultdom.getElementsByTagName('StyledLayerDescriptor')[0].appendChild(namedlayer)
# append both fname AND temporary file object (to prevent it being removed)
layerslds.append((fname, f))
if resultdom == None:
self.iface.messageBar().pushMessage("Warning", "Geen sld bestand aangemaakt. Zijn er wel vector lagen aanwezig?", level=QgsMessageBar.WARNING, duration=510)
return
# multiplier for stroke width in sld
self.multiplyStrokeWidth(resultdom)
# multiplier for point size symbols
self.multipyPointSize(resultdom)
#print resultdom.toxml()
# toprettyxml looks better, but has an awfull lot of whitespace,
# see: http://ronrothman.com/public/leftbraned/xml-dom-minidom-toprettyxml-and-silly-whitespace/
#xml = resultdom.toprettyxml()
xml = resultdom.toxml()
# now replace all replacements from config
for key in config.replace:
xml = xml.replace(key, config.replace[key])
file.write(xml)
file.close()
# only remember if checked and succesfull
if self.dlg.ui.cbx_save_as_file.isChecked():
self.setSettingsValue('lastfile', self.filename)
self.iface.messageBar().pushMessage("Info", "Sld succesvol opgeslagen als: "+unicode(self.filename), level=QgsMessageBar.INFO, duration=2)
if self.dlg.ui.cbx_post_to_server.isChecked():
self.post_sld()
def post_sld(self):
url = config.params['post-url']
files = {'file': open(self.filename, 'rb')}
workspace = self.dlg.ui.le_workspace.text()
workspacekey = config.params['post-workspace-param']
self.setSettingsValue('default-workspace', workspace)
username = config.params['post-username']
password = config.params['post-password']
try:
r = requests.post(url,
timeout=10,
data={workspacekey:workspace},
files=files,
auth=HTTPBasicAuth(username, password))
if r.status_code == 200:
self.iface.messageBar().pushMessage("Info", "Sld succesvol verstuurd... ", level=QgsMessageBar.INFO, duration=2)
sldurl = r.text
QMessageBox.information(self.iface.mainWindow(), "Sld",
("Gebruik onderstaande url in Kaartenbalie:\n\n" + sldurl +
"\n"), QMessageBox.Ok, QMessageBox.Ok)
else:
self.iface.messageBar().pushMessage("Fout",
"Probleem bij het versturen van de sld: "+
unicode(r.status_code), level=QgsMessageBar.CRITICAL, duration=5)
except requests.exceptions.Timeout:
self.iface.messageBar().pushMessage("Fout",
"Timout bij posten naar: "+
unicode(url) + ". Is de url wel juist?", level=QgsMessageBar.CRITICAL, duration=5)
# <se:SvgParameter name="stroke-width">2</se:SvgParameter>
def multiplyStrokeWidth(self, dom):
strokeWidthMultiplier = float(config.params['stroke-width-multiplier'])
svgparams = dom.getElementsByTagName('se:SvgParameter')
if len(svgparams)>0:
for svgparam in svgparams:
if svgparam.getAttribute('name')=='stroke-width':
width = float(svgparam.childNodes[0].nodeValue)
svgparam.childNodes[0].nodeValue=width*strokeWidthMultiplier
# <se:PointSymbolizer>..<se:Size>2</se:Size>
def multipyPointSize(self, dom):
pointSizeMultiplier = float(config.params['point-size-multiplier'])
pointsyms = dom.getElementsByTagName('se:PointSymbolizer')
if len(pointsyms)>0:
for pointsym in pointsyms:
size = float(self.childNodeValue(pointsym, 'se:Size'))
sizeElement = pointsym.getElementsByTagName('se:Size')
sizeElement[0].childNodes[0].nodeValue=size*pointSizeMultiplier
def addFillElementToGraphicMark(self, dom):
marks = dom.getElementsByTagName('se:Mark')
if len(marks)>0:
for mark in marks:
mark_type = self.childNodeValue(mark, 'se:WellKnownName')
# only adding fill for following 'wellknowname' types:
# QGIS: only do: horline, line, cross, slash, backslash, x
if mark_type in ['horline', 'line', 'cross', 'slash', 'backslash', 'x']:
stroke = mark.getElementsByTagName('se:Stroke')
if len(stroke):
stroke_color = self.childNodeValue(stroke[0], 'se:SvgParameter')
fill = parseString(u'<se:Fill xmlns:se="http://www.opengis.net/se"><se:CssParameter name="fill">'+stroke_color+u'</se:CssParameter></se:Fill>')
fill_elm = fill.getElementsByTagName('se:Fill')[0]
mark.appendChild(fill_elm)
def childNodeValue(self, node, childName):
nodes = node.getElementsByTagName(childName)
if len(nodes)==1 and nodes[0].hasChildNodes():
return nodes[0].childNodes[0].nodeValue
if len(nodes)>1:
arr = u''
for child in nodes:
# extra check, we only want direct childs
if child.parentNode.nodeName==node.nodeName and child.hasChildNodes():
arr+=(child.childNodes[0].nodeValue)
arr+=','
return arr.rstrip(',')
return ''