-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoiExportDialog.py
380 lines (351 loc) · 19 KB
/
poiExportDialog.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
POIExport
A QIGS plugin for exporting GPS Points of Interest
-------------------
begin : 2016-08-29
copyright : (C) 2016 by
C. Hamilton
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 os
from qgis.PyQt import uic
from qgis.PyQt.QtCore import QSettings, QUrl
from qgis.PyQt.QtWidgets import QDialog, QFileDialog, QDialogButtonBox
from qgis.core import Qgis, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsFeatureRequest, QgsVectorLayer, QgsMapLayerProxyModel, QgsProject
from xml.sax.saxutils import escape, unescape
from .colorhash import ColorHash
import webbrowser
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'poiexportdialog.ui'))
class POIExportDialog(QDialog, FORM_CLASS):
def __init__(self, iface):
"""Initialize the QGIS POI (Points of Interest) dialog window."""
super(POIExportDialog, self).__init__(iface.mainWindow())
self.setupUi(self)
self.iface = iface
self.canvas = iface.mapCanvas()
self.buttonBox.button(QDialogButtonBox.Ok).setText("Export")
self.outputFormatComboBox.addItems(['GPX', 'Garmin CSV'])
self.fileButton.clicked.connect(self.getDirPath)
self.vectorComboBox.layerChanged.connect(self.initLayerFields)
self.vectorComboBox.setFilters(QgsMapLayerProxyModel.PointLayer)
self.categoryComboBox.activated.connect(self.setEnabled)
self.visualComboBox.activated.connect(self.setEnabled)
self.poiNameComboBox.activated.connect(self.setEnabled)
self.epsg4326 = QgsCoordinateReferenceSystem("EPSG:4326")
self.buttonBox.button(QDialogButtonBox.Help).clicked.connect(self.help)
def help(self):
url = QUrl.fromLocalFile(os.path.dirname(__file__) + "/index.html").toString()
webbrowser.open(url, new=2)
def accept(self):
"""Called when the OK button has been pressed."""
if self.vectorComboBox.count() == 0:
self.iface.messageBar().pushMessage("", "No point vector layers are available", level=Qgis.Warning, duration=3)
return
# Read and get the state of all of the POI widgets
base = self.fileLineEdit.text()
categoryCol = self.categoryComboBox.currentIndex()
poiNameCol = self.poiNameComboBox.currentIndex()
visualCol = self.visualComboBox.currentIndex()
descriptionCol = self.descriptionComboBox.currentIndex()
commentCol = self.commentComboBox.currentIndex()
linkCol = self.webLinkComboBox.currentIndex()
symCol = self.symbolComboBox.currentIndex()
defaultCategory = self.defaultCategoryLineEdit.text()
defaultPOI = self.defaultPOILineEdit.text()
outputFormat = self.outputFormatComboBox.currentIndex()
# Set up a coordinate transform to make sure the output is always epsg:4326
layerCRS = self.selectedLayer.crs()
self.transform4326 = QgsCoordinateTransform(layerCRS, self.epsg4326, QgsProject.instance())
if outputFormat == 0: # GPX output formt
if categoryCol == 0:
# Process the input data to one output category as GPX format
self.processSingleCatGPX(self.selectedLayer, base, defaultCategory, poiNameCol, defaultPOI, descriptionCol, commentCol, linkCol, symCol, visualCol)
else:
# The user has selected a column to be used as POI categories. This will create
# multiple files with the names coming from this category. Output is GPX
categoryName = str(self.categoryComboBox.currentText())
self.processCatGPX(self.selectedLayer, base, categoryCol, categoryName, poiNameCol, defaultPOI, descriptionCol, commentCol, linkCol, symCol, visualCol)
else: # CSV output formt
if categoryCol == 0:
# Process the input data to one output category as CSV format
self.processSingleCat(self.selectedLayer, base, defaultCategory, poiNameCol, defaultPOI, descriptionCol, commentCol, linkCol, symCol, visualCol)
else:
# The user has selected a column to be used as POI categories. This will create
# multiple files with the names coming from this category. Output is CSV
categoryName = str(self.categoryComboBox.currentText())
self.processCat(self.selectedLayer, base, categoryCol, categoryName, poiNameCol, defaultPOI, descriptionCol, commentCol, linkCol, symCol, visualCol)
QDialog.accept(self)
def processSingleCat(self, layer, base, cat, poiNameCol, defaultPOI, descCol, cmtCol, linkCol, symCol, visualCol):
""" Output the POIs into a CSV file with only one category. Note that it
quotes the text."""
filename = "{}.csv".format(cat)
path = os.path.join(base, filename)
defaultPOI = str(defaultPOI).replace('"', '""')
fp = open(path, "w", encoding="utf-8")
for f in layer.getFeatures( ):
point = self.transform4326.transform(f.geometry().asPoint())
if poiNameCol == 0:
# Format: longitude, latitude, default name
line = '{},{},"{}"'.format(point.x(), point.y(),defaultPOI)
else:
# Format: longitude, latitude, name from the specified column
line = '{},{},"{}"'.format(point.x(), point.y(),str(f[poiNameCol-1]).replace('"','""'))
fp.write(line)
if cmtCol > 0:
# Add: , comment to the line
if f[cmtCol-1]:
line = ',"{}"'.format(str(f[cmtCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
if descCol > 0:
# Add: , description to the line
if f[descCol-1]:
line = ',"{}"'.format(str(f[descCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
if linkCol > 0:
link = str(f[linkCol-1]).strip()
if link and link != 'NULL':
line = ',"{}"'.format(link)
else:
line = ',""'
fp.write(line)
if symCol > 0:
sym = str(f[symCol-1]).strip()
if sym and sym != 'NULL':
line = ',"{}"'.format(sym)
else:
line = ',""'
fp.write(sym)
if visualCol > 0:
# Add: , comment to the line
if f[visualCol-1]:
line = ',"{}"'.format(str(f[visualCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
fp.write('\n')
fp.close()
def processCat(self, layer, base, categoryCol, categoryName, poiNameCol, defaultPOI, descCol, cmtCol, linkCol, symCol, visualCol):
""" Output the POIs into multiple CSV files based on the unique categories specified by
categoryCol."""
categories = layer.uniqueValues(categoryCol-1)
defaultPOI = str(defaultPOI).replace('"', '""')
for cat in categories:
if cat is None or str(cat) is 'NULL':
continue
cat = str(cat)
filename = "{}.csv".format(cat)
path = os.path.join(base, filename)
fp = open(path, "w", encoding="utf-8")
filter = '"{}" = \'{}\''.format(categoryName, cat)
request = QgsFeatureRequest().setFilterExpression( filter )
for f in layer.getFeatures( request ):
point = self.transform4326.transform(f.geometry().asPoint())
if poiNameCol == 0:
# Format: longitude, latitude, default name
line = '{},{},"{}"'.format(point.x(), point.y(),defaultPOI)
else:
line = '{},{},"{}"'.format(point.x(), point.y(),str(f[poiNameCol-1]).replace('"','""'))
fp.write(line)
if cmtCol > 0:
if f[cmtCol-1]:
line = ',"{}"'.format(str(f[cmtCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
if descCol > 0:
if f[descCol-1]:
line = ',"{}"'.format(str(f[descCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
if linkCol > 0:
link = str(f[linkCol-1]).strip()
if link and link != 'NULL':
line = ',"{}"'.format(link)
else:
line = ',""'
fp.write(line)
if symCol > 0:
sym = str(f[symCol-1]).strip()
if sym and sym != 'NULL':
line = ',"{}"'.format(sym)
else:
line = ',""'
fp.write(sym)
if visualCol > 0:
if f[visualCol-1]:
line = ',"{}"'.format(str(f[visualCol-1]).replace('"','""'))
else:
line = ',""'
fp.write(line)
fp.write('\n')
fp.close()
def processSingleCatGPX(self, layer, base, cat, poiNameCol, defaultPOI, descCol, cmtCol, linkCol, symCol, visualCol):
""" Output the POIs into a GPX file with only one category. Note that it
quotes the text."""
filename = "{}.gpx".format(cat)
path = os.path.join(base, filename)
# Make sure the defaultPOI string is XML friendly
defaultPOI = escape( unescape( str(defaultPOI), {"'": "'", """: '"'}), {"'":"'",'"':"""})
fp = open(path, "w", encoding="utf-8")
fp.write('<?xml version="1.0" encoding="utf-8"?><gpx version="1.1" creator="QGIS POI Export" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')
for f in layer.getFeatures( ):
point = self.transform4326.transform(f.geometry().asPoint())
line = '<wpt lat="{}" lon="{}">\n'.format(point.y(), point.x())
fp.write(line)
if poiNameCol == 0:
line = '<name>{}</name>\n'.format(defaultPOI)
else:
# Make sure the name string is XML friendly
name = escape( unescape( str(f[poiNameCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<name>{}</name>\n'.format(name)
fp.write(line)
if cmtCol > 0:
if f[cmtCol-1]:
# Make sure the comment string is XML friendly
cmt = escape( unescape( str(f[cmtCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<cmt>{}</cmt>\n'.format(cmt)
fp.write(line)
if descCol > 0:
if f[descCol-1]:
# Make sure the description string is XML friendly
desc = escape( unescape( str(f[descCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<desc>{}</desc>\n'.format(desc)
fp.write(line)
if linkCol > 0:
link = '{}'.format(f[linkCol-1]).strip()
if link and link != 'NULL':
line = '<link href="{}" />\n'.format(link)
fp.write(line)
if symCol > 0:
sym = '{}'.format(f[symCol-1]).strip()
if sym and sym != 'NULL':
line = '<sym>{}</sym>\n'.format(sym)
fp.write(line)
if visualCol > 0:
if f[visualCol-1]:
# Make sure the comment string is XML friendly
color = escape( unescape( str(f[visualCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
colorAgence = ColorHash(color)
line = '<extensions><color>{}</color></extensions>\n'.format(colorAgence.hex)
fp.write(line)
fp.write('</wpt>\n')
fp.write('</gpx>\n')
fp.close()
def processCatGPX(self, layer, base, categoryCol, categoryName, poiNameCol, defaultPOI, descCol, cmtCol, linkCol, symCol, visualCol):
""" Output the POIs into multiple GPX files based on the unique categories specified by
categoryCol."""
categories = layer.uniqueValues(categoryCol-1)
defaultPOI = escape( unescape( str(defaultPOI), {"'": "'", """: '"'}), {"'":"'",'"':"""})
for cat in categories:
if cat is None or str(cat) is 'NULL':
continue
cat = str(cat)
filename = "{}.gpx".format(cat)
path = os.path.join(base, filename)
fp = open(path, "w", encoding="utf-8")
fp.write('<?xml version="1.0" encoding="utf-8"?><gpx version="1.1" creator="QGIS POI Export" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n')
filter = '"{}" = \'{}\''.format(categoryName, cat)
request = QgsFeatureRequest().setFilterExpression( filter )
for f in layer.getFeatures( request ):
point = self.transform4326.transform(f.geometry().asPoint())
line = '<wpt lat="{}" lon="{}">\n'.format(point.y(), point.x())
fp.write(line)
if poiNameCol == 0:
line = '<name>{}</name>\n'.format(defaultPOI)
else:
name = escape( unescape( str(f[poiNameCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<name>{}</name>\n'.format(name)
fp.write(line)
if cmtCol > 0:
if f[cmtCol-1]:
cmt = escape( unescape( str(f[cmtCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<cmt>{}</cmt>\n'.format(cmt)
fp.write(line)
if descCol > 0:
if f[descCol-1]:
desc = escape( unescape( str(f[descCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
line = '<desc>{}</desc>\n'.format(desc)
fp.write(line)
if linkCol > 0:
link = str(f[linkCol-1]).strip()
if link and link != 'NULL':
line = '<link href="{}" />\n'.format(link)
fp.write(line)
if symCol > 0:
sym = str(f[symCol-1]).strip()
if sym and sym != 'NULL':
line = '<sym>{}</sym>\n'.format(sym)
fp.write(line)
if visualCol > 0:
if f[visualCol-1]:
color = escape( unescape( str(f[visualCol-1]), {"'": "'", """: '"'}), {"'":"'",'"':"""})
colorAgence = ColorHash(color)
line = '<extensions><color>{}</color></extensions>\n'.format(colorAgence.hex)
fp.write(line)
fp.write('</wpt>\n')
fp.write('</gpx>\n')
fp.close()
def getDirPath(self):
settings = QSettings()
path = settings.value("LastPath/POIExportPath", None)
folder = QFileDialog.getExistingDirectory(self, "Select POI folder", path)
if not folder:
return
settings.setValue("LastPath/POIExportPath", folder)
self.fileLineEdit.setText(folder)
def showEvent(self, event):
""" Initialize the Dialog widgets when it is first displyaed """
super(POIExportDialog, self).showEvent(event)
self.initLayerFields()
def initLayerFields(self):
""" Populate the QComboBox fields box with a list of all the fields in the selected
points vector layer"""
self.categoryComboBox.clear()
self.poiNameComboBox.clear()
self.visualComboBox.clear()
self.descriptionComboBox.clear()
self.commentComboBox.clear()
self.webLinkComboBox.clear()
self.symbolComboBox.clear()
if self.vectorComboBox.count() == 0:
return
self.selectedLayer = self.vectorComboBox.currentLayer()
self.categoryComboBox.addItem('[Use default category]', -1)
self.poiNameComboBox.addItem('[Use default POI name]', -1)
self.visualComboBox.addItem('[Select an optional POI color category field]', -1)
self.commentComboBox.addItem('[Select an optional POI comment, address]', -1)
self.descriptionComboBox.addItem('[Select an optional POI description]', -1)
self.webLinkComboBox.addItem('[Select an optional URL field]', -1)
self.symbolComboBox.addItem('[Select an optional symbol name]', -1)
for idx, field in enumerate(self.selectedLayer.fields()):
self.categoryComboBox.addItem(field.name(), idx)
self.poiNameComboBox.addItem(field.name(), idx)
self.visualComboBox.addItem(field.name(), idx)
self.descriptionComboBox.addItem(field.name(), idx)
self.commentComboBox.addItem(field.name(), idx)
self.webLinkComboBox.addItem(field.name(), idx)
self.symbolComboBox.addItem(field.name(), idx)
self.setEnabled()
def setEnabled(self):
""" Depending on the settings some widgets are enabled and some are disabled. """
categoryCol = self.categoryComboBox.currentIndex()
poiNameCol = self.poiNameComboBox.currentIndex()
self.defaultCategoryLineEdit.setEnabled(categoryCol == 0)
self.defaultPOILineEdit.setEnabled(poiNameCol == 0)