[GIS] Merging layers into one- QGIS

mergeqgis

I have numerous population layers for the Baltic Sea. In the attribute table of each layer, I have population densities, name of towns etc, all the same geometry (points).
I'd like to merge the layers together so that I could have all the population densities of the Baltic inside one layer. I tried to used the "merge vector layers" tool, but when I do so, the information gets all mixed up in the output attribute table. For example, the first 11 columns are linked to Germany (picture 1), but then, the next columns show only zeros and no data (picture 2)
enter image description here
Is there a way to make all this tidier?
enter image description here

Best Answer

The issue is that the fields have different names in each layer, so QGIS creates a separate set of fields for each layer.

Slow method: rename the fields in the original layers

You could fix the issue by renaming the fields in the original layers (eg, change "SwedenPo_1" and "GermanyP_1" to "Field_1" or "CountryName"). To do this, open the layer properties, go to source fields, turn on layer editing, double-click on the name of each field and change it. This is pretty slow, because you have to repeat for each field in every layer.

enter image description here


Fast method: change the names in the combined layer

A much faster method is to change the names in the combined layer, using the Field Calculator. The individual steps are a bit longer, but you only have to do it once for each field.

  • For text columns, use the concat() and replace() functions. This will concatenate all the field values together into one string, then remove any zeros.

     replace( concat("SwedenPo_2", "GermanyPo_2", ...),'0','')
    
  • For numerical columns, use the max() function. This will choose the largest value from the list of values, so if only one field has a value greater than 0 it will choose that one.

    max("SwedenPo_4", "GermanyPo_4", ...)
    

Alternatively, the sum() function would work just as well as max() for this dataset.

Repeat for all fields. Delete the original fields.

Related Question