def processAlgorithm(self, parameters, context, feedback): # pylint: disable=missing-docstring,too-many-locals input_file = self.parameterAsString(parameters, self.INPUT, context) output_file = self.parameterAsFileOutput(parameters, self.OUTPUT, context) mdbtools_folder = ProcessingConfig.getSetting('MDB_PATH') style = QgsStyle() results = {} for type_index, symbol_type in enumerate( (Extractor.FILL_SYMBOLS, Extractor.LINE_SYMBOLS, Extractor.MARKER_SYMBOLS)): feedback.pushInfo('Importing {} from {}'.format(symbol_type, input_file)) raw_symbols = Extractor.extract_styles(input_file, symbol_type, mdbtools_path=mdbtools_folder) feedback.pushInfo('Found {} symbols of type "{}"\n\n'.format(len(raw_symbols), symbol_type)) if feedback.isCanceled(): break unreadable = 0 for index, raw_symbol in enumerate(raw_symbols): feedback.setProgress(index / len(raw_symbols) * 33.3 + 33.3 * type_index) if feedback.isCanceled(): break name = raw_symbol[Extractor.NAME] feedback.pushInfo('{}/{}: {}'.format(index + 1, len(raw_symbols), name)) handle = BytesIO(raw_symbol[Extractor.BLOB]) try: symbol = read_symbol(file_handle=handle) except UnreadableSymbolException as e: feedback.reportError('Error reading symbol {}: {}'.format(name, e)) unreadable += 1 continue try: qgis_symbol = Symbol_to_QgsSymbol(symbol) except NotImplementedException as e: feedback.reportError(str(e)) unreadable += 1 continue style.addSymbol(name, qgis_symbol) if symbol_type == Extractor.FILL_SYMBOLS: results[self.FILL_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_FILL_SYMBOLS] = unreadable elif symbol_type == Extractor.LINE_SYMBOLS: results[self.LINE_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_LINE_SYMBOLS] = unreadable elif symbol_type == Extractor.MARKER_SYMBOLS: results[self.MARKER_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_MARKER_SYMBOLS] = unreadable style.exportXml(output_file) results[self.OUTPUT] = output_file return results
def processAlgorithm(self, parameters, context, feedback): # pylint: disable=missing-docstring,too-many-locals,too-many-statements input_file = self.parameterAsString(parameters, self.INPUT, context) output_file = self.parameterAsFileOutput(parameters, self.OUTPUT, context) mdbtools_folder = ProcessingConfig.getSetting('MDB_PATH') results = {} colors = [] _, file_name = os.path.split(input_file) file_name, _ = os.path.splitext(file_name) feedback.pushInfo('Importing colors from {}'.format(input_file)) raw_colors = Extractor.extract_styles(input_file, Extractor.COLORS, mdbtools_path=mdbtools_folder) feedback.pushInfo('Found {} colors'.format(len(raw_colors))) unreadable = 0 for index, raw_color in enumerate(raw_colors): feedback.setProgress(index / len(raw_colors) * 100) if feedback.isCanceled(): break name = raw_color[Extractor.NAME] feedback.pushInfo('{}/{}: {}'.format(index + 1, len(raw_colors), name)) handle = BytesIO(raw_color[Extractor.BLOB]) stream = Stream(handle) try: color = stream.read_object() except InvalidColorException: feedback.reportError('Error reading color {}'.format(name)) unreadable += 1 continue qcolor = symbol_color_to_qcolor(color) colors.append((name, qcolor)) results[self.COLOR_COUNT] = len(raw_colors) results[self.UNREADABLE_COLOR_COUNT] = unreadable with open(output_file, 'wt') as f: f.write('GIMP Palette\n') f.write('Name: {}\n'.format(file_name)) f.write('Columns: 4\n') f.write('#\n') for c in colors: f.write('{} {} {} {}\n'.format(c[1].red(), c[1].green(), c[1].blue(), c[0])) results[self.OUTPUT] = output_file return results
from slyr.bintools.file_utils import FileUtils initialize_registry() parser = argparse.ArgumentParser() parser.add_argument("file", help="style file to extract") parser.add_argument("destination", help="destination folder") args = parser.parse_args() styles = [(args.file, Extractor.FILL_SYMBOLS)] output_path = args.destination for symbol_type in [ Extractor.FILL_SYMBOLS, Extractor.LINE_SYMBOLS, Extractor.MARKER_SYMBOLS, Extractor.COLORS, Extractor.COLOR_RAMPS ]: raw_symbols = Extractor.extract_styles(args.file, symbol_type) print('Found {} symbols of type "{}"\n\n'.format(len(raw_symbols), symbol_type)) for raw_symbol in raw_symbols: symbol_name = raw_symbol[Extractor.NAME] print('Extracting {}'.format(symbol_name)) out_filename = FileUtils.clean_symbol_name_for_file( symbol_name) + '.bin' file = os.path.join(output_path, out_filename) with open(file, 'wb') as e: e.write(raw_symbol[Extractor.BLOB])
args = parser.parse_args() if not args.file: args.file = '/home/nyall/Styles/GEO_Surface___Solid_Shades.style' if not args.destination: args.destination = '/home/nyall/Styles/GEO_Surface___Solid_Shades.xml' styles = [(args.file, Extractor.FILL_SYMBOLS)] style = QgsStyle() for (fill_style_db, symbol_type) in styles: print('{}:{}'.format(fill_style_db, symbol_type)) raw_symbols = Extractor.extract_styles(fill_style_db, symbol_type) print('Found {} symbols of type "{}"\n\n'.format(len(raw_symbols), symbol_type)) for index, raw_symbol in enumerate(raw_symbols): name = raw_symbol[Extractor.NAME] # print('{}/{}: {}'.format(index + 1, len(raw_symbols),name)) handle = BytesIO(raw_symbol[Extractor.BLOB]) try: symbol = read_symbol(file_handle=handle) except UnreadableSymbolException: print('Error reading symbol {}'.format(name)) continue qgis_symbol = FillSymbol_to_QgsFillSymbol(symbol)
def processAlgorithm(self, parameters, context, feedback): # pylint: disable=missing-docstring,too-many-locals,too-many-statements input_file = self.parameterAsString(parameters, self.INPUT, context) output_file = self.parameterAsFileOutput(parameters, self.OUTPUT, context) mdbtools_folder = ProcessingConfig.getSetting('MDB_PATH') style = QgsStyle() results = {} symbol_names = set() def make_name_unique(name): """ Ensures that the symbol name is unique (in a case insensitive way) """ counter = 0 candidate = name while candidate.lower() in symbol_names: # make name unique if counter == 0: candidate += '_1' else: candidate = candidate[:candidate.rfind('_') + 1] + str(counter) counter += 1 symbol_names.add(candidate.lower()) return candidate for type_index, symbol_type in enumerate( (Extractor.FILL_SYMBOLS, Extractor.LINE_SYMBOLS, Extractor.MARKER_SYMBOLS)): feedback.pushInfo('Importing {} from {}'.format( symbol_type, input_file)) raw_symbols = Extractor.extract_styles( input_file, symbol_type, mdbtools_path=mdbtools_folder) feedback.pushInfo('Found {} symbols of type "{}"\n\n'.format( len(raw_symbols), symbol_type)) if feedback.isCanceled(): break unreadable = 0 for index, raw_symbol in enumerate(raw_symbols): feedback.setProgress(index / len(raw_symbols) * 33.3 + 33.3 * type_index) if feedback.isCanceled(): break name = raw_symbol[Extractor.NAME] feedback.pushInfo('{}/{}: {}'.format(index + 1, len(raw_symbols), name)) unique_name = make_name_unique(name) if name != unique_name: feedback.pushInfo( 'Corrected to unique name of {}'.format(unique_name)) handle = BytesIO(raw_symbol[Extractor.BLOB]) stream = Stream(handle) try: symbol = stream.read_object() except UnreadableSymbolException as e: feedback.reportError('Error reading symbol {}: {}'.format( name, e)) unreadable += 1 continue except NotImplementedException as e: feedback.reportError( 'Parsing {} is not supported: {}'.format(name, e)) unreadable += 1 continue except UnsupportedVersionException as e: feedback.reportError('Cannot read {} version: {}'.format( name, e)) unreadable += 1 continue try: qgis_symbol = Symbol_to_QgsSymbol(symbol) except NotImplementedException as e: feedback.reportError(str(e)) unreadable += 1 continue style.addSymbol(unique_name, qgis_symbol) if symbol_type == Extractor.FILL_SYMBOLS: results[self.FILL_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_FILL_SYMBOLS] = unreadable elif symbol_type == Extractor.LINE_SYMBOLS: results[self.LINE_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_LINE_SYMBOLS] = unreadable elif symbol_type == Extractor.MARKER_SYMBOLS: results[self.MARKER_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_MARKER_SYMBOLS] = unreadable style.exportXml(output_file) results[self.OUTPUT] = output_file return results
def processAlgorithm(self, # pylint:disable=missing-docstring,too-many-locals,too-many-statements,too-many-branches parameters, context, feedback): input_file = self.parameterAsString(parameters, self.INPUT, context) output_file = self.parameterAsFileOutput(parameters, self.OUTPUT, context) embed_pictures = self.parameterAsBool(parameters, self.EMBED_PICTURES, context) convert_fonts = self.parameterAsBool(parameters, self.CONVERT_FONTS, context) parameterize = self.parameterAsBool(parameters, self.PARAMETERIZE, context) units = self.parameterAsEnum(parameters, self.UNITS, context) force_svg = self.parameterAsBool(parameters, self.FORCE_SVG, context) relative_paths = self.parameterAsBool(parameters, self.RELATIVE_PATHS, context) picture_folder = self.parameterAsString(parameters, self.PICTURE_FOLDER, context) if not picture_folder: picture_folder, _ = os.path.split(output_file) mdbtools_folder = ProcessingConfig.getSetting('MDB_PATH') fields = QgsFields() fields.append(QgsField('name', QVariant.String, '', 60)) fields.append(QgsField('warning', QVariant.String, '', 250)) sink, dest = self.parameterAsSink(parameters, self.REPORT, context, fields) style = QgsStyle() style.createMemoryDatabase() results = {} symbol_names = set() def make_name_unique(name): """ Ensures that the symbol name is unique (in a case insensitive way) """ counter = 0 candidate = name while candidate.lower() in symbol_names: # make name unique if counter == 0: candidate += '_1' else: candidate = candidate[:candidate.rfind('_') + 1] + str(counter) counter += 1 symbol_names.add(candidate.lower()) return candidate for type_index, symbol_type in enumerate( (Extractor.FILL_SYMBOLS, Extractor.LINE_SYMBOLS, Extractor.MARKER_SYMBOLS, Extractor.COLOR_RAMPS)): feedback.pushInfo('Importing {} from {}'.format(symbol_type, input_file)) raw_symbols = Extractor.extract_styles(input_file, symbol_type, mdbtools_path=mdbtools_folder) feedback.pushInfo('Found {} symbols of type "{}"\n\n'.format(len(raw_symbols), symbol_type)) if feedback.isCanceled(): break unreadable = 0 for index, raw_symbol in enumerate(raw_symbols): feedback.setProgress(index / len(raw_symbols) * 33.3 + 33.3 * type_index) if feedback.isCanceled(): break name = raw_symbol[Extractor.NAME] tags = raw_symbol[Extractor.TAGS].split(';') feedback.pushInfo('{}/{}: {}'.format(index + 1, len(raw_symbols), name)) unique_name = make_name_unique(name) if name != unique_name: feedback.pushInfo('Corrected to unique name of {}'.format(unique_name)) handle = BytesIO(raw_symbol[Extractor.BLOB]) stream = Stream(handle) f = QgsFeature() try: symbol = stream.read_object() except UnreadableSymbolException as e: feedback.reportError('Error reading symbol {}: {}'.format(name, e)) unreadable += 1 if sink: f.setAttributes([name, 'Error reading symbol: {}'.format(e)]) sink.addFeature(f) continue except NotImplementedException as e: feedback.reportError('Parsing {} is not supported: {}'.format(name, e)) unreadable += 1 if sink: f.setAttributes([name, 'Parsing not supported: {}'.format(e)]) sink.addFeature(f) continue except UnsupportedVersionException as e: feedback.reportError('Cannot read {} version: {}'.format(name, e)) unreadable += 1 if sink: f.setAttributes([name, 'Version not supported: {}'.format(e)]) sink.addFeature(f) continue except UnknownGuidException as e: feedback.reportError(str(e)) unreadable += 1 if sink: f.setAttributes([name, 'Unknown object: {}'.format(e)]) sink.addFeature(f) continue except UnreadablePictureException as e: feedback.reportError(str(e)) unreadable += 1 if sink: f.setAttributes([name, 'Unreadable picture: {}'.format(e)]) sink.addFeature(f) continue self.check_for_unsupported_property(name, symbol, feedback, sink) context = Context() context.symbol_name = unique_name context.picture_folder = picture_folder context.embed_pictures = embed_pictures context.convert_fonts = convert_fonts context.parameterise_svg = parameterize context.force_svg_instead_of_raster = force_svg context.relative_paths = relative_paths context.style_folder, _ = os.path.split(output_file) context.units = QgsUnitTypes.RenderPoints if units == 0 else QgsUnitTypes.RenderMillimeters try: qgis_symbol = Symbol_to_QgsSymbol(symbol, context) except NotImplementedException as e: feedback.reportError(str(e)) unreadable += 1 if sink: f.setAttributes([name, str(e)]) sink.addFeature(f) continue if isinstance(qgis_symbol, QgsSymbol): self.check_for_missing_fonts(qgis_symbol, feedback) style.addSymbol(unique_name, qgis_symbol, True) elif isinstance(qgis_symbol, QgsColorRamp): style.addColorRamp(unique_name, qgis_symbol, True) if tags: if isinstance(qgis_symbol, QgsSymbol): assert style.tagSymbol(QgsStyle.SymbolEntity, unique_name, tags) elif isinstance(qgis_symbol, QgsColorRamp): assert style.tagSymbol(QgsStyle.ColorrampEntity, unique_name, tags) if symbol_type == Extractor.FILL_SYMBOLS: results[self.FILL_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_FILL_SYMBOLS] = unreadable elif symbol_type == Extractor.LINE_SYMBOLS: results[self.LINE_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_LINE_SYMBOLS] = unreadable elif symbol_type == Extractor.MARKER_SYMBOLS: results[self.MARKER_SYMBOL_COUNT] = len(raw_symbols) results[self.UNREADABLE_MARKER_SYMBOLS] = unreadable elif symbol_type == Extractor.COLOR_RAMPS: results[self.COLOR_RAMP_COUNT] = len(raw_symbols) results[self.UNREADABLE_COLOR_RAMPS] = unreadable style.exportXml(output_file) results[self.OUTPUT] = output_file results[self.REPORT] = dest return results