def addLayerToProject(self, name, destName = None): ''' Adds a new layer to the current project based on a layer in a GeoServer catalog It will create a new layer with a WFS or WCS connection, pointing to the specified GeoServer layer. In the case of a vector layer, it will also fetch its associated style and set it as the current style for the created QGIS layer ''' layer = self.catalog.get_layer(name) if layer is None: raise Exception ("A layer with the name '" + name + "' was not found in the catalog") resource = layer.resource uri = uri_utils.layerUri(layer) if resource.resource_type == "featureType": qgslayer = QgsVectorLayer(uri, destName or resource.title, "WFS") if not qgslayer.isValid(): raise Exception ("Layer at %s is not a valid layer" % uri) ok = True try: sld = layer.default_style.sld_body sld = adaptGsToQgs(sld) sldfile = utils.tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgslayer.loadSldStyle(sldfile) except Exception, e: ok = False QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) if not ok: raise Exception ("Layer was added, but style could not be set (maybe GeoServer layer is missing default style)")
def addLayerToProject(self, name, destName=None): ''' Adds a new layer to the current project based on a layer in a GeoServer catalog It will create a new layer with a WFS or WCS connection, pointing to the specified GeoServer layer. In the case of a vector layer, it will also fetch its associated style and set it as the current style for the created QGIS layer ''' layer = self.catalog.get_layer(name) if layer is None: raise Exception("A layer with the name '" + name + "' was not found in the catalog") resource = layer.resource uri = uri_utils.layerUri(layer) if resource.resource_type == "featureType": qgslayer = QgsVectorLayer(uri, destName or resource.title, "WFS") if not qgslayer.isValid(): raise Exception("Layer at %s is not a valid layer" % uri) ok = True try: sld = layer.default_style.sld_body sld = adaptGsToQgs(sld) sldfile = utils.tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgslayer.loadSldStyle(sldfile) except Exception, e: ok = False QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) if not ok: raise Exception( "Layer was added, but style could not be set (maybe GeoServer layer is missing default style)" )
def layerAdded(qgislayer): try: qgislayer.styleChanged.connect(partial(updatePublishedStyle, qgislayer)) except: #styleChanged only available for QGIS >2.16 pass try: if qgislayer.providerType().lower() != "wfs": return except: pass #Not all layers have a providerType method catalogs = _explorer.explorerTree.gsItem._catalogs.values() for cat in catalogs: if cat.gs_base_url in qgislayer.source(): for layer in cat.get_layers(): uri = uri_utils.layerUri(layer) if uri == qgislayer.source(): try: sld = layer.default_style.sld_body sld = adaptGsToQgs(sld) sldfile = tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgislayer.loadSldStyle(sldfile) if not ok: raise Exception( "Could not load style for layer <b>%s</b>" % qgislayer.name()) except Exception, e: _explorer.setWarning( "Could not set style for layer <b>%s</b>" % qgislayer.name()) break
def layerAdded(qgislayer): if qgislayer.providerType().lower() != "wfs": return catalogs = _explorer.explorerTree.gsItem._catalogs.values() for cat in catalogs: if cat.gs_base_url in qgislayer.source(): for layer in cat.get_layers(): uri = uri_utils.layerUri(layer) if uri == qgislayer.source(): try: sld = layer.default_style.sld_body sld = adaptGsToQgs(sld) sldfile = tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgislayer.loadSldStyle(sldfile) if not ok: raise Exception("Could not set style for layer") except Exception, e: _explorer.setWarning("Could not set style for layer") break
def layerAdded(qgislayer): try: if qgislayer.providerType().lower() != "wfs": return except: pass #Not all layers have a providerType method catalogs = _explorer.explorerTree.gsItem._catalogs.values() for cat in catalogs: if cat.gs_base_url in qgislayer.source(): for layer in cat.get_layers(): uri = uri_utils.layerUri(layer) if uri == qgislayer.source(): try: sld = layer.default_style.sld_body sld = adaptGsToQgs(sld) sldfile = tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgislayer.loadSldStyle(sldfile) if not ok: raise Exception("Could not set style for layer") except Exception, e: _explorer.setWarning("Could not set style for layer") break