def toMapLayer(self): from qgis.core import QgsRasterLayer, QgsContrastEnhancement, QgsDataSourceUri, QgsCredentials rl = QgsRasterLayer(self.gdalUri(), self.name) if not rl.isValid(): err = rl.error().summary() uri = QgsDataSourceUri(self.database().uri()) conninfo = uri.connectionInfo(False) username = uri.username() password = uri.password() for i in range(3): (ok, username, password) = QgsCredentials.instance().get( conninfo, username, password, err) if ok: uri.setUsername(username) uri.setPassword(password) rl = QgsRasterLayer(self.gdalUri(uri), self.name) if rl.isValid(): break if rl.isValid(): rl.setContrastEnhancement( QgsContrastEnhancement.StretchToMinimumMaximum) return rl
def special_search(self, easter_code="isogeo"): """Make some special actions in certains cases. :param str easter_code: easter egg label. Available values: * isogeo: display Isogeo logo and zoom in our office location * picasa: change QGS project title """ canvas = iface.mapCanvas() if easter_code == "isogeo": # WMS wms_params = {"service": "WMS", "version": "1.3.0", "request": "GetMap", "layers": "Isogeo:isogeo_logo", "crs": "EPSG:3857", "format": "image/png", "styles": "isogeo_logo", "url": "http://noisy.hq.isogeo.fr:6090/geoserver/Isogeo/ows?" } wms_uri = unquote(urlencode(wms_params)) wms_lyr = QgsRasterLayer(wms_uri, u"Ici c'est Isogeo !", "wms") if wms_lyr.isValid: QgsMapLayerRegistry.instance().addMapLayer(wms_lyr) logger.info("Isogeo easter egg used and WMS displayed!") else: logger.error("WMS layer failed: {}" .format(wms_lyr.error().message())) # WFS uri = QgsDataSourceURI() uri.setParam("url", "http://noisy.hq.isogeo.fr:6090/geoserver/Isogeo/ows?") uri.setParam("service", "WFS") uri.setParam("version", "1.1.0") uri.setParam("typename", "Isogeo:isogeo_logo") uri.setParam("srsname", "EPSG:3857") uri.setParam("restrictToRequestBBOX", "0") wfs_uri = uri.uri() wfs_lyr = QgsVectorLayer(wfs_uri, u"Ici c'est Isogeo !", "WFS") if wfs_lyr.isValid: wfs_style = path.join(path.dirname(path.realpath(__file__)), "isogeo.qml") wfs_lyr.loadNamedStyle(wfs_style) QgsMapLayerRegistry.instance().addMapLayer(wfs_lyr) canvas.setExtent(wfs_lyr.extent()) logger.debug("Isogeo easter egg used") else: logger.error("Esater egg - WFS layer failed: {}" .format(wfs_lyr.error().message())) elif easter_code == "picasa": project = QgsProject.instance() project.setTitle(u"Isogeo, le Picasa de l'information géographique") logger.debug("Picasa easter egg used") else: pass # ending method return
def toMapLayer(self): from qgis.core import QgsRasterLayer, QgsContrastEnhancement, QgsDataSourceURI, QgsCredentials rl = QgsRasterLayer(self.gdalUri(), self.name) if not rl.isValid(): err = rl.error().summary() uri = QgsDataSourceURI(self.database().uri()) conninfo = uri.connectionInfo(False) username = uri.username() password = uri.password() for i in range(3): (ok, username, password) = QgsCredentials.instance().get(conninfo, username, password, err) if ok: uri.setUsername(username) uri.setPassword(password) rl = QgsRasterLayer(self.gdalUri(uri), self.name) if rl.isValid(): break if rl.isValid(): rl.setContrastEnhancement(QgsContrastEnhancement.StretchToMinimumMaximum) return rl
uri_dpi = "dpiMode=7" uri_style = "style=" # URI construction wms_url_complete = uri_url + uri_separator.join( (uri_service, uri_version, uri_request, uri_username, uri_password, uri_format, uri_layers, uri_crs, uri_style, uri_dpi)) print(wms_url_complete) rlayer = QgsRasterLayer(wms_url_complete, 'norther', 'wms') if rlayer.isValid(): pass else: print(rlayer.error().message()) QgsMapLayerRegistry.instance().addMapLayer(rlayer) ### With QgsDatSourceURI uri = QgsDataSourceURI() uri.setParam('url', 'http://localhost:8080/geoserver/wms?') uri.setParam("layers", "ESIPE_IG3:Camera_WGS84") uri.setParam("service", "WMS") uri.setParam("request", "GetMap") uri.setParam("version", "version%3D1.3.0%26") uri_layer = QgsRasterLayer(str(uri.encodedUri()), 'WMSlayer', 'wms') if uri_layer.isValid(): pass
def addButtonPressed(self): a = self.dlg.layerTree.selectedIndexes() lyr = a[0].data(Qt.UserRole) if lyr is None: log("lyr is none") return #get threshold value = self.dlg.threshold1_value.value() args = lyr.wmtsArgs() email = None password = None if self.settings.contains("password"): password = self.settings.value("password") else: log("Error adding layer: no password.") return if self.settings.contains("email"): email = self.settings.value("email") else: log("Error adding layer: no email.") return args['login'] = email args['request'] = 'GetCapabilities' args['password'] = password #parse the crs from urn:ogc:def:crs:EPSG::3857 to EPSG:3857 qcrs = QgsCoordinateReferenceSystem() qcrs.createFromOgcWmsCrs(lyr.crs) crs = qcrs.authid() log("QGIS CRS is %s" % crs) url = self.wmtsGetCapabilitiesUrl() if url is None: log("AddButtonPressed: No url.") return #TODO, ideally extraTileArgs should be added here but backend cannot handle them whey they are encoded. # url.addQueryItem("extraTileArgs","threshold=%s"%value) qgisArgs = { 'contextualWMSLegend': 0, 'crs': crs, 'dpimode': 7, 'format': args['format'], 'layers': args['layer'], 'styles': args['style'], 'tileMatrixSet': args['tileMatrixSet'], 'url': unicode(url.toEncoded(), "utf-8") + u'&extraTileArgs=threshold=%g' % value } log("QgsRasterLayer: %s" % qgisArgs) qgisURI = urllib.urlencode(qgisArgs) if (len(self.wmts.tiletypes[lyr.contentType].thresholds) > 0): tt = self.wmts.tiletypes[lyr.contentType] name = "%s%s - %s" % (value, tt.unit, lyr.simpleTitle) else: name = lyr.simpleTitle qgis_wmts_lyr_manual = QgsRasterLayer(qgisURI, name, 'wms') if qgis_wmts_lyr_manual.isValid(): QgsMapLayerRegistry.instance().addMapLayer(qgis_wmts_lyr_manual) else: log(qgis_wmts_lyr_manual.error().message()) QMessageBox.warning( self.dlg, "SCALGO Live Plugin", "Error adding layer %s" % qgis_wmts_lyr_manual.error().message(), QMessageBox.Close, QMessageBox.Close)