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]) addTrackedLayer(qgslayer, self.catalog.service_url) if not ok: raise Exception( "Layer was added, but style could not be set (maybe GeoServer layer is missing default style)" )
def publishLayer(self, layer, workspace=None, overwrite=True, name=None, style=None): ''' Publishes a QGIS layer. It creates the corresponding store and the layer itself. If a pre-upload hook is set, its runs it and publishes the resulting layer layer: the layer to publish, whether as a QgsMapLayer object or its name in the QGIS TOC. workspace: the workspace to publish to. USes the default workspace if not passed or None name: the name for the published layer. Uses the QGIS layer name if not passed or None style: the style to use from the ones in the catalog. Will upload the QGIS style if not passed or None ''' if isinstance(layer, basestring): layer = layers.resolveLayer(layer) addTrackedLayer(layer, self.catalog.service_url) name = xmlNameFixUp(name) if name is not None \ else xmlNameFixUp(layer.name()) gslayer = self.catalog.get_layer(name) if gslayer is not None and not overwrite: return sld = self.publishStyle(layer, overwrite, name) if style is None else None layer = self.preprocess(layer) self.upload(layer, workspace, overwrite, name) if sld is not None or style is not None: #assign style to created store publishing = self.catalog.get_layer(name) publishing.default_style = style or self.catalog.get_style(name) self.catalog.save(publishing)
def publishLayer (self, layer, workspace=None, overwrite=True, name=None, style=None): ''' Publishes a QGIS layer. It creates the corresponding store and the layer itself. layer: the layer to publish, whether as a QgsMapLayer object or its name in the QGIS TOC. workspace: the workspace to publish to. USes the default workspace if not passed or None name: the name for the published layer. Uses the QGIS layer name if not passed or None style: the style to use from the ones in the catalog. Will upload the QGIS style if not passed or None ''' if isinstance(layer, str): layer = layers.resolveLayer(layer) addTrackedLayer(layer, self.catalog.service_url) name = xmlNameFixUp(name) if name is not None \ else xmlNameFixUp(layer.name()) gslayer = self.catalog.get_layer(name) if gslayer is not None and not overwrite: return sld = self.publishStyle(layer, overwrite, name) if style is None else None self.upload(layer, workspace, overwrite, name) if sld is not None or style is not None: #assign style to created store publishing = self.catalog.get_layer(name) publishing.default_style = style or self.catalog.get_styles(name)[0] self.catalog.save(publishing)
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) QgsNetworkAccessManager.instance().cache().clear() 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.decode() sld = adaptGsToQgs(str(sld)) sldfile = tempFilename("sld") with open(sldfile, 'w') as f: f.write(sld) msg, ok = qgslayer.loadSldStyle(sldfile) except Exception as e: ok = False QgsProject.instance().addMapLayers([qgslayer]) setUnits(qgslayer) addTrackedLayer(qgslayer, self.catalog.service_url) if not ok: raise Exception ("Layer was added, but style could not be set (maybe GeoServer layer is missing default style)") elif resource.resource_type == "coverage": qgslayer = QgsRasterLayer(uri, destName or resource.title, "wcs" ) if not qgslayer.isValid(): raise Exception ("Layer at %s is not a valid layer" % uri) QgsProject.instance().addMapLayers([qgslayer]) addTrackedLayer(qgslayer, self.catalog.service_url) elif resource.resource_type == "wmsLayer": qgslayer = QgsRasterLayer(uri, destName or resource.title, "wms") if not qgslayer.isValid(): raise Exception ("Layer at %s is not a valid layer" % uri) QgsProject.instance().addMapLayers([qgslayer]) addTrackedLayer(qgslayer, self.catalog.service_url) else: raise Exception("Cannot add layer. Unsupported layer type.")
f.write(sld) msg, ok = qgslayer.loadSldStyle(sldfile) except Exception, e: ok = False QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) addTrackedLayer(qgslayer, self.catalog.service_url) if not ok: raise Exception( "Layer was added, but style could not be set (maybe GeoServer layer is missing default style)" ) elif resource.resource_type == "coverage": qgslayer = QgsRasterLayer(uri, destName or resource.title, "wcs") if not qgslayer.isValid(): raise Exception("Layer at %s is not a valid layer" % uri) QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) addTrackedLayer(qgslayer, self.catalog.service_url) elif resource.resource_type == "wmsLayer": qgslayer = QgsRasterLayer(uri, destName or resource.title, "wms") if not qgslayer.isValid(): raise Exception("Layer at %s is not a valid layer" % uri) QgsMapLayerRegistry.instance().addMapLayers([qgslayer]) addTrackedLayer(qgslayer, self.catalog.service_url) else: raise Exception("Cannot add layer. Unsupported layer type.") def addGroupToProject(self, name): group = self.catalog.get_layergroup(name) if group is None: raise Exception("A layer with the name '" + name + "' was not found in the catalog")