def __init__(self, iface): # 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', '{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 = {} # register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType( self.tileLayerType) # connect signal-slot QgsMapLayerRegistry.instance().layerRemoved.connect(self.layerRemoved)
def __init__(self, iface): # 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', '{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 = {} # register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType(self.tileLayerType) # connect signal-slot QgsMapLayerRegistry.instance().layerRemoved.connect(self.layerRemoved)
def addPluginLayer(self): from .viewer.q3dlayer import Qgis2threejsLayer, Qgis2threejs25DLayerType self.initManagers() if self.pluginLayerType is None: # register plugin layer self.pluginLayerType = Qgis2threejs25DLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType(self.pluginLayerType) layer = self.iface.activeLayer() valid = True if layer is None or layer.type() == QgsMapLayer.PluginLayer: valid = False elif layer.type() == QgsMapLayer.RasterLayer: if layer.providerType() != "gdal" or layer.bandCount() != 1: valid = False if not valid: QMessageBox.information(None, "Qgis2threejs", "Select a DEM/Vector layer.") return # select camera mode perspective = QMessageBox.question(None, "Select Camera", "Use perspective camera?\n[Yes] -> Perspective camera\n[No] -> Orthographic camera", QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) == QMessageBox.Yes # create a plugin layer self.lastLayerIndex += 1 serverName = "Qgis2threejsLayer{0}_{1}".format(os.getpid(), self.lastLayerIndex) layer = Qgis2threejsLayer(self, serverName, perspective) QgsProject.instance().addMapLayer(layer) self.layers[layer.id()] = layer # TODO: remove item from dict when the layer is removed from registry
def unload(self): self.iface.removeWebToolBarIcon(self._mainAction) self.iface.removeWebToolBarIcon(self._loadDataAction) self.iface.removeWebToolBarIcon(self._createVizAction) self.iface.removeWebToolBarIcon(self._addSQLAction) self.iface.webMenu().removeAction(self._cdbMenu.menuAction()) self.iface.removeWebToolBarIcon(self._toolbarAction) # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType(CartoDBPluginLayer.LAYER_TYPE)
def initGui(self): self._cdbMenu = QMenu("CartoDB plugin", self.iface.mainWindow()) self._cdbMenu.setIcon(QIcon(":/plugins/qgis-cartodb/images/icon.png")) self._mainAction = QAction(self.tr('Add CartoDB Layer'), self.iface.mainWindow()) self._mainAction.setIcon(QIcon(":/plugins/qgis-cartodb/images/icons/add.png")) self._loadDataAction = QAction(self.tr('Upload layers to CartoDB'), self.iface.mainWindow()) self._loadDataAction.setIcon(QIcon(":/plugins/qgis-cartodb/images/icons/upload.png")) self._createVizAction = QAction(self.tr('Create New Map'), self.iface.mainWindow()) self._createVizAction.setIcon(QIcon(":/plugins/qgis-cartodb/images/icons/map.png")) self._addSQLAction = QAction(self.tr('Add SQL CartoDB Layer'), self.iface.mainWindow()) self._addSQLAction.setIcon(QIcon(":/plugins/qgis-cartodb/images/icons/sql.png")) self.toolbar = CartoDBToolbar() self.toolbar.setClick(self.connectionManager) self.toolbar.error.connect(self.toolbarError) self._toolbarAction = self.iface.addWebToolBarWidget(self.toolbar) worker = CartoDBPluginWorker(self.toolbar, 'connectCartoDB') worker.start() if not self.toolbar.isCurrentUserValid(): self._mainAction.setEnabled(False) self._loadDataAction.setEnabled(False) self._createVizAction.setEnabled(False) self._addSQLAction.setEnabled(False) self._mainAction.triggered.connect(self.run) self._loadDataAction.triggered.connect(self.upload) self._createVizAction.triggered.connect(self.createNewMap) self._addSQLAction.triggered.connect(self.addSQL) self._cdbMenu.addAction(self._mainAction) self._cdbMenu.addAction(self._loadDataAction) self._cdbMenu.addAction(self._createVizAction) self._cdbMenu.addAction(self._addSQLAction) try: self.iface.layerToolBar().addAction(self._mainAction) except: self.iface.addWebToolBarIcon(self._mainAction) self.iface.addWebToolBarIcon(self._loadDataAction) self.iface.addWebToolBarIcon(self._createVizAction) try: self.iface.layerToolBar().addAction(self._addSQLAction) except: self.iface.addWebToolBarIcon(self._addSQLAction) # Create Web menu, if it doesn't exist yet tmpAction = QAction("Temporal", self.iface.mainWindow()) self.iface.addPluginToWebMenu("_tmp", tmpAction) self._menu = self.iface.webMenu() self._menu.addMenu(self._cdbMenu) self.iface.removePluginWebMenu("_tmp", tmpAction) # Register plugin layer type self.pluginLayerType = CartoDBPluginLayerType(self.iface, self.createLayerCB) QgsPluginLayerRegistry.instance().addPluginLayerType(self.pluginLayerType)
def unload(self): self.iface.removeWebToolBarIcon(self._mainAction) self.iface.removeWebToolBarIcon(self._loadDataAction) self.iface.removeWebToolBarIcon(self._createVizAction) self.iface.removeWebToolBarIcon(self._addSQLAction) self.iface.webMenu().removeAction(self._cdbMenu.menuAction()) self.iface.removeWebToolBarIcon(self._toolbarAction) # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType( CartoDBPluginLayer.LAYER_TYPE)
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) # disconnect signal-slot QgsMapLayerRegistry.instance().layerRemoved.disconnect(self.layerRemoved)
def unload(self): # remove menu/ self.remove_menu_buttons() # clean vars self.menu = None self.toolbutton = None self.service_actions = None self.ds_list = None self.groups_list = None self.service_layers = None # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType(TileLayer.LAYER_TYPE)
def initGui(self): #import pydevd #pydevd.settrace('localhost', port=9921, stdoutToServer=True, stderrToServer=True, suspend=False) # Register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType(self.tileLayerType) # Create menu self.build_menu_tree() # add to QGIS menu/toolbars self.append_menu_buttons()
def unload(self): # remove menu/ self.remove_menu_buttons() # clean vars self.menu = None self.toolbutton = None self.service_actions = None self.ds_list = None self.groups_list = None self.service_layers = None # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType( TileLayer.LAYER_TYPE)
def unload(self): # remove menu self.iface.webMenu().removeAction(self.menu.menuAction()) # remove toolbar button self.iface.webToolBar().removeAction(self.tb_action) # clean vars self.menu = None self.toolbutton = None self.service_actions = None self.ds_list = None self.groups_list = None self.service_layers = None # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType(TileLayer.LAYER_TYPE)
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) # disconnect signal-slot QgsMapLayerRegistry.instance().layerRemoved.disconnect( self.layerRemoved)
def unload(self): # remove menu self.iface.webMenu().removeAction(self.menu.menuAction()) # remove toolbar button self.iface.webToolBar().removeAction(self.tb_action) # clean vars self.menu = None self.toolbutton = None self.service_actions = None self.ds_list = None self.groups_list = None self.service_layers = None # Unregister plugin layer type QgsPluginLayerRegistry.instance().removePluginLayerType( TileLayer.LAYER_TYPE)
def unload(self): """Removes the plugin menu item and icon from QGIS GUI.""" for action in self.actions: self.iface.removePluginWebMenu( self.menu, action) self.iface.removeToolBarIcon(action) self.iface.webMenu().removeAction(self.menuObject.menuAction()) # remove the toolbar del self.toolbar del self.menuObject QgsPluginLayerRegistry.instance().removePluginLayerType(QOSMTileLayer.LAYER_TYPE) QgsMapLayerRegistry.instance().layersWillBeRemoved["QStringList"].disconnect(self.cleanLayerResources)
def initGui(self): #import pydevd #pydevd.settrace('localhost', port=9921, stdoutToServer=True, stderrToServer=True, suspend=False) # Register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType(self.tileLayerType) # Create menu icon_path = self.plugin_dir + '/icons/mActionAddLayer.png' self.menu = QMenu(self.tr(u'QuickMapServices')) self.menu.setIcon(QIcon(icon_path)) self.build_menu_tree() # add to QGIS menu/toolbars self.append_menu_buttons()
def initGui(self): #import pydevd #pydevd.settrace('localhost', port=9921, stdoutToServer=True, stderrToServer=True, suspend=False) # Register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType( self.tileLayerType) # Create menu icon_path = self.plugin_dir + '/icons/mActionAddLayer.png' self.menu = QMenu(self.tr(u'QuickMapServices')) self.menu.setIcon(QIcon(icon_path)) self.build_menu_tree() # add to QGIS menu/toolbars self.append_menu_buttons()
def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Keep a reference to all OL layers to avoid GC self._ol_layers = [] # initialize locale locale = QSettings().value("locale/userLocale")[0:2] localePath = os.path.join(self.plugin_dir, "i18n", "openlayers_{}.qm".format(locale)) if os.path.exists(localePath): self.translator = QTranslator() self.translator.load(localePath) self._olLayerTypeRegistry = WebLayerTypeRegistry(self) self.olOverview = OLOverview(iface, self._olLayerTypeRegistry) self.dlgAbout = AboutDialog() self.pluginLayerRegistry = QgsPluginLayerRegistry()
def initGui(self): """Create the menu entries and toolbar icons inside the QGIS GUI.""" #start logging qosmlogging.initialize_logging() qosmlogging.log("Initizlizing GUI") self.settingsdialog = DialogSettings(None) #add menu to web self.menuObject = QMenu(self.menu) self.menuObject.setIcon(QIcon(":/plugins/qosm/icon.png")) tmpAction = QAction("_tmp", self.iface.mainWindow()) self.iface.addPluginToWebMenu("_tmp", tmpAction) self._menu = self.iface.webMenu() self._menu.addMenu(self.menuObject) self.iface.removePluginWebMenu("_tmp", tmpAction) self.add_action( ':/plugins/qosm/icon_newlayer.png', text=self.tr(u'Add OSM tile layer'), callback=self.addOSMLayer, parent=self.iface.mainWindow()) self.add_action( ':/plugins/qosm/icon_refresh.png', text=self.tr(u'Refresh OSM tile layers'), callback=self.refreshOSMLayers, parent=self.iface.mainWindow()) self.add_action( ':/plugins/qosm/icon_settings.png', text=self.tr(u'QOSM Settings'), callback=self.qosmSettings, parent=self.iface.mainWindow()) self.pluginLayerType = QOSMTileLayerType(self, self.onAddOSMLayer) QgsPluginLayerRegistry.instance().addPluginLayerType(self.pluginLayerType) QgsMapLayerRegistry.instance().layersWillBeRemoved["QStringList"].connect(self.cleanLayerResources)
def load_osm_plugin(self): """ Initialize the QGIS OpenLayer plugin and load the basemap layer """ canvas = self.canvas class IFace(object): def mapCanvas(self): return canvas iface = IFace() olplugin = OpenlayersPlugin(iface) # hacked from #OpenlayersPlugin#initGui if not olplugin._OpenlayersPlugin__setCoordRSGoogle(): utils.alert("Error in setting coordinate system") oltype = OpenlayersPluginLayerType( iface, olplugin.setReferenceLayer, olplugin._OpenlayersPlugin__coordRSGoogle, olplugin.olLayerTypeRegistry) QgsPluginLayerRegistry.instance().addPluginLayerType(oltype) return olplugin
def initGui(self): #import pydevd #pydevd.settrace('localhost', port=9921, stdoutToServer=True, stderrToServer=True, suspend=False) """Create the menu entries and toolbar icons inside the QGIS GUI.""" # Main Menu icon_path = self.plugin_dir + '/icons/mActionAddLayer.png' self.menu = QMenu(self.tr(u'QuickMapServices')) self.menu.setIcon(QIcon(icon_path)) # DataSources Actions # Register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType(self.tileLayerType) self.groups_list = DsGroupsList(self.locale, self.custom_translator) self.ds_list = DataSourcesList(self.locale, self.custom_translator) data_sources = self.ds_list.data_sources.values() data_sources.sort(key=lambda x: x.alias or x.id) for ds in data_sources: ds.action.triggered.connect(self.insert_layer) gr_menu = self.groups_list.get_group_menu(ds.group) gr_menu.addAction(ds.action) if gr_menu not in self.menu.children(): self.menu.addMenu(gr_menu) # Scales, Settings and About actions icon_set_nearest_scale_path = self.plugin_dir + '/icons/mActionSettings.png' # TODO change icon set_nearest_scale_act = QAction(QIcon(icon_set_nearest_scale_path), self.tr('Set proper scale'), self.iface.mainWindow()) set_nearest_scale_act.triggered.connect(self.set_nearest_scale) self.menu.addAction(set_nearest_scale_act) # TODO: uncomment after fix self.service_actions.append(set_nearest_scale_act) icon_scales_path = self.plugin_dir + '/icons/mActionSettings.png' # TODO change icon scales_act = QAction(QIcon(icon_scales_path), self.tr('Set SlippyMap scales'), self.iface.mainWindow()) scales_act.triggered.connect(self.set_tms_scales) #self.menu.addAction(scales_act) # TODO: uncomment after fix self.service_actions.append(scales_act) icon_settings_path = self.plugin_dir + '/icons/mActionSettings.png' settings_act = QAction(QIcon(icon_settings_path), self.tr('Settings'), self.iface.mainWindow()) self.service_actions.append(settings_act) #self.menu.addAction(settings_act) icon_about_path = self.plugin_dir + '/icons/mActionAbout.png' info_act = QAction(QIcon(icon_about_path), self.tr('About'), self.iface.mainWindow()) self.service_actions.append(info_act) info_act.triggered.connect(self.info_dlg.show) self.menu.addAction(info_act) # add to QGIS menu self.iface.addPluginToWebMenu("_tmp", info_act) self.iface.webMenu().addMenu(self.menu) self.iface.removePluginWebMenu("_tmp", info_act) # add to QGIS toolbar toolbutton = QToolButton() toolbutton.setPopupMode(QToolButton.InstantPopup) toolbutton.setMenu(self.menu) toolbutton.setIcon(self.menu.icon()) toolbutton.setText(self.menu.title()) toolbutton.setToolTip(self.menu.title()) self.tb_action = self.iface.webToolBar().addWidget(toolbutton)
def initGui(self): self._cdbMenu = QMenu("CartoDB plugin", self.iface.mainWindow()) self._cdbMenu.setIcon(QIcon(":/plugins/qgis-cartodb/images/icon.png")) self._mainAction = QAction(self.tr('Add CartoDB Layer'), self.iface.mainWindow()) self._mainAction.setIcon( QIcon(":/plugins/qgis-cartodb/images/icons/add.png")) self._loadDataAction = QAction(self.tr('Upload layers to CartoDB'), self.iface.mainWindow()) self._loadDataAction.setIcon( QIcon(":/plugins/qgis-cartodb/images/icons/upload.png")) self._createVizAction = QAction(self.tr('Create New Map'), self.iface.mainWindow()) self._createVizAction.setIcon( QIcon(":/plugins/qgis-cartodb/images/icons/map.png")) self._addSQLAction = QAction(self.tr('Add SQL CartoDB Layer'), self.iface.mainWindow()) self._addSQLAction.setIcon( QIcon(":/plugins/qgis-cartodb/images/icons/sql.png")) self.toolbar.setClick(self.connectionManager) self.toolbar.error.connect(self.toolbarError) self._toolbarAction = self.iface.addWebToolBarWidget(self.toolbar) worker = CartoDBPluginWorker(self.toolbar, 'connectCartoDB') worker.start() if not self.toolbar.isCurrentUserValid(): self._mainAction.setEnabled(False) self._loadDataAction.setEnabled(False) self._createVizAction.setEnabled(False) self._addSQLAction.setEnabled(False) self._mainAction.triggered.connect(self.run) self._loadDataAction.triggered.connect(self.upload) self._createVizAction.triggered.connect(self.createNewMap) self._addSQLAction.triggered.connect(self.addSQL) self._cdbMenu.addAction(self._mainAction) self._cdbMenu.addAction(self._loadDataAction) self._cdbMenu.addAction(self._createVizAction) self._cdbMenu.addAction(self._addSQLAction) try: self.iface.layerToolBar().addAction(self._mainAction) except: self.iface.addWebToolBarIcon(self._mainAction) self.iface.addWebToolBarIcon(self._loadDataAction) self.iface.addWebToolBarIcon(self._createVizAction) try: self.iface.layerToolBar().addAction(self._addSQLAction) except: self.iface.addWebToolBarIcon(self._addSQLAction) # Create Web menu, if it doesn't exist yet tmpAction = QAction("Temporal", self.iface.mainWindow()) self.iface.addPluginToWebMenu("_tmp", tmpAction) self._menu = self.iface.webMenu() self._menu.addMenu(self._cdbMenu) self.iface.removePluginWebMenu("_tmp", tmpAction) # Register plugin layer type self.pluginLayerType = CartoDBPluginLayerType(self.iface, self.createLayerCB) QgsPluginLayerRegistry.instance().addPluginLayerType( self.pluginLayerType)
class OpenlayersPlugin: def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Keep a reference to all OL layers to avoid GC self._ol_layers = [] # initialize locale locale = QSettings().value("locale/userLocale")[0:2] localePath = os.path.join(self.plugin_dir, "i18n", "openlayers_{}.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._olLayerTypeRegistry = WebLayerTypeRegistry(self) self.olOverview = OLOverview(iface, self._olLayerTypeRegistry) self.dlgAbout = AboutDialog() self.pluginLayerRegistry = QgsPluginLayerRegistry() def initGui(self): self._olMenu = QMenu("TMS for Korea") self._olMenu.setIcon(QIcon(":/plugins/openlayers/openlayers.png")) # Overview self.overviewAddAction = QAction( QApplication.translate("OpenlayersPlugin", "OpenLayers Overview"), self.iface.mainWindow()) self.overviewAddAction.setCheckable(True) self.overviewAddAction.setChecked(False) self.overviewAddAction.toggled.connect(self.olOverview.setVisible) self._olMenu.addAction(self.overviewAddAction) self._actionAbout = QAction( QApplication.translate("dlgAbout", "About OpenLayers Plugin"), self.iface.mainWindow()) self._actionAbout.triggered.connect(self.dlgAbout.show) self._olMenu.addAction(self._actionAbout) self.dlgAbout.finished.connect(self._publicationInfoClosed) # Kakao Maps - 5181 self._olLayerTypeRegistry.register(OlDaumStreetLayer()) self._olLayerTypeRegistry.register(OlDaumHybridLayer()) self._olLayerTypeRegistry.register(OlDaumSatelliteLayer()) self._olLayerTypeRegistry.register(OlDaumPhysicalLayer()) self._olLayerTypeRegistry.register(OlDaumCadstralLayer()) # Naver Maps - 3857(New) self._olLayerTypeRegistry.register(OlNaverStreetLayer()) self._olLayerTypeRegistry.register(OlNaverHybridLayer()) self._olLayerTypeRegistry.register(OlNaverSatelliteLayer()) self._olLayerTypeRegistry.register(OlNaverPhysicalLayer()) self._olLayerTypeRegistry.register(OlNaverCadastralLayer()) # Naver Maps - 5179(Old) self._olLayerTypeRegistry.register(OlNaverStreet5179Layer()) self._olLayerTypeRegistry.register(OlNaverHybrid5179Layer()) self._olLayerTypeRegistry.register(OlNaverSatellite5179Layer()) self._olLayerTypeRegistry.register(OlNaverPhysical5179Layer()) self._olLayerTypeRegistry.register(OlNaverCadastral5179Layer()) # VWorld - 3857 self._olLayerTypeRegistry.register(OlVWorldStreetLayer()) self._olLayerTypeRegistry.register(OlVWorldSatelliteLayer()) self._olLayerTypeRegistry.register(OlVWorldGrayLayer()) self._olLayerTypeRegistry.register(OlVWorldHybridLayer()) # NGII - 5179 self._olLayerTypeRegistry.register(OlNgiiStreetLayer()) self._olLayerTypeRegistry.register(OlNgiiBlankLayer()) self._olLayerTypeRegistry.register(OlNgiiEnglishLayer()) self._olLayerTypeRegistry.register(OlNgiiHighDensityLayer()) self._olLayerTypeRegistry.register(OlNgiiColorBlindLayer()) # Mango - 3857 #self._olLayerTypeRegistry.register(OlMangoBaseMapLayer()) #self._olLayerTypeRegistry.register(OlMangoBaseMapGrayLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapGrayLayer()) for group in self._olLayerTypeRegistry.groups(): groupMenu = group.menu() for layer in self._olLayerTypeRegistry.groupLayerTypes(group): layer.addMenuEntry(groupMenu, self.iface.mainWindow()) self._olMenu.addMenu(groupMenu) # Create Web menu, if it doesn't exist yet self.iface.addPluginToWebMenu("_tmp", self._actionAbout) self._menu = self.iface.webMenu() self._menu.addMenu(self._olMenu) self.iface.removePluginWebMenu("_tmp", self._actionAbout) # Register plugin layer type self.pluginLayerType = OpenlayersPluginLayerType( self.iface, self.setReferenceLayer, self._olLayerTypeRegistry) self.pluginLayerRegistry.addPluginLayerType(self.pluginLayerType) QgsProject.instance().readProject.connect(self.projectLoaded) QgsProject.instance().projectSaved.connect(self.projectSaved) def unload(self): self.iface.webMenu().removeAction(self._olMenu.menuAction()) self.olOverview.setVisible(False) del self.olOverview # Unregister plugin layer type self.pluginLayerRegistry.removePluginLayerType( OpenlayersLayer.LAYER_TYPE) QgsProject.instance().readProject.disconnect(self.projectLoaded) QgsProject.instance().projectSaved.disconnect(self.projectSaved) def addLayer(self, layerType): if layerType.hasXYZUrl(): # create XYZ layer layer, url = self.createXYZLayer(layerType, layerType.displayName) else: # create OpenlayersLayer layer = OpenlayersLayer(self.iface, self._olLayerTypeRegistry) layer.setName(layerType.displayName) layer.setLayerType(layerType) if layer.isValid(): coordRefSys = layerType.coordRefSys(self.canvasCrs()) self.setMapCrs(coordRefSys) QgsProject.instance().addMapLayer(layer) self._ol_layers += [layer] # last added layer is new reference self.setReferenceLayer(layer) def setReferenceLayer(self, layer): self.layer = layer def removeLayer(self, layerId): if self.layer is not None: if self.layer.id() == layerId: self.layer = None # TODO: switch to next available OpenLayers layer? def canvasCrs(self): mapCanvas = self.iface.mapCanvas() crs = mapCanvas.mapSettings().destinationCrs() return crs def setMapCrs(self, targetCRS): mapCanvas = self.iface.mapCanvas() mapExtent = mapCanvas.extent() sourceCRS = self.canvasCrs() QgsProject.instance().setCrs(targetCRS) mapCanvas.freeze(False) try: coordTrans = QgsCoordinateTransform(sourceCRS, targetCRS, QgsProject.instance()) mapExtent = coordTrans.transform( mapExtent, QgsCoordinateTransform.ForwardTransform) mapCanvas.setExtent(mapExtent) except: pass def projectLoaded(self): # replace old OpenlayersLayer with XYZ layer(OL plugin <= 1.3.6) rootGroup = self.iface.layerTreeView().layerTreeModel().rootGroup() for layer in QgsProject.instance().mapLayers().values(): if layer.type( ) == QgsMapLayer.PluginLayer and layer.pluginLayerType( ) == OpenlayersLayer.LAYER_TYPE: if layer.layerType.hasXYZUrl(): # replace layer xyzLayer, url = self.createXYZLayer( layer.layerType, layer.name()) if xyzLayer.isValid(): self.replaceLayer(rootGroup, layer, xyzLayer) def _hasOlLayer(self): for layer in QgsProject.instance().mapLayers().values(): if layer.customProperty("ol_layer_type"): return True return False def _publicationInfo(self): cloud_info_off = QSettings().value("Plugin-OpenLayers/cloud_info_off", defaultValue=False, type=bool) day = 3600 * 24 now = time.time() lastInfo = QSettings().value("Plugin-OpenLayers/cloud_info_ts", defaultValue=0.0, type=float) if lastInfo == 0.0: lastInfo = now - 20 * day # Show first time after 10 days QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", lastInfo) days = (now - lastInfo) / day if days >= 30 and not cloud_info_off: self.dlgAbout.tabWidget.setCurrentWidget( self.dlgAbout.tab_publishing) self.dlgAbout.show() QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", now) def _publicationInfoClosed(self): QSettings().setValue("Plugin-OpenLayers/cloud_info_off", self.dlgAbout.cb_publishing.isChecked()) def projectSaved(self): if self._hasOlLayer(): self._publicationInfo() def createXYZLayer(self, layerType, name): # create XYZ layer with tms url as uri provider = "wms" # isinstance(P, (list, tuple, np.ndarray)) xyzUrls = layerType.xyzUrlConfig() layerName = name tilePixelRatio = layerType.tilePixelRatio coordRefSys = layerType.coordRefSys(self.canvasCrs()) self.setMapCrs(coordRefSys) if isinstance(xyzUrls, (list)): # create group layer root = QgsProject.instance().layerTreeRoot() layer = root.addGroup(layerType.groupName) i = 0 for xyzUrl in xyzUrls: tmsLayerName = layerName # https://github.com/qgis/QGIS/blob/master/src/providers/wms/qgsxyzconnectiondialog.cpp uri = "url=" + xyzUrl + "&zmax=18&zmin=0&type=xyz" if (tilePixelRatio > 0): uri = uri + "&tilePixelRatio=" + str(tilePixelRatio) if i > 0: tmsLayerName = layerName + " Label" tmsLayer = QgsRasterLayer(uri, tmsLayerName, provider, QgsRasterLayer.LayerOptions()) tmsLayer.setCustomProperty("ol_layer_type", tmsLayerName) layer.insertChildNode(0, QgsLayerTreeLayer(tmsLayer)) i = i + 1 if tmsLayer.isValid(): QgsProject.instance().addMapLayer(tmsLayer, False) self._ol_layers += [tmsLayer] # last added layer is new reference self.setReferenceLayer(tmsLayer) # add to XYT Tiles self.addToXYZTiles(tmsLayerName, xyzUrl, tilePixelRatio) else: uri = "url=" + xyzUrls + "&zmax=18&zmin=0&type=xyz" if (tilePixelRatio > 0): uri = uri + "&tilePixelRatio=" + str(tilePixelRatio) layer = QgsRasterLayer(uri, layerName, provider, QgsRasterLayer.LayerOptions()) layer.setCustomProperty("ol_layer_type", layerName) if layer.isValid(): QgsProject.instance().addMapLayer(layer) self._ol_layers += [layer] # last added layer is new reference self.setReferenceLayer(layer) # add to XYT Tiles self.addToXYZTiles(layerName, xyzUrls, tilePixelRatio) # reload connections to update Browser Panel content self.iface.reloadConnections() return layer, xyzUrls def addToXYZTiles(self, name, url, tilePixelRatio): # store xyz config into qgis settings settings = QSettings() settings.beginGroup("qgis/connections-xyz") settings.setValue("%s/authcfg" % (name), "") settings.setValue("%s/password" % (name), "") settings.setValue("%s/referer" % (name), "") settings.setValue("%s/url" % (name), url) settings.setValue("%s/username" % (name), "") # specify max/min or else only a picture of the map is saved in settings settings.setValue("%s/zmax" % (name), "18") settings.setValue("%s/zmin" % (name), "0") if tilePixelRatio >= 0 and tilePixelRatio <= 2: settings.setValue("%s/tilePixelRatio" % (name), str(tilePixelRatio)) settings.endGroup() def replaceLayer(self, group, oldLayer, newLayer): index = 0 for child in group.children(): if QgsLayerTree.isLayer(child): if child.layerId() == oldLayer.id(): # insert new layer QgsProject.instance().addMapLayer(newLayer, False) newLayerNode = group.insertLayer(index, newLayer) newLayerNode.setVisible(child.isVisible()) # remove old layer QgsProject.instance().removeMapLayer(oldLayer.id()) msg = "Updated layer '%s' from old OpenLayers Plugin version" % newLayer.name( ) self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(0)) QgsMessageLog.logMessage(msg, "OpenLayers Plugin", QgsMessageLog.INFO) # layer replaced return True else: if self.replaceLayer(child, oldLayer, newLayer): # layer replaced in child group return True index += 1 # layer not in this group return False
class OpenlayersPlugin: def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Keep a reference to all OL layers to avoid GC self._ol_layers = [] # initialize locale locale = QSettings().value("locale/userLocale")[0:2] localePath = os.path.join(self.plugin_dir, "i18n", "openlayers_{}.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._olLayerTypeRegistry = WebLayerTypeRegistry(self) self.olOverview = OLOverview(iface, self._olLayerTypeRegistry) self.dlgAbout = AboutDialog() self.pluginLayerRegistry = QgsPluginLayerRegistry() def initGui(self): self._olMenu = QMenu("TMS for Korea") self._olMenu.setIcon(QIcon(":/plugins/openlayers/openlayers.png")) # Overview self.overviewAddAction = QAction( QApplication.translate("OpenlayersPlugin", "OpenLayers Overview"), self.iface.mainWindow()) self.overviewAddAction.setCheckable(True) self.overviewAddAction.setChecked(False) self.overviewAddAction.toggled.connect(self.olOverview.setVisible) self._olMenu.addAction(self.overviewAddAction) self._actionAbout = QAction( QApplication.translate("dlgAbout", "About OpenLayers Plugin"), self.iface.mainWindow()) self._actionAbout.triggered.connect(self.dlgAbout.show) self._olMenu.addAction(self._actionAbout) self.dlgAbout.finished.connect(self._publicationInfoClosed) # Daum self._olLayerTypeRegistry.register(OlDaumStreetLayer()) self._olLayerTypeRegistry.register(OlDaumHybridLayer()) self._olLayerTypeRegistry.register(OlDaumSatelliteLayer()) self._olLayerTypeRegistry.register(OlDaumPhysicalLayer()) self._olLayerTypeRegistry.register(OlDaumCadstralLayer()) # Naver self._olLayerTypeRegistry.register(OlNaverStreetLayer()) self._olLayerTypeRegistry.register(OlNaverHybridLayer()) self._olLayerTypeRegistry.register(OlNaverSatelliteLayer()) self._olLayerTypeRegistry.register(OlNaverPhysicalLayer()) self._olLayerTypeRegistry.register(OlNaverCadastralLayer()) # VWorld self._olLayerTypeRegistry.register(OlVWorldStreetLayer()) self._olLayerTypeRegistry.register(OlVWorldSatelliteLayer()) self._olLayerTypeRegistry.register(OlVWorldGrayLayer()) self._olLayerTypeRegistry.register(OlVWorldHybridLayer()) # NGII self._olLayerTypeRegistry.register(OlNgiiStreetLayer()) self._olLayerTypeRegistry.register(OlNgiiBlankLayer()) self._olLayerTypeRegistry.register(OlNgiiEnglishLayer()) self._olLayerTypeRegistry.register(OlNgiiHighDensityLayer()) self._olLayerTypeRegistry.register(OlNgiiColorBlindLayer()) # Mango #self._olLayerTypeRegistry.register(OlMangoBaseMapLayer()) #self._olLayerTypeRegistry.register(OlMangoBaseMapGrayLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapGrayLayer()) for group in self._olLayerTypeRegistry.groups(): groupMenu = group.menu() for layer in self._olLayerTypeRegistry.groupLayerTypes(group): layer.addMenuEntry(groupMenu, self.iface.mainWindow()) self._olMenu.addMenu(groupMenu) # Create Web menu, if it doesn't exist yet self.iface.addPluginToWebMenu("_tmp", self._actionAbout) self._menu = self.iface.webMenu() self._menu.addMenu(self._olMenu) self.iface.removePluginWebMenu("_tmp", self._actionAbout) # Register plugin layer type self.pluginLayerType = OpenlayersPluginLayerType( self.iface, self.setReferenceLayer, self._olLayerTypeRegistry) self.pluginLayerRegistry.addPluginLayerType(self.pluginLayerType) QgsProject.instance().readProject.connect(self.projectLoaded) QgsProject.instance().projectSaved.connect(self.projectSaved) def unload(self): self.iface.webMenu().removeAction(self._olMenu.menuAction()) self.olOverview.setVisible(False) del self.olOverview # Unregister plugin layer type self.pluginLayerRegistry.removePluginLayerType( OpenlayersLayer.LAYER_TYPE) QgsProject.instance().readProject.disconnect(self.projectLoaded) QgsProject.instance().projectSaved.disconnect(self.projectSaved) def addLayer(self, layerType): if layerType.hasXYZUrl(): # create XYZ layer layer, url = self.createXYZLayer(layerType, layerType.displayName) else: # create OpenlayersLayer layer = OpenlayersLayer(self.iface, self._olLayerTypeRegistry) layer.setName(layerType.displayName) layer.setLayerType(layerType) if layer.isValid(): coordRefSys = layerType.coordRefSys(self.canvasCrs()) self.setMapCrs(coordRefSys) QgsProject.instance().addMapLayer(layer) # store xyz config into qgis settings if layerType.hasXYZUrl(): settings = QSettings() settings.beginGroup('qgis/connections-xyz') settings.setValue("%s/authcfg" % (layer.name()), '') settings.setValue("%s/password" % (layer.name()), '') settings.setValue("%s/referer" % (layer.name()), '') settings.setValue("%s/url" % (layer.name()), url) settings.setValue("%s/username" % (layer.name()), '') # specify max/min or else only a picture of the map is saved # in settings settings.setValue("%s/zmax" % (layer.name()), '18') settings.setValue("%s/zmin" % (layer.name()), '0') settings.endGroup() # reload connections to update Browser Panel content self.iface.reloadConnections() self._ol_layers += [layer] # last added layer is new reference self.setReferenceLayer(layer) if not layerType.hasXYZUrl(): msg = "Printing and rotating of Javascript API " \ "based layers is currently not supported!" self.iface.messageBar().pushMessage("OpenLayers Plugin", msg, level=Qgis.MessageLevel(1), duration=5) def setReferenceLayer(self, layer): self.layer = layer def removeLayer(self, layerId): if self.layer is not None: if self.layer.id() == layerId: self.layer = None # TODO: switch to next available OpenLayers layer? def canvasCrs(self): mapCanvas = self.iface.mapCanvas() crs = mapCanvas.mapSettings().destinationCrs() return crs def setMapCrs(self, targetCRS): mapCanvas = self.iface.mapCanvas() mapExtent = mapCanvas.extent() sourceCRS = self.canvasCrs() QgsProject.instance().setCrs(targetCRS) mapCanvas.freeze(False) try: coordTrans = QgsCoordinateTransform(sourceCRS, targetCRS, QgsProject.instance()) mapExtent = coordTrans.transform( mapExtent, QgsCoordinateTransform.ForwardTransform) mapCanvas.setExtent(mapExtent) except: pass def projectLoaded(self): # replace old OpenlayersLayer with XYZ layer(OL plugin <= 1.3.6) rootGroup = self.iface.layerTreeView().layerTreeModel().rootGroup() for layer in QgsProject.instance().mapLayers().values(): if layer.type( ) == QgsMapLayer.PluginLayer and layer.pluginLayerType( ) == OpenlayersLayer.LAYER_TYPE: if layer.layerType.hasXYZUrl(): # replace layer xyzLayer, url = self.createXYZLayer( layer.layerType, layer.name()) if xyzLayer.isValid(): self.replaceLayer(rootGroup, layer, xyzLayer) def _hasOlLayer(self): for layer in QgsProject.instance().mapLayers().values(): if layer.customProperty('ol_layer_type'): return True return False def _publicationInfo(self): cloud_info_off = QSettings().value("Plugin-OpenLayers/cloud_info_off", defaultValue=False, type=bool) day = 3600 * 24 now = time.time() lastInfo = QSettings().value("Plugin-OpenLayers/cloud_info_ts", defaultValue=0.0, type=float) if lastInfo == 0.0: lastInfo = now - 20 * day # Show first time after 10 days QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", lastInfo) days = (now - lastInfo) / day if days >= 30 and not cloud_info_off: self.dlgAbout.tabWidget.setCurrentWidget( self.dlgAbout.tab_publishing) self.dlgAbout.show() QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", now) def _publicationInfoClosed(self): QSettings().setValue("Plugin-OpenLayers/cloud_info_off", self.dlgAbout.cb_publishing.isChecked()) def projectSaved(self): if self._hasOlLayer(): self._publicationInfo() def createXYZLayer(self, layerType, name): # create XYZ layer with tms url as uri provider = 'wms' url = "type=xyz&url=" + layerType.xyzUrlConfig() layer = QgsRasterLayer(url, name, provider, QgsRasterLayer.LayerOptions()) layer.setCustomProperty('ol_layer_type', layerType.layerTypeName) return layer, layerType.xyzUrlConfig() def replaceLayer(self, group, oldLayer, newLayer): index = 0 for child in group.children(): if QgsLayerTree.isLayer(child): if child.layerId() == oldLayer.id(): # insert new layer QgsProject.instance().addMapLayer(newLayer, False) newLayerNode = group.insertLayer(index, newLayer) newLayerNode.setVisible(child.isVisible()) # remove old layer QgsProject.instance().removeMapLayer(oldLayer.id()) msg = "Updated layer '%s' from old \ OpenLayers Plugin version" % newLayer.name() self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(0)) QgsMessageLog.logMessage(msg, "OpenLayers Plugin", QgsMessageLog.INFO) # layer replaced return True else: if self.replaceLayer(child, oldLayer, newLayer): # layer replaced in child group return True index += 1 # layer not in this group return False
class OpenlayersPlugin: def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Keep a reference to all OL layers to avoid GC self._ol_layers = [] # initialize locale locale = QSettings().value("locale/userLocale")[0:2] localePath = os.path.join(self.plugin_dir, "i18n", "openlayers_{}.qm".format(locale)) if os.path.exists(localePath): self.translator = QTranslator() self.translator.load(localePath) self._olLayerTypeRegistry = WebLayerTypeRegistry(self) self.olOverview = OLOverview(iface, self._olLayerTypeRegistry) self.dlgAbout = AboutDialog() self.pluginLayerRegistry = QgsPluginLayerRegistry() def initGui(self): self._olMenu = QMenu("OpenLayers plugin") self._olMenu.setIcon(QIcon(":/plugins/openlayers/openlayers.png")) # Overview self.overviewAddAction = QAction(QApplication.translate( "OpenlayersPlugin", "OpenLayers Overview"), self.iface.mainWindow()) self.overviewAddAction.setCheckable(True) self.overviewAddAction.setChecked(False) self.overviewAddAction.toggled.connect(self.olOverview.setVisible) self._olMenu.addAction(self.overviewAddAction) self._actionAbout = QAction("Terms of Service / About", self.iface.mainWindow()) self._actionAbout.triggered.connect(self.dlgAbout.show) self._olMenu.addAction(self._actionAbout) self.dlgAbout.finished.connect(self._publicationInfoClosed) self._olLayerTypeRegistry.register(OlGooglePhysicalLayer()) self._olLayerTypeRegistry.register(OlGoogleStreetsLayer()) self._olLayerTypeRegistry.register(OlGoogleHybridLayer()) self._olLayerTypeRegistry.register(OlGoogleSatelliteLayer()) self._olLayerTypeRegistry.register(OlOpenStreetMapLayer()) self._olLayerTypeRegistry.register(OlOpenCycleMapLayer()) self._olLayerTypeRegistry.register(OlOCMLandscapeLayer()) self._olLayerTypeRegistry.register(OlOCMPublicTransportLayer()) # ID 8-10 was Yahoo self._olLayerTypeRegistry.register(OlOSMHumanitarianDataModelLayer()) self._olLayerTypeRegistry.register(OlOCMOutdoorstLayer()) self._olLayerTypeRegistry.register(OlOCMTransportDarkLayer()) self._olLayerTypeRegistry.register(OlBingRoadLayer()) self._olLayerTypeRegistry.register(OlBingAerialLayer()) self._olLayerTypeRegistry.register(OlBingAerialLabelledLayer()) # Order from here on is free. Layers 0-14 should keep order for # compatibility with OL Plugin < 2.3 self._olLayerTypeRegistry.register(OlOCMSpinalMapLayer()) self._olLayerTypeRegistry.register(OlOCMPioneerLayer()) self._olLayerTypeRegistry.register(OlOCMMobileAtlasLayer()) self._olLayerTypeRegistry.register(OlOCMNeighbourhoodLayer()) self._olLayerTypeRegistry.register(OlOSMStamenTonerLayer()) self._olLayerTypeRegistry.register(OlOSMStamenTonerLiteLayer()) self._olLayerTypeRegistry.register(OlOSMStamenWatercolorLayer()) self._olLayerTypeRegistry.register(OlOSMStamenTerrainLayer()) self._olLayerTypeRegistry.register(OlAppleiPhotoMapLayer()) self._olLayerTypeRegistry.register(WikimediaLabelledLayer()) self._olLayerTypeRegistry.register(WikimediaUnLabelledLayer()) for group in self._olLayerTypeRegistry.groups(): groupMenu = group.menu() for layer in self._olLayerTypeRegistry.groupLayerTypes(group): layer.addMenuEntry(groupMenu, self.iface.mainWindow()) self._olMenu.addMenu(groupMenu) # add action for API key dialogs for action in self._olMenu.actions(): if action.text() == "Google Maps": self._actionGoogleMapsApiKey = QAction( "Set API key", self.iface.mainWindow()) self._actionGoogleMapsApiKey.triggered.connect( self.showGoogleMapsApiKeyDialog) action.menu().addAction(self._actionGoogleMapsApiKey) if action.text() == "OSM/Thunderforest": self._actionThunderforestApiKey = QAction( "Set API key", self.iface.mainWindow()) self._actionThunderforestApiKey.triggered.connect( self.showThunderforestApiKeyDialog) action.menu().addAction(self._actionThunderforestApiKey) # Create Web menu, if it doesn't exist yet self.iface.addPluginToWebMenu("_tmp", self._actionAbout) self._menu = self.iface.webMenu() self._menu.addMenu(self._olMenu) self.iface.removePluginWebMenu("_tmp", self._actionAbout) # Register plugin layer type self.pluginLayerType = OpenlayersPluginLayerType( self.iface, self.setReferenceLayer, self._olLayerTypeRegistry) self.pluginLayerRegistry.addPluginLayerType( self.pluginLayerType) QgsProject.instance().readProject.connect(self.projectLoaded) QgsProject.instance().projectSaved.connect(self.projectSaved) def unload(self): self.iface.webMenu().removeAction(self._olMenu.menuAction()) self.olOverview.setVisible(False) del self.olOverview # Unregister plugin layer type self.pluginLayerRegistry.removePluginLayerType( OpenlayersLayer.LAYER_TYPE) QgsProject.instance().readProject.disconnect(self.projectLoaded) QgsProject.instance().projectSaved.disconnect(self.projectSaved) def addLayer(self, layerType): if layerType.hasXYZUrl(): # create XYZ layer layer, url = self.createXYZLayer(layerType, layerType.displayName) else: # create OpenlayersLayer layer = OpenlayersLayer(self.iface, self._olLayerTypeRegistry) layer.setName(layerType.displayName) layer.setLayerType(layerType) if layer.isValid(): coordRefSys = layerType.coordRefSys(self.canvasCrs()) self.setMapCrs(coordRefSys) QgsProject.instance().addMapLayer(layer) # store xyz config into qgis settings if layerType.hasXYZUrl(): settings = QSettings() settings.beginGroup('qgis/connections-xyz') settings.setValue("%s/authcfg" % (layer.name()), '') settings.setValue("%s/password" % (layer.name()), '') settings.setValue("%s/referer" % (layer.name()), '') settings.setValue("%s/url" % (layer.name()), url) settings.setValue("%s/username" % (layer.name()), '') # specify max/min or else only a picture of the map is saved # in settings settings.setValue("%s/zmax" % (layer.name()), '18') settings.setValue("%s/zmin" % (layer.name()), '0') settings.endGroup() # reload connections to update Browser Panel content self.iface.reloadConnections() self._ol_layers += [layer] # last added layer is new reference self.setReferenceLayer(layer) if not layerType.hasXYZUrl(): msg = "Printing and rotating of Javascript API " \ "based layers is currently not supported!" self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(1), duration=5) def setReferenceLayer(self, layer): self.layer = layer def removeLayer(self, layerId): if self.layer is not None: if self.layer.id() == layerId: self.layer = None # TODO: switch to next available OpenLayers layer? def canvasCrs(self): mapCanvas = self.iface.mapCanvas() crs = mapCanvas.mapSettings().destinationCrs() return crs def setMapCrs(self, coordRefSys): mapCanvas = self.iface.mapCanvas() # On the fly canvasCrs = self.canvasCrs() if canvasCrs != coordRefSys: coordTrans = QgsCoordinateTransform(canvasCrs, coordRefSys, QgsProject.instance()) extMap = mapCanvas.extent() extMap = coordTrans.transform( extMap, QgsCoordinateTransform.ForwardTransform) mapCanvas.setDestinationCrs(coordRefSys) mapCanvas.freeze(False) mapCanvas.setExtent(extMap) def projectLoaded(self): # replace old OpenlayersLayer with XYZ layer(OL plugin <= 1.3.6) rootGroup = self.iface.layerTreeView().layerTreeModel().rootGroup() for layer in QgsProject.instance().mapLayers().values(): if layer.type() == QgsMapLayer.PluginLayer and layer.pluginLayerType() == OpenlayersLayer.LAYER_TYPE: if layer.layerType.hasXYZUrl(): # replace layer xyzLayer, url = self.createXYZLayer(layer.layerType, layer.name()) if xyzLayer.isValid(): self.replaceLayer(rootGroup, layer, xyzLayer) def _hasOlLayer(self): for layer in QgsProject.instance().mapLayers().values(): if layer.customProperty('ol_layer_type'): return True return False def _publicationInfo(self): cloud_info_off = QSettings().value("Plugin-OpenLayers/cloud_info_off", defaultValue=False, type=bool) day = 3600*24 now = time.time() lastInfo = QSettings().value("Plugin-OpenLayers/cloud_info_ts", defaultValue=0.0, type=float) if lastInfo == 0.0: lastInfo = now-20*day # Show first time after 10 days QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", lastInfo) days = (now-lastInfo)/day if days >= 30 and not cloud_info_off: self.dlgAbout.tabWidget.setCurrentWidget( self.dlgAbout.tab_publishing) self.dlgAbout.show() QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", now) def _publicationInfoClosed(self): QSettings().setValue("Plugin-OpenLayers/cloud_info_off", self.dlgAbout.cb_publishing.isChecked()) def projectSaved(self): if self._hasOlLayer(): self._publicationInfo() def createXYZLayer(self, layerType, name): # create XYZ layer with tms url as uri provider = 'wms' url = "type=xyz&url=" + layerType.xyzUrlConfig() layer = QgsRasterLayer(url, name, provider, QgsRasterLayer.LayerOptions()) layer.setCustomProperty('ol_layer_type', layerType.layerTypeName) return layer, layerType.xyzUrlConfig() def replaceLayer(self, group, oldLayer, newLayer): index = 0 for child in group.children(): if QgsLayerTree.isLayer(child): if child.layerId() == oldLayer.id(): # insert new layer QgsProject.instance().addMapLayer(newLayer, False) newLayerNode = group.insertLayer(index, newLayer) newLayerNode.setVisible(child.isVisible()) # remove old layer QgsProject.instance().removeMapLayer( oldLayer.id()) msg = "Updated layer '%s' from old \ OpenLayers Plugin version" % newLayer.name() self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(0)) QgsMessageLog.logMessage( msg, "OpenLayers Plugin", QgsMessageLog.INFO) # layer replaced return True else: if self.replaceLayer(child, oldLayer, newLayer): # layer replaced in child group return True index += 1 # layer not in this group return False def showGoogleMapsApiKeyDialog(self): apiKey = QSettings().value("Plugin-OpenLayers/googleMapsApiKey") newApiKey, ok = QInputDialog.getText( self.iface.mainWindow(), "API key", "Enter your Google Maps API key", QLineEdit.Normal, apiKey) if ok: QSettings().setValue("Plugin-OpenLayers/googleMapsApiKey", newApiKey) def showThunderforestApiKeyDialog(self): apiKey = QSettings().value("Plugin-OpenLayers/thunderforestApiKey") newApiKey, ok = QInputDialog.getText( self.iface.mainWindow(), "API key", "Enter your API key (<a href=\"https://thunderforest.com/pricing/\">https://thunderforest.com</a>)", QLineEdit.Normal, apiKey) if ok: QSettings().setValue("Plugin-OpenLayers/thunderforestApiKey", newApiKey)
def initGui(self): #import pydevd #pydevd.settrace('localhost', port=9921, stdoutToServer=True, stderrToServer=True, suspend=False) """Create the menu entries and toolbar icons inside the QGIS GUI.""" # Main Menu icon_path = self.plugin_dir + '/icons/mActionAddLayer.png' self.menu = QMenu(self.tr(u'QuickMapServices')) self.menu.setIcon(QIcon(icon_path)) # DataSources Actions # Register plugin layer type self.tileLayerType = TileLayerType(self) QgsPluginLayerRegistry.instance().addPluginLayerType( self.tileLayerType) self.groups_list = DsGroupsList(self.locale, self.custom_translator) self.ds_list = DataSourcesList(self.locale, self.custom_translator) data_sources = self.ds_list.data_sources.values() data_sources.sort(key=lambda x: x.alias or x.id) for ds in data_sources: ds.action.triggered.connect(self.insert_layer) gr_menu = self.groups_list.get_group_menu(ds.group) gr_menu.addAction(ds.action) if gr_menu not in self.menu.children(): self.menu.addMenu(gr_menu) # Scales, Settings and About actions icon_set_nearest_scale_path = self.plugin_dir + '/icons/mActionSettings.png' # TODO change icon set_nearest_scale_act = QAction(QIcon(icon_set_nearest_scale_path), self.tr('Set proper scale'), self.iface.mainWindow()) set_nearest_scale_act.triggered.connect(self.set_nearest_scale) self.menu.addAction(set_nearest_scale_act) # TODO: uncomment after fix self.service_actions.append(set_nearest_scale_act) icon_scales_path = self.plugin_dir + '/icons/mActionSettings.png' # TODO change icon scales_act = QAction(QIcon(icon_scales_path), self.tr('Set SlippyMap scales'), self.iface.mainWindow()) scales_act.triggered.connect(self.set_tms_scales) #self.menu.addAction(scales_act) # TODO: uncomment after fix self.service_actions.append(scales_act) icon_settings_path = self.plugin_dir + '/icons/mActionSettings.png' settings_act = QAction(QIcon(icon_settings_path), self.tr('Settings'), self.iface.mainWindow()) self.service_actions.append(settings_act) #self.menu.addAction(settings_act) icon_about_path = self.plugin_dir + '/icons/mActionAbout.png' info_act = QAction(QIcon(icon_about_path), self.tr('About'), self.iface.mainWindow()) self.service_actions.append(info_act) info_act.triggered.connect(self.info_dlg.show) self.menu.addAction(info_act) # add to QGIS menu self.iface.addPluginToWebMenu("_tmp", info_act) self.iface.webMenu().addMenu(self.menu) self.iface.removePluginWebMenu("_tmp", info_act) # add to QGIS toolbar toolbutton = QToolButton() toolbutton.setPopupMode(QToolButton.InstantPopup) toolbutton.setMenu(self.menu) toolbutton.setIcon(self.menu.icon()) toolbutton.setText(self.menu.title()) toolbutton.setToolTip(self.menu.title()) self.tb_action = self.iface.webToolBar().addWidget(toolbutton)
class OpenlayersPlugin: def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface # initialize plugin directory self.plugin_dir = os.path.dirname(__file__) # Keep a reference to all OL layers to avoid GC self._ol_layers = [] # initialize locale locale = QSettings().value("locale/userLocale")[0:2] localePath = os.path.join(self.plugin_dir, "i18n", "openlayers_{}.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._olLayerTypeRegistry = WebLayerTypeRegistry(self) self.olOverview = OLOverview(iface, self._olLayerTypeRegistry) self.dlgAbout = AboutDialog() self.pluginLayerRegistry = QgsPluginLayerRegistry() def initGui(self): self._olMenu = QMenu("TMS for Korea") self._olMenu.setIcon(QIcon(":/plugins/openlayers/openlayers.png")) # Overview self.overviewAddAction = QAction(QApplication.translate("OpenlayersPlugin", "OpenLayers Overview"), self.iface.mainWindow()) self.overviewAddAction.setCheckable(True) self.overviewAddAction.setChecked(False) self.overviewAddAction.toggled.connect(self.olOverview.setVisible) self._olMenu.addAction(self.overviewAddAction) self._actionAbout = QAction(QApplication.translate("dlgAbout", "About OpenLayers Plugin"), self.iface.mainWindow()) self._actionAbout.triggered.connect(self.dlgAbout.show) self._olMenu.addAction(self._actionAbout) self.dlgAbout.finished.connect(self._publicationInfoClosed) # Daum self._olLayerTypeRegistry.register(OlDaumStreetLayer()) self._olLayerTypeRegistry.register(OlDaumHybridLayer()) self._olLayerTypeRegistry.register(OlDaumSatelliteLayer()) self._olLayerTypeRegistry.register(OlDaumPhysicalLayer()) self._olLayerTypeRegistry.register(OlDaumCadstralLayer()) # Naver self._olLayerTypeRegistry.register(OlNaverStreetLayer()) self._olLayerTypeRegistry.register(OlNaverHybridLayer()) self._olLayerTypeRegistry.register(OlNaverSatelliteLayer()) self._olLayerTypeRegistry.register(OlNaverPhysicalLayer()) self._olLayerTypeRegistry.register(OlNaverCadastralLayer()) # VWorld self._olLayerTypeRegistry.register(OlVWorldStreetLayer()) self._olLayerTypeRegistry.register(OlVWorldSatelliteLayer()) self._olLayerTypeRegistry.register(OlVWorldGrayLayer()) self._olLayerTypeRegistry.register(OlVWorldHybridLayer()) # NGII self._olLayerTypeRegistry.register(OlNgiiStreetLayer()) self._olLayerTypeRegistry.register(OlNgiiBlankLayer()) self._olLayerTypeRegistry.register(OlNgiiEnglishLayer()) self._olLayerTypeRegistry.register(OlNgiiHighDensityLayer()) self._olLayerTypeRegistry.register(OlNgiiColorBlindLayer()) # Mango #self._olLayerTypeRegistry.register(OlMangoBaseMapLayer()) #self._olLayerTypeRegistry.register(OlMangoBaseMapGrayLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapLayer()) #self._olLayerTypeRegistry.register(OlMangoHiDPIMapGrayLayer()) for group in self._olLayerTypeRegistry.groups(): groupMenu = group.menu() for layer in self._olLayerTypeRegistry.groupLayerTypes(group): layer.addMenuEntry(groupMenu, self.iface.mainWindow()) self._olMenu.addMenu(groupMenu) # Create Web menu, if it doesn't exist yet self.iface.addPluginToWebMenu("_tmp", self._actionAbout) self._menu = self.iface.webMenu() self._menu.addMenu(self._olMenu) self.iface.removePluginWebMenu("_tmp", self._actionAbout) # Register plugin layer type self.pluginLayerType = OpenlayersPluginLayerType( self.iface, self.setReferenceLayer, self._olLayerTypeRegistry) self.pluginLayerRegistry.addPluginLayerType( self.pluginLayerType) QgsProject.instance().readProject.connect(self.projectLoaded) QgsProject.instance().projectSaved.connect(self.projectSaved) def unload(self): self.iface.webMenu().removeAction(self._olMenu.menuAction()) self.olOverview.setVisible(False) del self.olOverview # Unregister plugin layer type self.pluginLayerRegistry.removePluginLayerType( OpenlayersLayer.LAYER_TYPE) QgsProject.instance().readProject.disconnect(self.projectLoaded) QgsProject.instance().projectSaved.disconnect(self.projectSaved) def addLayer(self, layerType): if layerType.hasXYZUrl(): # create XYZ layer layer, url = self.createXYZLayer(layerType, layerType.displayName) else: # create OpenlayersLayer layer = OpenlayersLayer(self.iface, self._olLayerTypeRegistry) layer.setName(layerType.displayName) layer.setLayerType(layerType) if layer.isValid(): coordRefSys = layerType.coordRefSys(self.canvasCrs()) self.setMapCrs(coordRefSys) QgsProject.instance().addMapLayer(layer) # store xyz config into qgis settings if layerType.hasXYZUrl(): settings = QSettings() settings.beginGroup('qgis/connections-xyz') settings.setValue("%s/authcfg" % (layer.name()), '') settings.setValue("%s/password" % (layer.name()), '') settings.setValue("%s/referer" % (layer.name()), '') settings.setValue("%s/url" % (layer.name()), url) settings.setValue("%s/username" % (layer.name()), '') # specify max/min or else only a picture of the map is saved # in settings settings.setValue("%s/zmax" % (layer.name()), '18') settings.setValue("%s/zmin" % (layer.name()), '0') settings.endGroup() # reload connections to update Browser Panel content self.iface.reloadConnections() self._ol_layers += [layer] # last added layer is new reference self.setReferenceLayer(layer) if not layerType.hasXYZUrl(): msg = "Printing and rotating of Javascript API " \ "based layers is currently not supported!" self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(1), duration=5) def setReferenceLayer(self, layer): self.layer = layer def removeLayer(self, layerId): if self.layer is not None: if self.layer.id() == layerId: self.layer = None # TODO: switch to next available OpenLayers layer? def canvasCrs(self): mapCanvas = self.iface.mapCanvas() crs = mapCanvas.mapSettings().destinationCrs() return crs def setMapCrs(self, targetCRS): mapCanvas = self.iface.mapCanvas() mapExtent = mapCanvas.extent() sourceCRS = self.canvasCrs() QgsProject.instance().setCrs(targetCRS) mapCanvas.freeze(False) try: coordTrans = QgsCoordinateTransform(sourceCRS, targetCRS, QgsProject.instance()) mapExtent = coordTrans.transform(mapExtent, QgsCoordinateTransform.ForwardTransform) mapCanvas.setExtent(mapExtent) except: pass def projectLoaded(self): # replace old OpenlayersLayer with XYZ layer(OL plugin <= 1.3.6) rootGroup = self.iface.layerTreeView().layerTreeModel().rootGroup() for layer in QgsProject.instance().mapLayers().values(): if layer.type() == QgsMapLayer.PluginLayer and layer.pluginLayerType() == OpenlayersLayer.LAYER_TYPE: if layer.layerType.hasXYZUrl(): # replace layer xyzLayer, url = self.createXYZLayer(layer.layerType, layer.name()) if xyzLayer.isValid(): self.replaceLayer(rootGroup, layer, xyzLayer) def _hasOlLayer(self): for layer in QgsProject.instance().mapLayers().values(): if layer.customProperty('ol_layer_type'): return True return False def _publicationInfo(self): cloud_info_off = QSettings().value("Plugin-OpenLayers/cloud_info_off", defaultValue=False, type=bool) day = 3600*24 now = time.time() lastInfo = QSettings().value("Plugin-OpenLayers/cloud_info_ts", defaultValue=0.0, type=float) if lastInfo == 0.0: lastInfo = now-20*day # Show first time after 10 days QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", lastInfo) days = (now-lastInfo)/day if days >= 30 and not cloud_info_off: self.dlgAbout.tabWidget.setCurrentWidget( self.dlgAbout.tab_publishing) self.dlgAbout.show() QSettings().setValue("Plugin-OpenLayers/cloud_info_ts", now) def _publicationInfoClosed(self): QSettings().setValue("Plugin-OpenLayers/cloud_info_off", self.dlgAbout.cb_publishing.isChecked()) def projectSaved(self): if self._hasOlLayer(): self._publicationInfo() def createXYZLayer(self, layerType, name): # create XYZ layer with tms url as uri provider = 'wms' url = "type=xyz&url=" + layerType.xyzUrlConfig() layer = QgsRasterLayer(url, name, provider, QgsRasterLayer.LayerOptions()) layer.setCustomProperty('ol_layer_type', layerType.layerTypeName) return layer, layerType.xyzUrlConfig() def replaceLayer(self, group, oldLayer, newLayer): index = 0 for child in group.children(): if QgsLayerTree.isLayer(child): if child.layerId() == oldLayer.id(): # insert new layer QgsProject.instance().addMapLayer(newLayer, False) newLayerNode = group.insertLayer(index, newLayer) newLayerNode.setVisible(child.isVisible()) # remove old layer QgsProject.instance().removeMapLayer( oldLayer.id()) msg = "Updated layer '%s' from old \ OpenLayers Plugin version" % newLayer.name() self.iface.messageBar().pushMessage( "OpenLayers Plugin", msg, level=Qgis.MessageLevel(0)) QgsMessageLog.logMessage( msg, "OpenLayers Plugin", QgsMessageLog.INFO) # layer replaced return True else: if self.replaceLayer(child, oldLayer, newLayer): # layer replaced in child group return True index += 1 # layer not in this group return False