/
svg_attributes.py
418 lines (336 loc) · 15.9 KB
/
svg_attributes.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# -*- coding: utf-8 -*-
"""
/***************************************************************************
SvgAttributes
A QGIS plugin
This plugin creates SVG with attributes
-------------------
begin : 2016-07-30
git sha : $Format:%H$
copyright : (C) 2016 by Petr Silhak
email : petrsilhak@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. *
* *
***************************************************************************/
"""
from PyQt4 import *
from qgis.PyQt import QtCore
from random import randint
import os.path
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon, QFileDialog, QListWidgetItem, QListView
# Initialize Qt resources from file resources.py
from PyQt4.QtGui import QMessageBox
from PyQt4.QtGui import QStandardItem
from PyQt4.QtGui import QStandardItemModel
from qgis.core import QgsFeatureRequest
from qgis.core import QgsMessageLog
from qgis.core import QGis
import GeometryError
import resources
# Import the code for the dialog
from svg_attributes_dialog import SvgAttributesDialog
import os.path
class SvgAttributes:
"""QGIS Plugin Implementation."""
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]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'SvgAttributes_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = SvgAttributesDialog()
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Svg With Attributes')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'SvgAttributes')
self.toolbar.setObjectName(u'SvgAttributes')
self.dlg.lineEdit.clear()
self.dlg.pushButton.clicked.connect(lambda: self.selectOutputFile())
# 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('SvgAttributes', 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):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
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:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/SvgAttributes/icon.png'
self.add_action(
icon_path,
text=self.tr(u'Create SVG'),
callback=self.run,
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&Svg With Attributes'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def selectOutputFile(self):
"""
Function for selecting path for output file
:return:
"""
filename = QFileDialog.getSaveFileName(self.dlg, "Select output file", "", '*.svg')
self.dlg.lineEdit.setText(filename)
def fillCheckboxes(self, layers, name):
"""
Function for filling listView with checkboxes
:param layers: layers from Qgis legend
:param name: name of current layer from comboBox
:return:
"""
layer = None
model = QStandardItemModel()
self.dlg.listView.reset()
for layer in layers:
if layer.name() == name:
fields = layer.pendingFields()
fieldNames = [field.name() for field in fields]
for fieldName in fieldNames:
item = QStandardItem(fieldName)
item.setCheckable(True)
item.setText(fieldName)
model.appendRow(item)
self.dlg.listView.setModel(model)
def createSVG(self, filename, attributes_dict):
width = self.iface.mapCanvas().size().width()
height = self.iface.mapCanvas().size().height()
with open(filename, 'w') as outputfile:
outputfile.write('''<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n''')
outputfile.write('<svg width="'+str(width)+'" height="'+str(height)+'" version="1.1" xmlns="http://www.w3.org/2000/svg">\n\n')
features_list = self.writeLayer(attributes_dict)
for svg_feature in features_list:
outputfile.write(svg_feature)
outputfile.write('</svg>')
QMessageBox.information(None, "SVG Writing:", 'Writing SVG was finished')
def writeLayer(self, attributes_dict):
current_layer = self.dlg.comboBox_layers.currentText()
layers = self.iface.legendInterface().layers()
for layer in layers:
if layer.name() == current_layer:
return self.geometryLayerType(layer, attributes_dict)
def writePointFeature(self, layer, attributes_dict):
points_svg = []
request = QgsFeatureRequest()
request.setFilterRect(self.iface.mapCanvas().extent())
for feature in layer.getFeatures(request):
points_svg.append(self.writePointToSVG(feature, attributes_dict))
return points_svg
def writeLineFeature(self, layer, attributes_dict):
lines_svg = []
request = QgsFeatureRequest()
request.setFilterRect(self.iface.mapCanvas().extent())
for feature in layer.getFeatures(request):
lines_svg.append(self.writeLineToSVG(feature, attributes_dict))
return lines_svg
def writePolygonFeature(self, layer, attributes_dict):
polygons_svg = []
request = QgsFeatureRequest()
request.setFilterRect(self.iface.mapCanvas().extent())
for feature in layer.getFeatures(request):
polygons_svg.append(self.writePolygonToSVG(feature, attributes_dict))
return polygons_svg
def writeLineToSVG(self, feature, attributes_dict):
line = feature.geometry().asPolyline()
attributes = feature.attributes()
atr_string = ''
line_string = 'M'
pixel_value = self.iface.mapCanvas().mapUnitsPerPixel()
extent = self.iface.mapCanvas().extent()
iterator_number = 0
#TODO Need find only points in mapcanvas - not working for big features outside of canvas
for key, value in attributes_dict.iteritems():
atr_string += ' '+value.encode('utf-8')+'="'+attributes[key].encode('utf-8')+'"'
for coordinates in line:
pixelX = (coordinates[0]-extent.xMinimum())/pixel_value
pixelY = (coordinates[1]-extent.yMaximum())/pixel_value
if iterator_number == 0:
line_string += str(pixelX)+' '+str(-pixelY)+' '
iterator_number += 1
else:
line_string += 'L'+str(pixelX) + ' ' + str(-pixelY) + ' '
return '<path fill="None" fill-opacity="0" stroke="#000" stroke-width="0.26" d="'+line_string+'" '+atr_string+' />\n'
def writePolygonToSVG(self, feature, attributes_dict):
polygon = feature.geometry().asPolygon()
attributes = feature.attributes()
atr_string = ''
polygon_string = 'M'
pixel_value = self.iface.mapCanvas().mapUnitsPerPixel()
extent = self.iface.mapCanvas().extent()
iterator_number = 0
#TODO Needed find why some polygons are problematic for export
for key, value in attributes_dict.iteritems():
atr_string += ' '+value.encode('utf-8')+'="'+attributes[key].encode('utf-8')+'"'
for polygon_list in polygon:
QgsMessageLog.logMessage('Zapisuji Polygon','Zkouska')
if polygon_list:
for coordinates in polygon_list:
pixelX = (coordinates[0]-extent.xMinimum())/pixel_value
pixelY = (coordinates[1]-extent.yMaximum())/pixel_value
if iterator_number == 0:
polygon_string += str(pixelX)+' '+str(-pixelY)+' '
iterator_number += 1
else:
polygon_string += 'L'+str(pixelX) + ' ' + str(-pixelY) + ' '
multiPolygon = feature.geometry().asMultiPolygon()
for multiPolygon_list in multiPolygon:
QgsMessageLog.logMessage('Zapisuji Multipolygon', 'Zkouska')
if multiPolygon_list:
for multiPolygon_parts in multiPolygon_list:
if multiPolygon_parts:
for coordinates in multiPolygon_parts:
pixelX = (coordinates[0] - extent.xMinimum()) / pixel_value
pixelY = (coordinates[1] - extent.yMaximum()) / pixel_value
if iterator_number == 0:
polygon_string += str(pixelX) + ' ' + str(-pixelY) + ' '
iterator_number += 1
else:
polygon_string += 'L' + str(pixelX) + ' ' + str(-pixelY) + ' '
QgsMessageLog.logMessage('Konec zapisu', 'Zkouska')
return '<path stroke-width="0.26" d="'+polygon_string+'" '+atr_string+' />\n'
def writePointToSVG(self, feature, attributes_dict):
point = feature.geometry().asPoint()
attributes = feature.attributes()
atr_string = ''
for key, value in attributes_dict.iteritems():
#QgsMessageLog.logMessage(str(key)+' '+str(value), 'Repaired')
atr_string += ' '+value.encode('utf-8')+'="'+attributes[key].encode('utf-8')+'"'
pixel_value = self.iface.mapCanvas().mapUnitsPerPixel()
extent = self.iface.mapCanvas().extent()
pixelX = (point.x()-extent.xMinimum())/pixel_value
pixelY = (point.y()-extent.yMaximum())/pixel_value
return '<circle cx="' + str(pixelX) + '" cy="' + str(-pixelY) + '" r="3" '+atr_string+'/>\n'
def createAttributesDictionary(self):
checked_attributes = {}
model = self.dlg.listView.model()
for row in range(model.rowCount()):
item = model.item(row)
if item.checkState() == QtCore.Qt.Checked:
checked_attributes[row] = item.text()
return checked_attributes
def geometryLayerType(self, layer, attributes_dict):
if layer.wkbType() == QGis.WKBPoint:
return self.writePointFeature(layer, attributes_dict)
elif layer.wkbType() == QGis.WKBLineString:
return self.writeLineFeature(layer, attributes_dict)
elif layer.wkbType() == QGis.WKBPolygon:
return self.writePolygonFeature(layer, attributes_dict)
elif layer.wkbType() == QGis.WKBMultiLineString:
return self.writeLineFeature(layer, attributes_dict)
elif layer.wkbType() == QGis.WKBMultiPolygon:
return self.writePolygonFeature(layer, attributes_dict)
else:
raise GeometryError('Unknown geometry type')
def run(self):
"""Run method that performs all the real work"""
layers = self.iface.legendInterface().layers()
layers_list = []
for layer in layers:
layers_list.append(layer.name())
self.dlg.comboBox_layers.clear()
self.dlg.comboBox_layers.addItems(layers_list)
#Initial setting list of checkboxes
initialLayerComboBox = str(self.dlg.comboBox_layers.currentText())
self.fillCheckboxes(layers, initialLayerComboBox)
#signal for filling listView
self.dlg.comboBox_layers.currentIndexChanged.connect(lambda: self.fillCheckboxes(layers, str(self.dlg.comboBox_layers.currentText())))
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
filename = self.dlg.lineEdit.text()
selectedLayerIndex = self.dlg.comboBox_layers.currentIndex()
#TODO Create reprojection of layer because it is not working with other CRS than EPSG: 4326
self.createSVG(filename, self.createAttributesDictionary())
pass