[Qgis-user] Changing SVG Search Paths without GUI on Debian Buster | QGIS 3.10.12

Charles Dixon-Paver charles at kartoza.com
Tue Jan 5 02:47:22 PST 2021


Any number of things can go wrong with file paths, but QGIS allows you to
embed SVG objects in project files to avoid these problems. If you can open
the project on your windows environment, where the SVG file path is
specified you should be able to use the drop down on the "Browse" menu and
select the embed option, then specify the SVG file to include.

I also previously wrote a script to automatically embed the svg symbols for
all layers in a project if you have a lot of layers to update. Currently
only supports single layer marker (so if you have complex symbology it may
not work). You may be able to adapt it to your needs

One issue is that the marker is embedded as a base64 string. This means if
you have a complex SVG and use the same symbol on multiple layers, it may
be a bit clunky especially when used on QGIS server. I still think
investigating the file path issue is worth figuring out, but the embed
method should help as a quickfix.

"""
Run from the python QGIS console to take existing svg paths and
convert them to project-embedded symbol items. Currently only
supports single symbol and rule style types. Doesn't cater for
geometry builder elements or other advanced functions.
"""

import os
import base64

def embedSymbol(symbol):
    try:
        layer_type = symbol.layerType()
        if layer_type == 'SvgMarker':
            svg_path = symbol.path()
            if svg_path[:7] == 'base64:':
                print('svg symbol already embedded')
            else:
                encoded_string = ""
                with open(svg_path, "rb") as svg:
                    encoded_string = base64.b64encode(svg.read())
                    decoded_string = encoded_string.decode("utf-8")
                    svg_content = 'base64:' + decoded_string
                    symbol.setPath(svg_content)
                    print('embedded svg symbol')
        else:
            print('not an svg symbol')
    except Exception as err:
        print(err)


layers = [layer for layer in QgsProject.instance().mapLayers().values()]

for layer in layers:
    if layer.type() == QgsMapLayer.VectorLayer:
        try:
            print(layer.name())
            render = layer.renderer()
            render_type = render.type()
            if render_type == 'singleSymbol':
                if hasattr(render.symbol(), 'symbolLayers'):
                    for symbol in render.symbol().symbolLayers():
                        embedSymbol(symbol)
                else:
                    embedSymbol(render.symbol())
            if render_type == 'RuleRenderer':
                rules = render.rootRule()
                rules = rules.children()
                for rule in rules:
                    if hasattr(rule.symbol(), 'symbolLayers'):
                        for symbol in rule.symbol().symbolLayers():
                            embedSymbol(symbol)
                    else:
                        embedSymbol(rule.symbol())
        except Exception as err:
            print(err)


I hope that helps


On Tue, 5 Jan 2021 at 12:35, STEINER Andreas <asteiner at eurofunk.com> wrote:

> Hello everyone.
>
> I tried to load a QGIS Project which was created on windows. In this
> project I used a svg symbol "symbol/blue-marker.svg". In the .qgis file the
> path is also "symbol/blue-marker.svg".
>
> When I start my qgisserver and request the map there are only this ?
> questions marks because qgis can't find the blue-marker.svg.
>
> It should look like this:
>
> I tried to copy the symbol folder with the symbols to /usr/lib/cgi-bin/
> folder where the qgis_mapserv.fcgi file is located. Didn't work.
> Then I tried to copy the symbol folder to the location of the .qgis
> project file. Didn't work.
>
> I researched a little bit and found a command to show me what the default
> svg paths are:
>
> python3
>
> from qgis.core import QgsApplication
>
> QgsApplication.showSettings()
>
> 'Application state:\nQGIS_PREFIX_PATH env
> var:\t\t/usr/share/qgis/lib/qgis/server\nPrefix:\t\t\nPlugin
> Path:\t\t\nPackage Data
> Path:\t/usr/share/qgis/lib/qgis/server/share/qgis\nActive Theme
> Name:\t\nActive Theme
> Path:\t/usr/share/qgis/lib/qgis/server/share/qgis/resources/themes//icons/\nDefault
> Theme Path:\t:/images/themes/default/\nSVG Search Paths:\t\nUser DB
> Path:\t/usr/share/qgis/lib/qgis/server/share/qgis/resources/qgis.db\nAuth
> DB Path:\tqgis-auth.db\n'
>
>
>
> *SVG Search Paths:\t\n*
>
> From there I know that my SVG Search Paths are empty.
>
> I can change the variable temporary with:
>
> from qgis.core import QgsApplication
>
> QgsApplication.setDefaultSvgPaths(['/usr/share/qgis/svg'])
>
> but this doesn't help.
>
> I also tried to execute this command when creating the docker image but
> the SVG Search Paths are also empty after that.
> So in my opinion somewhere this variable can be set but I can't find it,
> also not in the QGIS Environment variables.
>
> Here is my qgis apache2 config:
>
> <VirtualHost *:80>
>
>   ServerAdmin webmaster at localhost
>
>   ServerName qgis.demo
>
>
>
>   DocumentRoot /var/www/html
>
>
>
>   # Apache logs (different than QGIS Server log)
>
>   ErrorLog ${APACHE_LOG_DIR}/qgis.demo.error.log
>
>   CustomLog ${APACHE_LOG_DIR}/qgis.demo.access.log combined
>
>
>
>   # Longer timeout for WPS... default = 40
>
>   FcgidIOTimeout 120
>
>
>
>   FcgidInitialEnv LC_ALL "en_US.UTF-8"
>
>   FcgidInitialEnv PYTHONIOENCODING UTF-8
>
>   FcgidInitialEnv LANG "en_US.UTF-8"
>
>
>
>   # QGIS log (different from apache logs) see
> https://docs.qgis.org/testing/en/docs/user_manual/working_with_ogc/ogc_server_support.html#qgis-server-logging
>
>   FcgidInitialEnv QGIS_SERVER_LOG_FILE /var/log/qgis/qgisserver.log
>
>   FcgidInitialEnv QGIS_SERVER_LOG_LEVEL 0
>
>   #FcgidInitialEnv QGIS_PREFIX_PATH /usr/share/qgis/lib/qgis/server
>
>   FcgidInitialEnv QGIS_PREFIX_PATH /usr
>
>   FcgidInitialEnv QGIS_PLUGINPATH /opt/qgis-server/plugins
>
>   FcgidInitialEnv QGIS_DEBUG 0
>
>
>
>   # default QGIS project
>
>   SetEnv QGIS_PROJECT_FILE /home/qgis/projects/world.qgs
>
>
>
>   # QGIS_AUTH_DB_DIR_PATH must lead to a directory writeable by the
> Server's FCGI process user
>
>   FcgidInitialEnv QGIS_AUTH_DB_DIR_PATH "/home/qgis/qgisserverdb/"
>
>   FcgidInitialEnv QGIS_AUTH_PASSWORD_FILE
> "/home/qgis/qgisserverdb/qgis-auth.db"
>
>
>
>   # See
> https://docs.qgis.org/testing/en/docs/user_manual/working_with_vector/supported_data.html#pg-service-file
>
>   SetEnv PGSERVICEFILE /home/qgis/.pg_service.conf
>
>   FcgidInitialEnv PGPASSFILE "/home/qgis/.pgpass"
>
>
>
>   # Tell QGIS Server instances to use a specific display number
>
>   # FcgidInitialEnv DISPLAY ":99"
>
>
>
>   # if qgis-server is installed from packages in debian based distros this
> is usually /usr/lib/cgi-bin/
>
>   # run "locate qgis_mapserv.fcgi" if you don't know where
> qgis_mapserv.fcgi is
>
>   ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
>
>   <Directory "/usr/lib/cgi-bin/">
>
>     AllowOverride None
>
>     Options +ExecCGI -MultiViews -SymLinksIfOwnerMatch
>
>     Order allow,deny
>
>     Allow from all
>
>     Require all granted
>
>   </Directory>
>
>
>
> <IfModule mod_fcgid.c>
>
> FcgidMaxRequestLen 26214400
>
> FcgidConnectTimeout 60
>
> </IfModule>
>
>
>
> </VirtualHost>
>
>
>
> Thanks for helping 😊
>
>
>
> Best regards,
>
> *Andreas Steiner*
>
>
> _______________________________________________
> Qgis-user mailing list
> Qgis-user at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210105/381f3dfa/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image003.png
Type: image/png
Size: 18419 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210105/381f3dfa/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image002.jpg
Type: image/jpeg
Size: 5890 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20210105/381f3dfa/attachment-0001.jpg>


More information about the Qgis-user mailing list