def getDomDef(self, layer): XMLDocument = QDomDocument("undo-layer") XMLMapLayers = QDomElement() XMLMapLayers = XMLDocument.createElement("maplayers") XMLMapLayer = QDomElement() XMLMapLayer = XMLDocument.createElement("maplayer") layer.writeLayerXML(XMLMapLayer, XMLDocument) XMLMapLayers.appendChild(XMLMapLayer) XMLDocument.appendChild(XMLMapLayers) return XMLMapLayer
def setDataSource( self, newSourceUri): ''' Method to apply a new datasource to a vector Layer ''' layer = self.layer newDS, newUri = self.splitSource(newSourceUri) newDatasourceType = newDS or layer.dataProvider().name() # read layer definition XMLDocument = QDomDocument("style") XMLMapLayers = QDomElement() XMLMapLayers = XMLDocument.createElement("maplayers") XMLMapLayer = QDomElement() XMLMapLayer = XMLDocument.createElement("maplayer") layer.writeLayerXML(XMLMapLayer,XMLDocument) # apply layer definition XMLMapLayer.firstChildElement("datasource").firstChild().setNodeValue(newUri) XMLMapLayer.firstChildElement("provider").firstChild().setNodeValue(newDatasourceType) XMLMapLayers.appendChild(XMLMapLayer) XMLDocument.appendChild(XMLMapLayers) layer.readLayerXML(XMLMapLayer) # Update layer extent layer.updateExtents() # Update graduated symbol renderer if layer.rendererV2().type() == u'graduatedSymbol': if len(layer.rendererV2().ranges()) == 1: layer.rendererV2().updateClasses( layer, layer.rendererV2().mode(), len(layer.rendererV2().ranges()) ) #Reload layer layer.reload()
def __init__(self, distributedObjects, rootname): self.Xml = QDomDocument("xmldoc") self.rootname = rootname self.rootNode = QDomElement() self.clear()
class XmlHandler(): ''' Handler used to save session info to an xml file. ''' def __init__(self, distributedObjects, rootname): self.Xml = QDomDocument("xmldoc") self.rootname = rootname self.rootNode = QDomElement() self.clear() def createNode(self, nodeName, parentNode=None, withAttribs={}): ''' Create and insert node @param nodeName: String representing name of node @param parentNode: QDomElement representing parent node. Root will be parent if this is None @param withAttribs: dict with node attributes @return node on success, None on error ''' if isinstance(nodeName, str) and len(nodeName) != 0: node = self.Xml.createElement(nodeName) if isinstance(withAttribs, dict) and withAttribs != {}: for key, value in withAttribs.items(): node.setAttribute(key, value) if parentNode == None: self.rootNode.appendChild(node) else: if isinstance(parentNode, QDomElement): parentNode.appendChild(node) return node return None def getNode(self, nodeName, parent=None): ''' Get first xml node by name @return QDomElement ''' if isinstance(nodeName, str) and len(nodeName) != 0: if parent == None or not isinstance(parent, QDomNode): return self.rootNode.firstChildElement(nodeName) else: return parent.firstChildElement(nodeName) def getAttributes(self, node): ''' Get a dict with attributes of node @param node: QDomElement @return dict(QString, QString) with attributes ''' attribs = {} if isinstance(node, QDomElement) or isinstance(node, QDomNode): namedNodeMap = node.attributes() for i in range(namedNodeMap.count()): item = namedNodeMap.item(i) attribs[str(item.nodeName())] = str(item.nodeValue()) return attribs def save(self, filename): """Writes session info to xml file. Overwrites existing files""" if not filename.endswith(".xml"): filename = filename + ".xml" file_object = QFile(filename) if not file_object.open(QIODevice.WriteOnly): logging.error("File %s could not be opened.", filename) else: file_object.writeData(self.Xml.toString()) file_object.close() def clear(self): ''' Clear xml doc and reset root node''' self.Xml.clear() self.rootNode = self.Xml.createElement(self.rootname) self.Xml.appendChild(self.rootNode) def load(self, filename): """Loads session info from xml file. Returns false if loading fails, true otherwise.""" if not exists(filename): logging.error("Cannot restore session - File %s not found.", filename) return False file_object = QFile(filename) self.Xml.clear() if not file_object.open(QIODevice.ReadOnly): logging.error("File %s could not be opened.", filename) return False else: if not self.Xml.setContent(file_object.readAll()): logging.error("self.Xml.setContent() failed.") file_object.close() return False file_object.close() self.rootNode = self.Xml.documentElement() return True
class XmlHandler(): ''' Handler used to save session info to an xml file. ''' def __init__(self, distributedObjects, rootname): self.Xml = QDomDocument("xmldoc") self.rootname = rootname self.rootNode = QDomElement() self.clear() def createNode(self, nodeName, parentNode=None, withAttribs={}): ''' Create and insert node @param nodeName: String representing name of node @param parentNode: QDomElement representing parent node. Root will be parent if this is None @param withAttribs: dict with node attributes @return node on success, None on error ''' if isinstance(nodeName, str) and len(nodeName) != 0: node = self.Xml.createElement(nodeName) if isinstance(withAttribs, dict) and withAttribs != {}: for key, value in withAttribs.items(): node.setAttribute(key, value) if parentNode == None: self.rootNode.appendChild(node) else: if isinstance(parentNode, QDomElement): parentNode.appendChild(node) return node return None def getNode(self, nodeName, parent=None): ''' Get first xml node by name @return QDomElement ''' if isinstance(nodeName, str) and len(nodeName) != 0: if parent == None or not isinstance(parent, QDomNode): return self.rootNode.firstChildElement(nodeName) else: return parent.firstChildElement(nodeName) def getAttributes(self, node): ''' Get a dict with attributes of node @param node: QDomElement @return dict(QString, QString) with attributes ''' attribs = {} if isinstance(node, QDomElement) or isinstance(node, QDomNode): namedNodeMap = node.attributes() for i in xrange(namedNodeMap.count()): item = namedNodeMap.item(i) attribs[str(item.nodeName())] = str(item.nodeValue()) return attribs def save(self, filename): """Writes session info to xml file. Overwrites existing files""" if not filename.endswith(".xml"): filename = filename + ".xml" file_object = QFile(filename) if not file_object.open(QIODevice.WriteOnly): logging.error("File %s could not be opened.", filename) else: file_object.writeData(self.Xml.toString()) file_object.close() def clear(self): ''' Clear xml doc and reset root node''' self.Xml.clear() self.rootNode = self.Xml.createElement(self.rootname) self.Xml.appendChild(self.rootNode) def load(self, filename): """Loads session info from xml file. Returns false if loading fails, true otherwise.""" if not exists(filename): logging.error("Cannot restore session - File %s not found.", filename) return False file_object = QFile(filename) self.Xml.clear() if not file_object.open(QIODevice.ReadOnly): logging.error("File %s could not be opened.", filename) return False else: if not self.Xml.setContent(file_object.readAll()): logging.error("self.Xml.setContent() failed.") file_object.close() return False file_object.close() self.rootNode = self.Xml.documentElement() return True