forked from minorua/TileLayerPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilelayerplugin.py
183 lines (153 loc) · 6.85 KB
/
tilelayerplugin.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
TileLayerPlugin
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. *
* *
***************************************************************************/
"""
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# Initialize Qt resources from file resources.py
import resources_rc
import os.path
from tilelayer import *
debug_mode = 1
class TileLayerPlugin:
VERSION = "0.30"
def __init__(self, iface):
self.apiChanged23 = QGis.QGIS_VERSION_INT >= 20300
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(QFile.decodeName(__file__))
# initialize locale
settings = QSettings()
locale = settings.value("locale/userLocale")[0:2]
localePath = os.path.join(self.plugin_dir, 'i18n', 'tilelayerplugin_{0}.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.pluginName = self.tr("TileLayerPlugin")
self.downloadTimeout = int(settings.value("/TileLayerPlugin/timeout", 30, type=int))
self.navigationMessagesEnabled = int(settings.value("/TileLayerPlugin/naviMsg", Qt.Checked, type=int))
self.crs3857 = None
self.layers = {}
QObject.connect(QgsMapLayerRegistry.instance(), SIGNAL("layerRemoved(QString)"), self.layerRemoved)
def initGui(self):
# Create actions
self.action = QAction(
QIcon(":/plugins/tilelayerplugin/icon.png"),
self.tr("Add Tile Layer..."), self.iface.mainWindow())
# set object name
self.action.setObjectName("TileLayerPlugin_AddLayer")
# connect the actions to the methods
self.action.triggered.connect(self.run)
# Add toolbar button and menu item
if QSettings().value("/TileLayerPlugin/moveToLayer", 0, type=int):
self.iface.insertAddLayerAction(self.action)
self.iface.layerToolBar().addAction(self.action)
else:
self.iface.addPluginToWebMenu(self.pluginName, self.action)
# Register plugin layer type
self.tileLayerType = TileLayerType(self)
QgsPluginLayerRegistry.instance().addPluginLayerType(self.tileLayerType)
def unload(self):
# Remove the plugin menu item and icon
if QSettings().value("/TileLayerPlugin/moveToLayer", 0, type=int):
self.iface.layerToolBar().removeAction(self.action)
self.iface.removeAddLayerAction(self.action)
else:
self.iface.removePluginWebMenu(self.pluginName, self.action)
# Unregister plugin layer type
QgsPluginLayerRegistry.instance().removePluginLayerType(TileLayer.LAYER_TYPE)
QObject.disconnect(QgsMapLayerRegistry.instance(), SIGNAL("layerRemoved(QString)"), self.layerRemoved)
def layerRemoved(self, layerId):
if layerId in self.layers:
del self.layers[layerId]
if debug_mode:
qDebug("Layer %s removed" % layerId.encode("UTF-8"))
def run(self):
from addlayerdialog import AddLayerDialog
dialog = AddLayerDialog(self)
dialog.show()
accepted = dialog.exec_()
if not accepted:
return
if self.crs3857 is None:
self.crs3857 = QgsCoordinateReferenceSystem(3857)
creditVisibility = dialog.ui.checkBox_CreditVisibility.isChecked()
for serviceInfo in dialog.selectedServiceInfoList():
layer = TileLayer(self, serviceInfo, creditVisibility)
if layer.isValid():
QgsMapLayerRegistry.instance().addMapLayer(layer)
self.layers[layer.id()] = layer
def settings(self):
oldMoveToLayer = QSettings().value("/TileLayerPlugin/moveToLayer", 0, type=int)
from settingsdialog import SettingsDialog
dialog = SettingsDialog(self.iface)
accepted = dialog.exec_()
if not accepted:
return False
self.downloadTimeout = dialog.ui.spinBox_downloadTimeout.value()
self.navigationMessagesEnabled = dialog.ui.checkBox_NavigationMessages.checkState()
moveToLayer = dialog.ui.checkBox_MoveToLayer.checkState()
if moveToLayer != oldMoveToLayer:
if oldMoveToLayer:
self.iface.layerToolBar().removeAction(self.action)
self.iface.removeAddLayerAction(self.action)
else:
self.iface.removePluginWebMenu(self.pluginName, self.action)
if moveToLayer:
self.iface.insertAddLayerAction(self.action)
self.iface.layerToolBar().addAction(self.action)
else:
self.iface.addPluginToWebMenu(self.pluginName, self.action)
return True
def setCrs(self, crs):
if self.apiChanged23:
mapCanvas = self.iface.mapCanvas()
currentCrs = mapCanvas.mapSettings().destinationCrs()
if currentCrs == crs:
return
# enable "on the fly"
mapCanvas.setCrsTransformEnabled(True)
# set crs
mapCanvas.freeze()
mapCanvas.setDestinationCrs(crs)
if crs.mapUnits() != QGis.UnknownUnit:
mapCanvas.setMapUnits(crs.mapUnits())
mapCanvas.freeze(False)
else:
mapCanvas = self.iface.mapCanvas()
currentCrs = mapCanvas.mapRenderer().destinationCrs()
if currentCrs == crs:
return
# enable "on the fly"
mapCanvas.mapRenderer().setProjectionsEnabled(True)
# set crs
mapCanvas.freeze()
mapCanvas.mapRenderer().setDestinationCrs(crs)
if crs.mapUnits() != QGis.UnknownUnit:
mapCanvas.setMapUnits(crs.mapUnits())
mapCanvas.freeze(False)
msg = self.tr("Project CRS has been changed to EPSG:3857.")
self.iface.messageBar().pushMessage(self.pluginName, msg, QgsMessageBar.INFO, 5)
def tr(self, msg):
return QCoreApplication.translate("TileLayerPlugin", msg)