forked from minorua/TileLayerPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addlayerdialog.py
146 lines (127 loc) · 5.57 KB
/
addlayerdialog.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
TileLayer Plugin
A QGIS plugin
Plugin layer for Tile Maps
-------------------
begin : 2012-12-16
copyright : (C) 2013 by Minoru Akagi
email : akaginch@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.QtCore import QDir, QFile, QSettings
from PyQt4.QtGui import QDialog, QHeaderView, QStandardItem, QStandardItemModel
from qgis.core import QgsMessageLog
from ui_addlayerdialog import Ui_Dialog
import os
import codecs
from tiles import BoundingBox, TileLayerDefinition
debug_mode = 1
class AddLayerDialog(QDialog):
def __init__(self, plugin):
QDialog.__init__(self, plugin.iface.mainWindow())
self.plugin = plugin
# set up the user interface
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButton_Add.clicked.connect(self.accept)
self.ui.pushButton_Close.clicked.connect(self.reject)
self.ui.pushButton_Settings.clicked.connect(self.settingsClicked)
self.ui.treeView.doubleClicked.connect(self.treeItemDoubleClicked)
self.setupTreeView()
def setupTreeView(self):
# tree view header labels
headers = [self.tr("Title"), self.tr("Attribution"), self.tr("Url"), self.tr("Zoom"), self.tr("Extent"), self.tr("yOrigin")] + ["index"]
self.indexColumn = len(headers) - 1
self.model = QStandardItemModel(0, len(headers))
self.model.setHorizontalHeaderLabels(headers)
self.serviceInfoList = []
# import layer definitions from external layer definition directory, and append it into the tree
extDir = QSettings().value("/TileLayerPlugin/extDir", "", type=unicode)
if extDir:
self.importFromDirectory(extDir)
# import layer definitions from TileLayerPlugin/layers directory, and append it into the tree
pluginDir = os.path.dirname(QFile.decodeName(__file__))
self.importFromDirectory(os.path.join(pluginDir, "layers"))
# model and style settings
self.ui.treeView.setModel(self.model)
self.ui.treeView.header().setResizeMode(QHeaderView.ResizeToContents)
self.ui.treeView.expandAll()
def importFromDirectory(self, path):
d = QDir(path)
d.setFilter(QDir.Files | QDir.Hidden)
#d.setSorting(QDir.Size | QDir.Reversed)
for fileInfo in d.entryInfoList():
if debug_mode == 0 and fileInfo.fileName() == "debug.tsv":
continue
if fileInfo.suffix().lower() == "tsv":
self.importFromTsv(fileInfo.filePath())
# Line Format is:
# title attribution url [yOriginTop [zmin zmax [xmin ymin xmax ymax ]]]
def importFromTsv(self, filename):
# append file item
rootItem = self.model.invisibleRootItem()
basename = os.path.basename(filename)
parent = QStandardItem(os.path.splitext(basename)[0])
rootItem.appendRow([parent])
# load service info from tsv file
try:
with codecs.open(filename, "r", "utf-8") as f:
lines = f.readlines()
except Exception as e:
QgsMessageLog.logMessage(self.tr("Fail to read {0}: {1}").format(basename, unicode(e)), self.tr("TileLayerPlugin"))
return False
for i, line in enumerate(lines):
if line.startswith("#"):
continue
vals = line.rstrip().split("\t")
nvals = len(vals)
try:
if nvals < 3:
raise
title, attribution, url = vals[0:3]
if not url:
raise
if nvals < 4:
serviceInfo = TileLayerDefinition(title, attribution, url)
else:
yOriginTop = int(vals[3])
if nvals < 6:
serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop)
else:
zmin, zmax = map(int, vals[4:6])
if nvals < 10:
serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop, zmin, zmax)
else:
bbox = BoundingBox.fromString(",".join(vals[6:10]))
serviceInfo = TileLayerDefinition(title, attribution, url, yOriginTop, zmin, zmax, bbox)
except:
QgsMessageLog.logMessage(self.tr("Invalid line format: {} line {}").format(basename, i + 1), self.tr("TileLayerPlugin"))
continue
# append the service info into the tree
vals = serviceInfo.toArrayForTreeView() + [len(self.serviceInfoList)]
rowItems = map(QStandardItem, map(unicode, vals))
parent.appendRow(rowItems)
self.serviceInfoList.append(serviceInfo)
return True
def selectedLayerDefinitions(self):
list = []
for idx in self.ui.treeView.selectionModel().selection().indexes():
if idx.column() == self.indexColumn and idx.data() is not None:
list.append(self.serviceInfoList[int(idx.data())])
return list
def settingsClicked(self):
if self.plugin.settings():
self.setupTreeView()
def treeItemDoubleClicked(self, index):
if len(self.selectedLayerDefinitions()) > 0:
self.accept()