1

Is it possible, to restore the layer-styles from a *.QGS project file?

I have unhandled layers in my project. As I'm starting the project, QGIS is asking me for the source of the layers but I don't have them anymore. So I ignore it and the project will be - of course - loaded without any layers and without their styles. But I just need the styles - how do I get to them as style file?

5

If you open a .QGS with a text editor you will see some XML code. Your project layers are inside <projectlayers> ... </projectlayers> tags and each layer is in <maplayer>...</maplayer> tags. Search through the maplayer tags until you find the broken one that has a missing source. You can search for the layername tag to help you.

Once you find that, search for the next <renderer-v2> tag. This is the style definition for that layer. Copy all the text including the open <renderer-v2> and closing </renderer-v2> tag. All that text is a valid QGIS style definition which we can put into a QML file.

Put that text into a new file with a header with a DOCTYPE and <qgis ... tag so the start looks like this:

<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'> <qgis simplifyDrawingHints="1" simplifyLocal="1" maxScale="0" simplifyMaxScale="1" styleCategories="AllStyleCategories" version="3.4.2-Madeira" simplifyDrawingTol="1" labelsEnabled="0" minScale="1e+8" readOnly="0" hasScaleBasedVisibilityFlag="0" simplifyAlgorithm="0"> <renderer-v2 type="singleSymbol" enableorderby="0" symbollevels="0" forceraster="0"> <symbols> <symbol name="0" type="marker" alpha="1" clip_to_extent="1"> [etc]

and it ends with </renderer-v2>. Save this file as rescue.qml.

Then in QGIS from the properties dialog of an existing layer you can load this QML file and use it to style it. Note it has to be compatible - you'll struggle to apply a polygon style to a point layer, for example.

I'm working with QGIS 3.4, and it saves .qgz files, which are zipped .qgs files. You can run your standard unzipping program on a .qgz to get a .qgs which you can edit to get the style info in the renderer-v2 tag.

If you have a lot of these to do then a short Python program could be written to extract all the renderer info from a QGS and save it in separate QML files - it would not surprise me if something like this didn't already exist or was part of a QGIS plugin...

2

I don't know how to extract styles but one thing you could try is "fixing" the links with the wrong data. The data just needs to match the original layers' geometry. Then save the styles as a QML file to import into new QGIS projects.

Also I found that setting up shortcuts to copy and paste styles is so hand, I have mine set to ctrl+shift+c and crtl+shift+v.

0

.qgs files are formatted in XML so you can open them in a text editor and copy them in another .qgs file.

Look for <map-layer-style name=""> markup.

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.