def parse_xml(self): """Parse the xml file. Returns false if there is failure.""" xml_file = QFile(self._xml_path) if not xml_file.open(QIODevice.ReadOnly): return False document = QDomDocument() if not document.setContent(xml_file): return False xml_file.close() document_element = document.documentElement() if document_element.tagName() != 'qgis_style': return False # Get all the symbols self._symbols = [] symbols_element = document_element.firstChildElement('symbols') symbol_element = symbols_element.firstChildElement() while not symbol_element.isNull(): if symbol_element.tagName() == 'symbol': symbol = QgsSymbolLayerV2Utils.loadSymbol(symbol_element) if symbol: self._symbols.append({ 'name': symbol_element.attribute('name'), 'symbol': symbol }) symbol_element = symbol_element.nextSiblingElement() # Get all the colorramps self._colorramps = [] ramps_element = document_element.firstChildElement('colorramps') ramp_element = ramps_element.firstChildElement() while not ramp_element.isNull(): if ramp_element.tagName() == 'colorramp': colorramp = QgsSymbolLayerV2Utils.loadColorRamp(ramp_element) if colorramp: self._colorramps.append({ 'name': ramp_element.attribute('name'), 'colorramp': colorramp }) ramp_element = ramp_element.nextSiblingElement() return True
def check_pat_symbols(): pat_xml = os.path.join(PLUGIN_DIR, 'PAT_Symbols.xml') if not os.path.exists(pat_xml): return loaded_date = read_setting(PLUGIN_NAME + "/PAT_SYMBOLOGY") if loaded_date is not None: loaded_date = datetime.strptime(loaded_date,'%Y-%m-%d %H:%M:%S') xml_date = datetime.fromtimestamp(os.path.getmtime(pat_xml)).replace(microsecond=0) if loaded_date is None or xml_date > loaded_date: style = QgsStyleV2.defaultStyle() # add a group if it doesn't exist. group_id = style.groupId('PAT') if group_id == 0: group_id = style.addGroup('PAT') xml_file = QFile(pat_xml) document = QDomDocument() if not document.setContent(xml_file): LOGGER.debug('Could not open file {}'.format(xml_file)) return xml_file.close() document_element = document.documentElement() if document_element.tagName() != 'qgis_style': LOGGER.debug("File {} doesn't contain qgis styles".format(xml_file)) return # Get all the symbols symbols = [] symbols_element = document_element.firstChildElement('symbols') symbol_element = symbols_element.firstChildElement() while not symbol_element.isNull(): if symbol_element.tagName() == 'symbol': symbol = QgsSymbolLayerV2Utils.loadSymbol(symbol_element) if symbol: symbols.append({ 'name': symbol_element.attribute('name'), 'symbol': symbol }) symbol_element = symbol_element.nextSiblingElement() # Get all the colorramps colorramps = [] ramps_element = document_element.firstChildElement('colorramps') ramp_element = ramps_element.firstChildElement() while not ramp_element.isNull(): if ramp_element.tagName() == 'colorramp': colorramp = QgsSymbolLayerV2Utils.loadColorRamp(ramp_element) if colorramp: colorramps.append({ 'name': ramp_element.attribute('name'), 'colorramp': colorramp }) ramp_element = ramp_element.nextSiblingElement() for symbol in symbols: if style.addSymbol(symbol['name'], symbol['symbol'], True): style.group(QgsStyleV2.SymbolEntity, symbol['name'], group_id) for colorramp in colorramps: if style.addColorRamp(colorramp['name'], colorramp['colorramp'], True): style.group(QgsStyleV2.ColorrampEntity, colorramp['name'], group_id) LOGGER.info( 'Loaded {} symbols and {} colour ramps into group {}'.format(len(symbols), len(colorramps), style.groupName(group_id))) write_setting(PLUGIN_NAME + '/PAT_SYMBOLOGY', xml_date.strftime('%Y-%m-%d %H:%M:%S')) return