-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osm2pgrouting4qgis.py
822 lines (664 loc) · 35.6 KB
/
osm2pgrouting4qgis.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
# -*- coding: utf-8 -*-
"""
/***************************************************************************
osm2pgrouting4qgis
A QGIS plugin
Automatically builds a pgRouting-compatible layer in a PostGIS database
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2018-11-03
git sha : $Format:%H$
copyright : (C) 2018 by Isaac Boates
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. *
* *
***************************************************************************/
"""
from PyQt5.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt5.QtGui import QIcon, QIntValidator, QDoubleValidator
from PyQt5.QtWidgets import QAction, QListWidgetItem, QFileDialog, QDialog, QPushButton, QLabel
from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsPointXY, QgsMapSettings, QgsProject, \
QgsDataSourceUri, QgsApplication, QgsMapLayerStore
# Initialize Qt resources from file resources.py
# from .resources import *
# Import the code for the dialog
from .osm2pgrouting4qgis_dialog import osm2pgrouting4qgisDialog
import os.path
from psycopg2 import connect as dbconnect, sql
import requests
import subprocess
import sys
class osm2pgrouting4qgis:
"""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',
'osm2pgrouting4qgis_{}.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 = osm2pgrouting4qgisDialog()
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&osm2pgrouting4qgis')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'osm2pgrouting4qgis')
self.toolbar.setObjectName(u'osm2pgrouting4qgis')
# 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('osm2pgrouting4qgis', 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.addPluginToDatabaseMenu(
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/osm2pgrouting4qgis/icon.png'
self.add_action(
icon_path,
text=self.tr(u'osm2pgrouting4qgis'),
callback=self.run,
parent=self.iface.mainWindow())
# Toggle between local file, layer extent and other extent
self.file_source_toggle = "file"
self.dlg.local_file_radioButton.clicked.connect(self.select_local_osm)
self.dlg.layers_radioButton.clicked.connect(self.select_layer_extent)
self.dlg.other_extent_radioButton.clicked.connect(self.select_other_extent)
# Toggle between existing db and new db
self.dlg.existing_db_radioButton.clicked.connect(self.select_existing_db)
self.dlg.new_db_radioButton.clicked.connect(self.select_new_db)
# Toggle schema
self._schema_toggle = False
self.dlg.schema_checkBox.setChecked(False)
self.dlg.schema_checkBox.clicked.connect(self.toggle_schema)
# Toggle prefix
self._prefix_toggle = False
self.dlg.prefix_checkBox.setChecked(False)
self.dlg.prefix_checkBox.clicked.connect(self.toggle_prefix)
# Toggle suffix
self._suffix_toggle = False
self.dlg.suffix_checkBox.setChecked(False)
self.dlg.suffix_checkBox.clicked.connect(self.toggle_suffix)
# Toggle add attributes / add nodes
self._attributes_and_tags_toggle = False
self.dlg.nodes_checkBox.setChecked(False)
self.dlg.nodes_checkBox.clicked.connect(self.toggle_attributes_and_tags)
# Toggle alternative osm2pgr executable
self._alt_osm2pgr_exec_toggle = False
self.dlg.alt_osm2pgr_exec_checkBox.setChecked(False)
self.dlg.alt_osm2pgr_exec_checkBox.clicked.connect(self.toggle_alt_osm2pgr_exec)
# Only integers allowed for extents and chunk size
self.onlyInt = QIntValidator()
self.onlyDouble = QDoubleValidator()
self.dlg.bounding_box_top_lineEdit.setValidator(self.onlyDouble)
self.dlg.bounding_box_bottom_lineEdit.setValidator(self.onlyDouble)
self.dlg.bounding_box_left_lineEdit.setValidator(self.onlyDouble)
self.dlg.bounding_box_right_lineEdit.setValidator(self.onlyDouble)
self.dlg.chunk_size_lineEdit.setValidator(self.onlyInt)
# Set up file chooser
self.dlg.local_file_pushButton.clicked.connect(self.open_file_chooser)
# Make "Current Extent" button generate the current extent in their respective lineEdits
self.dlg.extent_pushButton.clicked.connect(self.use_current_extent)
# Make REST endpoint test button test the endpoint
self.dlg.rest_endpoint_test_pushButton.clicked.connect(self.test_rest_endpoint)
# Set up initial GUI state
self.set_initial_state()
# TODO: move this to when the plugin executes
# cd to the plugin home folder
os.chdir(os.path.join(QgsApplication.qgisSettingsDirPath(), r"python/plugins/osm2pgrouting4qgis"))
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginDatabaseMenu(
self.tr(u'&osm2pgrouting4qgis'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def select_local_osm(self):
self.dlg.local_file_lineEdit.setDisabled(False)
self.dlg.local_file_pushButton.setDisabled(False)
self.dlg.layers_comboBox.setDisabled(True)
self.dlg.extent_pushButton.setDisabled(True)
self.dlg.bounding_box_top_lineEdit.setDisabled(True)
self.dlg.bounding_box_left_lineEdit.setDisabled(True)
self.dlg.bounding_box_right_lineEdit.setDisabled(True)
self.dlg.bounding_box_bottom_lineEdit.setDisabled(True)
self.dlg.osm_download_label.setDisabled(True)
self.dlg.rest_endpoint_lineEdit.setDisabled(True)
self.dlg.rest_endpoint_test_pushButton.setDisabled(True)
return None
def select_layer_extent(self):
self.dlg.local_file_pushButton.setDisabled(True)
self.dlg.local_file_lineEdit.setDisabled(True)
self.dlg.layers_comboBox.setDisabled(False)
self.dlg.extent_pushButton.setDisabled(True)
self.dlg.bounding_box_top_lineEdit.setDisabled(True)
self.dlg.bounding_box_left_lineEdit.setDisabled(True)
self.dlg.bounding_box_right_lineEdit.setDisabled(True)
self.dlg.bounding_box_bottom_lineEdit.setDisabled(True)
self.dlg.osm_download_label.setDisabled(True)
self.dlg.rest_endpoint_lineEdit.setDisabled(False)
self.dlg.rest_endpoint_test_pushButton.setDisabled(False)
return None
def select_other_extent(self):
self.dlg.local_file_pushButton.setDisabled(True)
self.dlg.local_file_lineEdit.setDisabled(True)
self.dlg.layers_comboBox.setDisabled(True)
self.dlg.extent_pushButton.setDisabled(False)
self.dlg.bounding_box_top_lineEdit.setDisabled(False)
self.dlg.bounding_box_left_lineEdit.setDisabled(False)
self.dlg.bounding_box_right_lineEdit.setDisabled(False)
self.dlg.bounding_box_bottom_lineEdit.setDisabled(False)
self.dlg.osm_download_label.setDisabled(False)
self.dlg.rest_endpoint_lineEdit.setDisabled(False)
self.dlg.rest_endpoint_test_pushButton.setDisabled(False)
return None
def select_existing_db(self):
self.dlg.overwrite_checkBox.setDisabled(False)
self.dlg.db_listWidget.setDisabled(False)
self.dlg.new_db_name_label.setDisabled(True)
self.dlg.new_db_name_lineEdit.setDisabled(True)
self.dlg.new_db_service_label.setDisabled(True)
self.dlg.new_db_service_lineEdit.setDisabled(True)
self.dlg.new_db_host_label.setDisabled(True)
self.dlg.new_db_host_lineEdit.setDisabled(True)
self.dlg.new_db_port_label.setDisabled(True)
self.dlg.new_db_port_lineEdit.setDisabled(True)
self.dlg.new_db_database_label.setDisabled(True)
self.dlg.new_db_database_lineEdit.setDisabled(True)
self.dlg.new_db_username_label.setDisabled(True)
self.dlg.new_db_username_lineEdit.setDisabled(True)
self.dlg.new_db_password_label.setDisabled(True)
self.dlg.new_db_password_lineEdit.setDisabled(True)
self.dlg.new_db_save_username_checkBox.setDisabled(True)
self.dlg.new_db_save_password_checkBox.setDisabled(True)
return None
def select_new_db(self):
self.dlg.overwrite_checkBox.setDisabled(True)
self.dlg.db_listWidget.setDisabled(True)
self.dlg.new_db_name_label.setDisabled(False)
self.dlg.new_db_name_lineEdit.setDisabled(False)
self.dlg.new_db_service_label.setDisabled(False)
self.dlg.new_db_service_lineEdit.setDisabled(False)
self.dlg.new_db_host_label.setDisabled(False)
self.dlg.new_db_host_lineEdit.setDisabled(False)
self.dlg.new_db_port_label.setDisabled(False)
self.dlg.new_db_port_lineEdit.setDisabled(False)
self.dlg.new_db_database_label.setDisabled(False)
self.dlg.new_db_database_lineEdit.setDisabled(False)
self.dlg.new_db_username_label.setDisabled(False)
self.dlg.new_db_username_lineEdit.setDisabled(False)
self.dlg.new_db_password_label.setDisabled(False)
self.dlg.new_db_password_lineEdit.setDisabled(False)
self.dlg.new_db_save_username_checkBox.setDisabled(False)
self.dlg.new_db_save_password_checkBox.setDisabled(False)
return None
def toggle_schema(self):
if self._schema_toggle:
self.dlg.schema_lineEdit.setDisabled(True)
else:
self.dlg.schema_lineEdit.setDisabled(False)
self._schema_toggle = not self._schema_toggle
return None
def toggle_prefix(self):
if self._prefix_toggle:
self.dlg.prefix_lineEdit.setDisabled(True)
else:
self.dlg.prefix_lineEdit.setDisabled(False)
self._prefix_toggle = not self._prefix_toggle
return None
def toggle_suffix(self):
if self._suffix_toggle:
self.dlg.suffix_lineEdit.setDisabled(True)
else:
self.dlg.suffix_lineEdit.setDisabled(False)
self._suffix_toggle = not self._suffix_toggle
return None
def toggle_attributes_and_tags(self):
if self._attributes_and_tags_toggle:
self.dlg.add_attributes_checkBox.setDisabled(True)
self.dlg.add_tags_checkBox.setDisabled(True)
self.dlg.add_attributes_checkBox.setChecked(False)
self.dlg.add_tags_checkBox.setChecked(False)
self.dlg.addnodes_tree_decoration1.setDisabled(True)
self.dlg.addnodes_tree_decoration2.setDisabled(True)
self.dlg.addnodes_tree_decoration3.setDisabled(True)
else:
self.dlg.add_attributes_checkBox.setDisabled(False)
self.dlg.add_tags_checkBox.setDisabled(False)
self.dlg.addnodes_tree_decoration1.setDisabled(False)
self.dlg.addnodes_tree_decoration2.setDisabled(False)
self.dlg.addnodes_tree_decoration3.setDisabled(False)
self._attributes_and_tags_toggle = not self._attributes_and_tags_toggle
return None
def toggle_alt_osm2pgr_exec(self):
if self._alt_osm2pgr_exec_toggle:
self.dlg.alt_osm2pgr_exec_lineEdit.setDisabled(True)
else:
self.dlg.alt_osm2pgr_exec_lineEdit.setDisabled(False)
self._alt_osm2pgr_exec_toggle = not self._alt_osm2pgr_exec_toggle
return None
def open_file_chooser(self):
filename = QFileDialog.getOpenFileName(self.dlg, "Select .osm file", "", "*.osm")[0]
if filename:
self.dlg.local_file_lineEdit.setText(filename)
return None
def test_rest_endpoint(self):
rest_url = self.dlg.rest_endpoint_lineEdit.text()
try:
req = requests.get(r"{}?bbox=0,0.0001,0,0.0001".format(rest_url))
if req.ok:
self.dlg.rest_endpoint_test_label.setStyleSheet("font: bold 14px; color: green;")
else:
self.dlg.rest_endpoint_test_label.setStyleSheet("font: bold 14px; color: red;")
self.dlg.rest_endpoint_test_label.setText("{}: {}".format(req.status_code, req.reason))
except requests.exceptions.MissingSchema:
self.dlg.rest_endpoint_test_label.setText("Invalid URL")
self.dlg.rest_endpoint_test_label.setStyleSheet("font: bold 14px; color: red;")
return None
def get_wgs84_bbox(self, extent):
# Get current CRS and set up a CRS transformer for the current CRS and WGS84 (EPSG: 4326)
canvas = self.iface.mapCanvas()
# current_crs = canvas.mapRenderer().destinationCrs().authid()
current_crs = canvas.mapSettings().destinationCrs()
source_crs = QgsCoordinateReferenceSystem(current_crs)
target_crs = QgsCoordinateReferenceSystem(4326)
transformer = QgsCoordinateTransform(source_crs, target_crs, QgsProject.instance())
# Get current extent and transform to WGS84
bottom_left_point = QgsPointXY(extent.xMinimum(), extent.yMinimum())
top_right_point = QgsPointXY(extent.xMaximum(), extent.yMaximum())
bottom_left_point_transformed = transformer.transform(bottom_left_point)
top_right_point_transformed = transformer.transform(top_right_point)
# Extract extent boundaries
top = top_right_point_transformed.y()
left = bottom_left_point_transformed.x()
right = top_right_point_transformed.x()
bottom = bottom_left_point_transformed.y()
return [left, bottom, right, top]
def use_current_extent(self):
extent = self.get_wgs84_bbox(self.iface.mapCanvas().extent())
# Populate extent lineEdits
self.dlg.bounding_box_left_lineEdit.setText(str(extent[0]))
self.dlg.bounding_box_bottom_lineEdit.setText(str(extent[1]))
self.dlg.bounding_box_right_lineEdit.setText(str(extent[2]))
self.dlg.bounding_box_top_lineEdit.setText(str(extent[3]))
return None
def make_new_database(self, dbname, host, port, user, password):
# Log into maintenance database and create the new DB
# TODO: parameterize maintenance DB (even though it will almost certainly be "postgres")
conn_string = "dbname=postgres host={0} port={1} user={2} password={3}".format(host, port, user, password)
with dbconnect(conn_string) as conn:
conn.autocommit = True # connection MUST be in autocommit mode to create databases!
cur = conn.cursor()
cur.execute(sql.SQL("CREATE DATABASE {};").format(sql.Identifier(dbname)))
conn.commit()
cur.close()
# Add the connection to QGIS
settings = QSettings()
settings.setValue("PostgreSQL/connections/{0}/allowGeometrylessTables".format(self.db_credentials["name"]),
"false")
settings.setValue("PostgreSQL/connections/{0}/authcfg".format(self.db_credentials["name"]), "")
settings.setValue("PostgreSQL/connections/{0}/database".format(self.db_credentials["name"]),
self.db_credentials["dbname"])
settings.setValue("PostgreSQL/connections/{0}/dontResolveType".format(self.db_credentials["name"]), "false")
settings.setValue("PostgreSQL/connections/{0}/estimatedMetadata".format(self.db_credentials["name"]), "false")
settings.setValue("PostgreSQL/connections/{0}/geometryColumnsOnly".format(self.db_credentials["name"]), "false")
settings.setValue("PostgreSQL/connections/{0}/host".format(self.db_credentials["name"]),
self.db_credentials["host"])
settings.setValue("PostgreSQL/connections/{0}/password".format(self.db_credentials["name"]),
self.db_credentials["password"])
settings.setValue("PostgreSQL/connections/{0}/port".format(self.db_credentials["name"]),
self.db_credentials["port"])
settings.setValue("PostgreSQL/connections/{0}/publicOnly".format(self.db_credentials["name"]), "false")
settings.setValue("PostgreSQL/connections/{0}/savePassword".format(self.db_credentials["name"]),
self.db_credentials["save_password"])
settings.setValue("PostgreSQL/connections/{0}/saveUsername".format(self.db_credentials["name"]),
self.db_credentials["save_username"])
settings.setValue("PostgreSQL/connections/{0}/service".format(self.db_credentials["name"]),
self.db_credentials["service"])
settings.setValue("PostgreSQL/connections/{0}/sslmode".format(self.db_credentials["name"]), "1")
settings.setValue("PostgreSQL/connections/{0}/username".format(self.db_credentials["name"]),
self.db_credentials["user"])
QCoreApplication.processEvents() # refresh browser panel
return None
def make_db_schema(self, dbname, host, port, user, password, schema):
conn_string = "dbname={0} host={1} port={2} user={3} password={4}".format(dbname, host, port, user, password)
with dbconnect(conn_string) as conn:
cur = conn.cursor()
cur.execute(sql.SQL("CREATE SCHEMA IF NOT EXISTS {};").format(sql.Identifier(schema)))
conn.commit()
cur.close()
return None
def make_db_extensions(self, dbname, host, port, user, password):
conn_string = "dbname={0} host={1} port={2} user={3} password={4}".format(dbname, host, port, user, password)
with dbconnect(conn_string) as conn:
cur = conn.cursor()
cur.execute(sql.SQL("CREATE EXTENSION IF NOT EXISTS postgis; CREATE EXTENSION IF NOT EXISTS pgrouting;"))
conn.commit()
cur.close()
return None
def download_osm_data(self, rest_url, bbox):
print(r"{}?bbox={},{},{},{}"
.format(rest_url, bbox[0], bbox[1], bbox[2], bbox[3]))
req = requests.get(r"{}?bbox={},{},{},{}"
.format(rest_url, bbox[0], bbox[1], bbox[2], bbox[3]))
with open(os.path.join(os.getcwd(), r"osm/data.osm"), "w") as osm_file:
osm_file.write(req.text)
osm_file_path = osm_file.name
return osm_file_path
def get_db_credentials(self, db_name):
db_credentials = {}
qs = QSettings()
k_list = [k for k in sorted(qs.allKeys()) if k[:10] == "PostgreSQL" and k.split("/")[2] == db_name]
for k in k_list:
if k.split("/")[-1] == "database":
db_credentials["dbname"] = qs.value(k)
elif k.split("/")[-1] == "host":
db_credentials["host"] = qs.value(k)
elif k.split("/")[-1] == "port":
db_credentials["port"] = qs.value(k)
elif k.split("/")[-1] == "username":
db_credentials["user"] = qs.value(k)
elif k.split("/")[-1] == "password":
db_credentials["password"] = qs.value(k)
return db_credentials
def add_hstore(self, dbname, host, port, user, password):
conn_string = "dbname={0} host={1} port={2} user={3} password={4}".format(dbname, host, port, user, password)
with dbconnect(conn_string) as conn:
cur = conn.cursor()
cur.execute(sql.SQL("CREATE EXTENSION IF NOT EXISTS hstore;"))
conn.commit()
cur.close()
return None
def set_initial_state(self):
# Radio buttons
self.dlg.local_file_radioButton.setChecked(True)
self.dlg.local_file_lineEdit.setText("")
self.dlg.existing_db_radioButton.setChecked(True)
self.dlg.mapconfig_std_radioButton.setChecked(True)
# Source Data
self.dlg.extent_pushButton.setDisabled(True)
self.dlg.layers_comboBox.setDisabled(True)
self.dlg.bounding_box_top_lineEdit.setDisabled(True)
self.dlg.bounding_box_top_lineEdit.setText("")
self.dlg.bounding_box_left_lineEdit.setDisabled(True)
self.dlg.bounding_box_left_lineEdit.setText("")
self.dlg.bounding_box_right_lineEdit.setDisabled(True)
self.dlg.bounding_box_right_lineEdit.setText("")
self.dlg.bounding_box_bottom_lineEdit.setDisabled(True)
self.dlg.bounding_box_bottom_lineEdit.setText("")
self.dlg.osm_download_label.setDisabled(True)
self.dlg.rest_endpoint_lineEdit.setDisabled(True)
self.dlg.rest_endpoint_lineEdit.setText("https://api.openstreetmap.org/api/0.6/map")
self.dlg.rest_endpoint_test_pushButton.setDisabled(True)
self.dlg.rest_endpoint_test_label.setText("")
self.dlg.rest_endpoint_test_label.setStyleSheet("color: black;")
# Database
self.dlg.overwrite_checkBox.setDisabled(False)
self.dlg.overwrite_checkBox.setChecked(False)
self.dlg.db_listWidget.setDisabled(False)
self.dlg.new_db_name_label.setDisabled(True)
self.dlg.new_db_name_lineEdit.setDisabled(True)
self.dlg.new_db_name_lineEdit.setText("")
self.dlg.new_db_service_label.setDisabled(True)
self.dlg.new_db_service_lineEdit.setDisabled(True)
self.dlg.new_db_service_lineEdit.setText("")
self.dlg.new_db_host_label.setDisabled(True)
self.dlg.new_db_host_lineEdit.setDisabled(True)
self.dlg.new_db_host_lineEdit.setText("")
self.dlg.new_db_port_label.setDisabled(True)
self.dlg.new_db_port_lineEdit.setDisabled(True)
self.dlg.new_db_port_lineEdit.setText("")
self.dlg.new_db_database_label.setDisabled(True)
self.dlg.new_db_database_lineEdit.setDisabled(True)
self.dlg.new_db_database_lineEdit.setText("")
self.dlg.new_db_username_label.setDisabled(True)
self.dlg.new_db_username_lineEdit.setDisabled(True)
self.dlg.new_db_username_lineEdit.setText("")
self.dlg.new_db_password_label.setDisabled(True)
self.dlg.new_db_password_lineEdit.setDisabled(True)
self.dlg.new_db_password_lineEdit.setText("")
self.dlg.new_db_save_username_checkBox.setDisabled(True)
self.dlg.new_db_save_username_checkBox.setChecked(False)
self.dlg.new_db_save_password_checkBox.setDisabled(True)
self.dlg.new_db_save_password_checkBox.setChecked(False)
# Schema
self.dlg.schema_checkBox.setChecked(False)
self.dlg.schema_lineEdit.setText("")
self.dlg.schema_lineEdit.setDisabled(True)
# Prefix
self.dlg.prefix_checkBox.setChecked(False)
self.dlg.prefix_checkBox.setChecked(False)
self.dlg.prefix_lineEdit.setDisabled(True)
# Suffix
self.dlg.suffix_checkBox.setChecked(False)
self.dlg.suffix_lineEdit.setDisabled(True)
# Add attributes / add nodes
self.dlg.nodes_checkBox.setChecked(False)
self.dlg.add_attributes_checkBox.setChecked(False)
self.dlg.add_attributes_checkBox.setDisabled(True)
self.dlg.add_tags_checkBox.setChecked(False)
self.dlg.add_tags_checkBox.setDisabled(True)
self.dlg.addnodes_tree_decoration1.setDisabled(True)
self.dlg.addnodes_tree_decoration2.setDisabled(True)
self.dlg.addnodes_tree_decoration3.setDisabled(True)
# Indexes
self.dlg.no_index_checkBox.setChecked(False)
# Chunk
self.dlg.chunk_size_lineEdit.setText("20000")
# Alternate osm2pgr exec
self.dlg.alt_osm2pgr_exec_checkBox.setChecked(False)
self.dlg.alt_osm2pgr_exec_lineEdit.setText("")
self.dlg.alt_osm2pgr_exec_lineEdit.setDisabled(True)
# Add existing layer extents to combobox
self.dlg.layers_comboBox.clear()
layers = QgsProject.instance().mapLayers()
for layer in layers:
print("{}: {}".format(layers[layer].name(), layer))
self.dlg.layers_comboBox.addItem(layers[layer].name(), layer)
# Database connections
self.dlg.db_listWidget.clear()
k_list = [k for k in sorted(QSettings().allKeys()) if k[:10] == "PostgreSQL" and k[-8:] == "database"]
for k in k_list:
item = QListWidgetItem(k.split("/")[2])
self.dlg.db_listWidget.addItem(item)
return None
def run(self):
"""Run method that performs all the real work"""
# show the dialog
self.dlg.show()
self.set_initial_state()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Get osm file either locally
# TODO: requesting too much data puts an error in data.osm, check for this and inform the user
if self.dlg.local_file_radioButton.isChecked():
osm_file = self.dlg.local_file_lineEdit.text()
# Get osm file from selected layer extent
elif self.dlg.layers_radioButton.isChecked():
osm_rest_endpoint = self.dlg.rest_endpoint_lineEdit.text()
target_layer = QgsProject.instance().mapLayer(
self.dlg.layers_comboBox.itemData(self.dlg.layers_comboBox.currentIndex()))
bbox = self.get_wgs84_bbox(target_layer.extent())
print(bbox)
osm_file = self.download_osm_data(osm_rest_endpoint, bbox)
# Get osm file from other extent
elif self.dlg.other_extent_radioButton.isChecked():
osm_rest_endpoint = self.dlg.rest_endpoint_lineEdit.text()
osm_file = self.download_osm_data(osm_rest_endpoint,
[float(self.dlg.bounding_box_left_lineEdit.text()),
float(self.dlg.bounding_box_bottom_lineEdit.text()),
float(self.dlg.bounding_box_right_lineEdit.text()),
float(self.dlg.bounding_box_top_lineEdit.text())])
# Get credentials if a pre-existing connection from QGIS was selected
if self.dlg.existing_db_radioButton.isChecked():
db_name = self.dlg.db_listWidget.currentItem().text()
self.db_credentials = self.get_db_credentials(db_name)
# Define credentials from dialog and create database if new connection was selected
elif self.dlg.new_db_radioButton.isChecked():
self.db_credentials = {
"name": self.dlg.new_db_name_lineEdit.text(),
"service": self.dlg.new_db_service_lineEdit.text(),
"host": self.dlg.new_db_host_lineEdit.text(),
"port": self.dlg.new_db_port_lineEdit.text(),
"dbname": self.dlg.new_db_database_lineEdit.text(),
"user": self.dlg.new_db_username_lineEdit.text(),
"password": self.dlg.new_db_password_lineEdit.text(),
"save_username": "true" if self.dlg.new_db_save_username_checkBox.isChecked() else False,
"save_password": "true" if self.dlg.new_db_save_password_checkBox.isChecked() else False,
"schema": self.dlg.schema_lineEdit if self.dlg.schema_checkBox.isChecked() else "public",
}
self.make_new_database(self.db_credentials["dbname"], self.db_credentials["host"],
self.db_credentials["port"], self.db_credentials["user"],
self.db_credentials["password"])
# Set map config
if self.dlg.mapconfig_std_radioButton.isChecked():
map_config = os.path.join(os.getcwd(), r"map_configs/mapconfig.xml")
elif self.dlg.mapconfig_cars_radioButton.isChecked():
map_config = os.path.join(os.getcwd(), r"map_configs/mapconfig_for_cars.xml")
elif self.dlg.mapconfig_bicycles_radioButton.isChecked():
map_config = os.path.join(os.getcwd(), r"map_configs/mapconfig_for_bicycles.xml")
# Add the custom schema if it does not exist & the user specified it
if self.dlg.schema_checkBox.isChecked():
schema = self.dlg.schema_lineEdit.text()
self.make_db_schema(self.db_credentials["dbname"], self.db_credentials["host"],
self.db_credentials["port"], self.db_credentials["user"],
self.db_credentials["password"], schema)
else:
schema = "public"
# Add postgis & pgrouting extensions
self.make_db_extensions(self.db_credentials["dbname"], self.db_credentials["host"],
self.db_credentials["port"], self.db_credentials["user"],
self.db_credentials["password"])
# Add hstore extension in case the user specified --attributes or --tags
if self.dlg.add_attributes_checkBox.isChecked() or self.dlg.add_tags_checkBox.isChecked():
self.add_hstore(self.db_credentials["dbname"], self.db_credentials["host"],
self.db_credentials["port"], self.db_credentials["user"],
self.db_credentials["password"])
# Set custom executable if applicable
if self.dlg.alt_osm2pgr_exec_checkBox.isChecked() and self.dlg.alt_osm2pgr_exec_lineEdit.text() is not None:
osm2pgr_exec = self.dlg.alt_osm2pgr_exec_lineEdit.text()
else:
osm2pgr_exec = "osm2pgrouting"
# Build command line statement
osm2pgrouting_parameters = [
osm2pgr_exec,
"--file", osm_file,
"--conf", map_config,
"--schema", schema,
"--dbname", self.db_credentials["dbname"],
"--host", self.db_credentials["host"],
"--username", self.db_credentials["user"],
"--password", self.db_credentials["password"],
"--chunk", self.dlg.chunk_size_lineEdit.text()
]
if self.dlg.overwrite_checkBox.isChecked():
osm2pgrouting_parameters.append("--clean")
if self.dlg.nodes_checkBox.isChecked():
osm2pgrouting_parameters.append("--addnodes")
if self.dlg.no_index_checkBox.isChecked():
osm2pgrouting_parameters.append("--no-index")
if self.dlg.prefix_checkBox.isChecked():
osm2pgrouting_parameters.extend(["--prefix", self.dlg.prefix_lineEdit.text().lower()])
if self.dlg.suffix_checkBox.isChecked():
osm2pgrouting_parameters.extend(["--suffix", self.dlg.suffix_lineEdit.text().lower()])
if self.dlg.add_attributes_checkBox.isChecked():
osm2pgrouting_parameters.extend(["--attributes", self.dlg.suffix_lineEdit.text().lower()])
if self.dlg.add_tags_checkBox.isChecked():
osm2pgrouting_parameters.extend(["--tags", self.dlg.suffix_lineEdit.text().lower()])
print("executing: {}".format(" ".join(osm2pgrouting_parameters)))
# Execute command line statement
osm2pgrouting_process = subprocess.Popen(osm2pgrouting_parameters, stdout=subprocess.PIPE)
for line in iter(osm2pgrouting_process.stdout.readline, ''):
if str(line) != "b''":
sys.stdout.write(str(line))
else:
break
# Remove the file only if it was downloaded
if self.dlg.layers_radioButton.isChecked() or self.dlg.other_extent_radioButton.isChecked():
os.remove(osm_file)
pass