4

I would like to save my file as the .geopackage file. Unfortunately, I've noticed that the order of the layers is not correct. It doesn't correspond to the order in the "Layers" panel.

I saw, that the DB manager is required for this purpose:

https://www.reddit.com/r/QGIS/comments/l5r2pa/grouping_layers_inside_geopackage/

or some coding is required:

Reordering layers in GeoPackage and changing the default naming in QGIS?

enter image description here

but is there some other option, which would assure me, that the layers in .geopackage file are in the correct order? It applies also to the situation after saving. When I attended to load the .gpkg file, this order is also different. How can I solve this problem?

Geographos
  • 4,087
  • 2
  • 27
  • 88
  • I do not know if the Geopackage stores the layer order like your QGIS project file - https://gis.stackexchange.com/questions/361290/is-it-possible-to-lock-layer-order-in-layer-browser-panel-in-qgis . You may need to write code specific to the order you want. Are you using the Geopackage in other software that is opening layers by index number? – Cary H Mar 15 '22 at 12:56
  • 2
    There is no inherent order to tables in a geopackage (or other database) – Ian Turton Mar 15 '22 at 13:03
  • 1
    QGIS obviously sorts the tables by default in alphabetical order but you can order the layers as you wish and save the QGIS project. When you re-open the project the layers should appear in the same order. – user30184 Mar 15 '22 at 13:11
  • I've reopened my project but it doesn't work, unfortunately. – Geographos Mar 15 '22 at 13:43
  • @IanTurton what does it mean this inherent order? – Geographos Mar 15 '22 at 13:52
  • they can be in any order they like and it doesn't matter – Ian Turton Mar 15 '22 at 13:59
  • @IanTurton sure database don't care about table order but QGIS seem to consistently present them in alphabetical order – J.R Mar 15 '22 at 14:55
  • I bet that there is ORDER BY somewhere here https://github.com/qgis/QGIS/tree/master/src. No surprise that PostGIS layers are also presented in alphabetical order. – user30184 Mar 15 '22 at 16:33
  • I had a try with QGIS 3.24 and a few layers from a GeoPackage. The project file does preserve the layer order for me. – user30184 Mar 15 '22 at 16:38
  • I have just 3.12 and probably this is the problem since in 3.24 everything is alright. – Geographos Mar 15 '22 at 16:41
  • 3
    For clarification, the layer order is preserved in the layer list of the QGIS project (TOC). In datastore view they are in alphabetical order. That is IMHO alto the right and logical way to present the database layers. I am not sure which layer order is important for you. It feels odd if the order in the layer list is not preserved in the project file with QGIS 3.12 because it would affect also rendering - roads could be buried under land usage layers etc. – user30184 Mar 15 '22 at 17:04
  • 1
    I think you can store a project into a geopackage. It's under File->Save to->GeoPackage... https://ocw.un-ihe.org/mod/book/view.php?id=6859&chapterid=642 As some comments said: the order of QGIS-Layers is defined in the project. A GPKG-Layer is not the same as the QGIS-Layer. – Andreas Müller Mar 18 '22 at 18:18
  • Yes, I can, but this tutorial shows just the standard way of saving files as the geo package. No information about layer order at all. – Geographos Mar 21 '22 at 14:25
  • 1
    This is a hacky workaround, but what if you just add a numerical prefix (i.e., 1-, 2-, 3-, etc..) in front of all your layer names that reflects the order you want them saved in? Since they are being alphabetized on save, that should give you the result you want. – Kartograaf Mar 21 '22 at 22:01
  • @Kartograaf admittedly I think that is a perfect idea! I wish I could take it into account earlier! – Geographos Mar 22 '22 at 15:03

1 Answers1

4

Because (in earlier releases of QGIS) the layers in your project are being reordered alphabetically on save to geopackage, there is no built in way to preserve the layer order and override this alphabetization in this case.

However, one workaround is to simply add an alphabetical or numerical prefix at the front of your layer names that reflects the order you would like them to be saved in (i.e., A-, B-, C-, etc... or 1-, 2-, 3-, etc...) and let the new names dictate the way they will be reordered in the geopackage.

Kartograaf
  • 2,902
  • 7
  • 23