2

I want to extract the SVG-Paths and SVG-Symbols for all Layers in my QGIS Project.

The purpose is to open the SVG files via a plugin (so via PyQgis) and I don't know how to do so. I checked the QGIS Pyton API but did not find a solution (have to admit that I'm quite new to PyQgis).

Would be glad if someone could give me a hint.

1

If an SVG is referenced in your project, it is contained in the project file, usually a file with .qgz (compressed) or .qgs (not compressed).

By expanding a .qgz project file, a .qgs file is obtained.

.qgs project files are actually standard .xml file, so you can open them with any text editor, searching "svg".

Under Linux you can conveniently grep for "svg" instances:

> grep svg your_project.qgs
          <prop k="name" v="food/food_bar.svg"/>
          ...
          <prop k="name" v="amenity/amenity_library.svg"/>
>
  • Got already the Qgs Project File, but need the whole path for the used svg files, since there could be more search paths / svg locations in my options. – Nunkuat Nov 21 '19 at 15:09
0

If someone is interested in how I fixed my problem:

import xml.etree.cElementTree as ET
import json
import os

tree = ET.parse('test_project.qgs')
root = tree.getroot()

svg_list = []
# get the svg-files from the qgs-file 
for svg in root.iter('prop'): 
    prop_dic = svg.attrib
    text = json.dumps(prop_dic)
    if ".svg" not in text:
        continue
    else:
        test = text.split('"')
        matching = [s for s in test if ".svg" in s]
        svg_list.append(str(matching)[2:-2])

#get the registered svg Paths that are deposited under options/system/svg-paths
svg_Path = qgis.core.QgsApplication.svgPaths()

#search for every file in each path
for p in svg_Path:
    for svg in svg_list:
        svg_w = os.path.join(p, svg)
        #check in which path the file is present
        if os.path.exists(svg_w) is True: 
            print('found')
        else:
            pass

Im pretty sure it's not the best way to do the task, but it worked for me, so maybe i can help somebody with this.

The code is executed in the QGis Python Console, otherwise more imports are needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.