class Style: def __init__(self): self._styles = [] def __getitem__(self, index): return self._styles[index] def __len__(self): return len(self._styles) def __iter__(self): return iter(self._styles) def _load_color_ramps(self, filename, attribute, classes=10): self._styleFactory = QgsStyle() self._styleFactory.importXml(filename) for styleName in self._styleFactory.colorRampNames(): self._styles.append({ 'name': styleName, 'colorramp': self._styleFactory.colorRamp(styleName), 'classes': classes, 'attribute': attribute })
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) styles = QgsStyle().defaultStyle() if 'PAT' not in styles.tags() or (loaded_date is None or xml_date > loaded_date): if styles.isXmlStyleFile(pat_xml): if styles.importXml(pat_xml): LOGGER.info('Loaded PAT Symbology') write_setting(PLUGIN_NAME + '/PAT_SYMBOLOGY', xml_date.strftime('%Y-%m-%d %H:%M:%S')) else: LOGGER.warning('Loading PAT Symbology failed') else: LOGGER.debug('Could not open file {}'.format(pat_xml)) return
if not validate_and_clean_xml(root, filename=extract_name(filename)): log.error(f"cannot validate {filename}") if root.findall("./symbols/symbol"): for symbol in root.findall("./symbols/symbol"): symbol.attrib['tags'] = auth + ',geology' n = (symbol.attrib['name']) print(n) c, d = name_parser(n) symbols.append(symbol) count_dict[auth] += 1 style = QgsStyle() style.importXml(xmlfile) n_styles = style.symbolCount() symbol_name = style.symbolNames()[0] symbol = style.symbol(symbol_name) size = QSize(64, 64) image = symbol.asImage(size) path, filename = os.path.split(xmlfile) png_filename = filename.replace('.xml', '.png') png_dir = os.path.join("../docs/images/library/", auth) if not os.path.exists(png_dir): os.makedirs(png_dir) image.save(r"{}".format(png_dir + '/' + png_filename), "PNG") status_path = os.path.join("docs/images/library/", auth) lnk = "![]({})".format(os.path.join(status_path, png_filename))
def run(self): """ Start the library generation """ patched_xml = ['S_01_081_0017', 'S_01_081_0018', 'S_14_145_0015', 'S_05_019_0001', 'L_19_006_0008', 'L_25_116_0001', 'S_11_042_0013', 'L_21_105_0002', 'L_21_107_0010', 'L_22_092_0003', 'L_22_092_0008', 'L_22_092_0009', 'L_22_092_0010', 'L_22_092_0011', 'L_22_092_0012', 'P_31_159_0002', 'S_01_081_0010', 'S_01_089_0016', 'S_05_017_0008', 'S_10_041_0004', 'S_13_046_0012', 'S_13_046_0018', 'S_14_048_0008', 'S_18_071_0001', 'P_28_157_0001', 'P_32_160_0001', 'P_33_161_0001'] # Symbols for which it exists a manually modified svg patched_svg = ['P_26_120_0015', 'P_33_172_0001', 'P_26_122_0007', 'P_26_124_0004_1', 'L_20_099_0006', 'L_20_099_0007', 'L_20_099_0009', 'L_20_099_0011', 'P_28_157_0001', 'P_32_160_0001', 'P_33_161_0001'] blobs = [] style = QgsStyle() context = Context() context.units = QgsUnitTypes.RenderMillimeters context.relative_paths = True context.picture_folder = os.path.join( self.output_directory, 'svg') context.convert_fonts = True context.force_svg_instead_of_raster = True context.parameterise_svg = False context.embed_pictures = False a = QApplication([]) for fn in os.listdir(self.bin_directory): file = os.path.join(self.bin_directory, fn) if os.path.isfile(file): blobs.append(file) symbol_name = os.path.splitext(fn)[0] with open(file, 'rb') as f: context.symbol_name = symbol_name symbol = read_symbol(f, debug=False) qgis_symbol = Symbol_to_QgsSymbol(symbol, context) if symbol_name in patched_xml: xml_file = os.path.join( self.patched_xml_directory, symbol_name + '.xml') style.importXml(xml_file) print("Patch symbol {} with {}".format( symbol_name, xml_file)) continue style.addSymbol(symbol_name, qgis_symbol) style.exportXml( os.path.join(self.output_directory, 'libreria.xml')) for svg in patched_svg: svg_src = os.path.join(self.patched_svg_directory, svg + '.svg') svg_dest = os.path.join( self.output_directory, 'svg', svg + '.svg') copyfile(svg_src, svg_dest) print("Patch svg {} with {}".format(svg_dest, svg_src))