13

I have a QGIS (2.14.3-Essen) project that I use both at work and at home. I keep all but two layers with the project.

The two remaining layers are very large - a DEM and hillshade totaling more than 20 GB - and they are stored in different paths at work vs. home.

I would like to ignore the Handle Bad Layers dialogue and keep the broken paths for those layers. I often don't care they are bad; I want to make changes to other parts of the project.

Is there a way to do so, to ignore or skip the Handle Bad Layers dialogue so I don't lose the reference to the layers if I don't update them?

5
  • This is something that should be solved upstream in QGIS. It wouldn't be very hard to do. I would recommend to get this fixed upstream (e.g. additional options in the dialog: a) hide in this session b) fix source path c) remove layer). There are a lot of companies around that will be happy to do this for you. Mar 22, 2017 at 14:51
  • 1
    @Matthias Kuhn - That is a very good idea, that would be a good way forward. I do use one of the consultancies on that list (not sure if it's the done thing to name them?). I shall explore the possibility with them. Thanks Mar 23, 2017 at 19:41
  • @MartinHügi did you make any progress on this? Something similar raised some acknowledgement on the mailing list lists.osgeo.org/pipermail/qgis-developer/2017-May/048450.html May 12, 2017 at 14:57
  • No I haven't progressed this, I had forgotten actually as it has just become one of those things we live with. Thanks for the reminder, I can ask the question to our consultancy or is it something that is already underway? @Aaron - How has this worked out for you? May 12, 2017 at 15:54
  • I continue to use my work-around. Two parallel QGIS projects - one for work and one for home.
    – Aaron
    May 13, 2017 at 22:50

3 Answers 3

5

A possible work around would be to modify your qgs project file using a script. I found that if you replace the datasource with <datasource>.</datsource> then it will ignore the Handle Bad Layers pop up.

Python script to run if you don't want to load certain layers.

import fileinput

replaceNextLine = False

for line in fileinput.input('path\to\your\project.qgs', inplace=True):
    if replaceNextLine:
        print '<datasource>.</datasource>'
        replaceNextLine = False
    else:
        print line,

    if '<id>layer_name_whos_datasource_needs_to_change' in line:
        replaceNextLine = True

Python script to run to insert the appropriate datasource path.

import fileinput

replaceNextLine = False

for line in fileinput.input('path\to\your\project.qgs', inplace=True):
    if replaceNextLine:
        print '<datasource>.\path\to\your\datasource</datasource>'
        replaceNextLine = False
    else:
        print line,

    if '<id>layer_name_whos_datasource_needs_to_change' in line:
        replaceNextLine = True

Doing this you could set up a DoNotLoad.py with the top script and a home.py with paths to your home sources and a work.py with paths to your work sources using the second script as a pattern.

You will then have a single project to work on but you can easily change your paths depending on the environment you are in, or change the paths so that the layers are not loaded, but are still in your project (all of your styling is saved and I think you can even modify the styling if you want. Just nothing will show up in the map).

3
  • Thanks. I get the concept, I quite clearly now need to spend that time I have been meaning to get around to start understanding basic use of Python. For now I get the concept because I manually edited the project.qgs with WordPad to change the datasource, and changed it back. QGIS didn't ignore the handle bad layers but as the <ID> acts a placeholder I was able to go back and revert my change back. So yes, I get that - I think. Mar 22, 2017 at 23:24
  • I guess this is a good place to start wiki.python.org/moin/BeginnersGuide Mar 22, 2017 at 23:33
  • @MartinHügi if it helps, I got the main pattern for my script from this stackoverflow answer stackoverflow.com/a/290494/4708150 This explains the logic of the script more thoroughly. Make sure to look at the comments. There is a top rated comment that explains why there is a comma at the end of the print statement.
    – TJR
    Mar 23, 2017 at 13:11
5
+50

Just make separate copies from your project file for home and work use.

Data you save will be written to the layer source files, not to the project file.

Once you have finished the homework part, you can take it to work and re-add the bad layers. If you have a difficult styling on layers, you might save it separately and add it too.


Another choice is to wrap a VRT file around the external source.

A sample example for a Geotif raster file, created with gdal_translate:

<VRTDataset rasterXSize="1656" rasterYSize="846">
  <GeoTransform> 2.0005201224994706e+005, 6.2653333397239589e+002, 0.0000000000000000e+000, 6.9906275735481549e+006, 0.0000000000000000e+000,-6.2653333397239589e+002</GeoTransform>
  <Metadata>
    <MDI key="TIFFTAG_RESOLUTIONUNIT">2 (pixels/inch)</MDI>
    <MDI key="TIFFTAG_XRESOLUTION">120</MDI>
    <MDI key="TIFFTAG_YRESOLUTION">120</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1">
    <ColorInterp>Red</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">testtif.tif</SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="1656" RasterYSize="846" DataType="Byte" BlockXSize="1656" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="1656" ySize="846" />
      <DstRect xOff="0" yOff="0" xSize="1656" ySize="846" />
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>

and a sample for a shapefile:

<OGRVRTDataSource>
    <OGRVRTLayer name="testshp">
        <SrcDataSource>F:\Karten\vrt\testshp.shp</SrcDataSource>
            <SrcLayer>testshp</SrcLayer>
            <GeometryType>wkbPolygon</GeometryType>
            <LayerSRS>EPSG:31466</LayerSRS>
    </OGRVRTLayer>
</OGRVRTDataSource>

When the source files are not present, the raster is displayed as a black square (you might turn the layer to invisible), and the vector layer becomes a non-geometry table, without any error messages. Additional non-vrt layers with source files that are not removed will stay and can be edited. If you reinstall the source files, everything is back to normal, even the changes to the other layers.

10
  • 1
    Making separate projects for home and office is a decent work-around in this particular case. However, there are other cases, such as when a map service is down temporarily, when you would want to ignore the bad layers notification. So again I ask, is there a way to ignore or skip the Handle Bad Layers dialogue so one doesn't lose reference certain layers?
    – Aaron
    Jun 28, 2016 at 21:50
  • Can you provide some more details on VRT files, what they are, and how to get started. From what I have looked into do VRTs only apply to rasters? What about vector files, or remote PostGIS layers that are not available if there is no connection. How can you ignore these 'Handle Bad Layers'? Mar 21, 2017 at 23:16
  • @MartinHügi you can also build VRT for vector files, but that still needs handwork: gdal.org/drv_vrt.html and paolocorti.net/2012/03/08/gdal_virtual_formats. I will expand my answer soon for some examples.
    – AndreJ
    Mar 22, 2017 at 6:29
  • @AndreJ Thanks, so looking through those links, my limited understanding is that an xml file sort of acts as an inbetween map to the source of the data. So QGIS 'thinks' it is there, even if the datasource is not available? GDAL is something that it looks like will be good to get to grips with, or at least have an understanding of. Mar 23, 2017 at 9:21
  • Yes, you are just fooling the QGIS error handler.
    – AndreJ
    Mar 23, 2017 at 9:42
1

The ChangeDataSource plugin keeps the layers in the file by moving them temporarily to a handle bad layers group. https://geogear.wordpress.com/2016/01/29/changedatasourceplugin-plugin-release-2-0/ It should do what you want. Last year I was having a similar issue and noticed that there was the ChangeDataSource plugin that worked with layers but did not handle bad layers. I mentioned that handling bad layers would be a great feature in the comments on the developer's (Enrico Ferreguti) blog. Within a week he had added it!

4
  • This plugin didn't work for me to handle bad layers. I tried on two different laptops - one running Win7 and the other Win10. Both have QGIS 2.18.0 installed. I checked "Handle bad layers" and re-started QGIS after installing. QGIS default of handling bad layers continued to pop up, not the plugin's notification it found and handled invalid datasources.
    – Aaron
    Jun 6, 2017 at 0:44
  • Not working for me right now either. I'll let the developer know.
    – Baswein
    Jun 7, 2017 at 13:40
  • I verified that changeDataSource plugin runs without issues. I fix some uncaught conditions throwing exceptions or reporting false error and commit to repository a new version. [If your issues persist please report ](github.com/enricofer/changeDataSource/issues) specifying datasets or the operating steps to reproduce them. Jun 19, 2017 at 8:09
  • In order to get the plugin to pop up you need to have Qgis running and then open the project.
    – Baswein
    Dec 4, 2017 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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