def testSymbolSizeAfterReload(self): # create a layer layer = createLayerWithOnePoint() # load a sld with marker size sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld' mFilePath = os.path.join(TEST_DATA_DIR, sld) layer.loadSldStyle(mFilePath) # get the size and unit of the symbol sl = layer.renderer().symbol().symbolLayers()[0] first_size = sl.size() first_unit = sl.outputUnit() # in pixels # export sld into a qdomdocument with namespace processing activated doc = QDomDocument() msg = "" layer.exportSldStyle(doc, msg) doc.setContent(doc.toString(), True) self.assertTrue(msg == "") # reload the same sld root = doc.firstChildElement("StyledLayerDescriptor") el = root.firstChildElement("NamedLayer") layer.readSld(el, msg) # extract the size and unit of symbol sl = layer.renderer().symbol().symbolLayers()[0] second_size = sl.size() second_unit = sl.outputUnit() # size and unit should be the same after export and reload the same # sld description self.assertEqual(first_size, second_size) self.assertEqual(first_unit, second_unit)
def testSymbolSizeAfterReload(self): # create a layer layer = createLayerWithOnePoint() # load a sld with marker size sld = 'symbol_layer/QgsSvgMarkerSymbolLayer.sld' mFilePath = os.path.join(TEST_DATA_DIR, sld) layer.loadSldStyle(mFilePath) # get the size and unit of the symbol sl = layer.renderer().symbol().symbolLayers()[0] first_size = sl.size() first_unit = sl.outputUnit() # in pixels # export sld into a qdomdocument with namespace processing activated doc = QDomDocument() msg = "" layer.exportSldStyle(doc, msg) doc.setContent(doc.toString(), True) self.assertTrue(msg == "") # reload the same sld root = doc.firstChildElement("StyledLayerDescriptor") el = root.firstChildElement("NamedLayer") layer.readSld(el, msg) # extract the size and unit of symbol sl = layer.renderer().symbol().symbolLayers()[0] second_size = sl.size() second_unit = sl.outputUnit() # size and unit should be the same after export and reload the same # sld description self.assertEqual(first_size, second_size) self.assertEqual(first_unit, second_unit)
def set_layer_symbology(layer, symbology): if symbology is not None: doc = QDomDocument("qgis") doc.setContent(symbology) errorMsg = "" ctx = QgsReadWriteContext() layer.readSymbology(doc.firstChildElement("qgis"), errorMsg, ctx)
def read_from_model(self, setting_instance): """Read settings from qplotly settings model instance""" document = QDomDocument() if document.setContent(setting_instance.xml): if self.read_xml(document.firstChildElement()): return True return False
def runProcessingModel(self, parametersData): doc = QDomDocument() doc.setContent(parametersData['model_xml']) model = core.QgsProcessingModelAlgorithm() model.loadVariant(core.QgsXmlUtils.readVariant( doc.firstChildElement())) processing.runAndLoadResults(model, {}) return "<p style=\"color:green\">{0}</p>".format( 'Rotina executada com sucesso!')
def read_from_file(self, file_name: str) -> bool: """ Reads the settings from an XML file """ f = QFile(file_name) if f.open(QIODevice.ReadOnly): document = QDomDocument() if document.setContent(f): if self.read_xml(document.firstChildElement()): return True return False
def downloadStyle(self, qgisLayerItem): #this downloads the layer's style, saves it as an sld and returns two #things: 1. the path to the .sld, 2. the specific name of the style #as it is saved in the background geoserver from shogun, obtained from #the xml-node sld:UserStyle - sld:Name #the specific name is later needed for re-uploading the edited style ## TODO: # maybe a better implementation would be to pass the QDomDocument directly # to the layer and set it's style from sld... # unfortunately for a not known reason this does not work and we first # have to save the sld to a file, then do layer.loadSldStyle(file) # can someone fix it?# shogunlayer = qgisLayerItem.parentShogunLayer url = shogunlayer.source['url'] if url.startswith('/shogun2-webapp'): url = self.baseurl.rstrip('/shogun2-webapp/rest/') + url url += '?service=WMS&request=GetStyles&version=1.1.1&layers=' url += shogunlayer.source['layerNames'] response = self.http.request(url, authenticate=False) mydoc = QDomDocument() mydoc.setContent(response[1]) root = mydoc.firstChildElement('sld:StyledLayerDescriptor') namedLayerNode = root.firstChildElement('sld:NamedLayer') userStyleNode = namedLayerNode.firstChildElement('sld:UserStyle') sldNameNode = userStyleNode.firstChildElement('sld:Name') geoServerStyleName = sldNameNode.text() # # TODO: implement more than point style # check if custom icons are used in the style: # # NOTE: this is the beginning of a larger implementation of # exchange of icons # problem is that shogun2 only serves png icons, and qgis needs # svg to turn them into a style if '<sld:ExternalGraphic>' in response[1]: self.iface.messageBar().pushInfo( 'Info', 'The downloaded style for the current layer contains custom icons ' 'from SHOGUN, which only serves them as PNG pictures, but QGIS ' 'can only read SVG. Until this is fixed, you see a default QGIS ' 'style for the layer') ''' featureTypeStyleNode = userStyleNode.firstChildElement('sld:FeatureTypeStyle') rules = featureTypeStyleNode.elementsByTagName('sld:Rule') for x in range(rules.length()): rule = rules.at(x).toElement() listOfGraphics = rule.elementsByTagName('sld:OnlineResource') for x in range(listOfGraphics.length()): graphicNode = listOfGraphics(x) attributes = graphicNode.attributes() url = attributes.namedItem('xlink:href').nodeValue() id = url.split('getThumbnail.action?id=')[1] iconPath = self.downloadIconThumbnail(id) ''' dirpath = os.path.dirname(__file__) filename = os.path.join(dirpath, 'latest-symbology.sld') with open(filename, 'w') as file: file.write(response[1]) return filename, geoServerStyleName