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) Is there a way to make all this tidier?
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.
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
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", ...)
sum() function would work just as well as
max() for this dataset.
Repeat for all fields. Delete the original fields.
Straightforward way: Name the columns in each shapefile the same, then merge them.
Clumbersome way: In your merged file, create new columns which you fill with the corresponding atrributes of the countries using if-functions.
Method 1: Try using a virtual and SQL layer and construct:
SELECT * from name_table1 UNION ALL SELECT * from name_table2 ...SELECT * from name_table12;
Method 2: Create a new point layer and copy 12 source layers into it. 1) Create a new point layer *.shp tools "layer" - "Create layer" - " Create shape-file..."Give it a name. 2) make your layer active, run "pencil" i.e. allow to edit it. 3) Now activate your first point the source layer and the tool "Select objects by area or click" select the objects, then make active your new layer and run the tool "edit"-"Insert objects" and so another 11 times :-)...Finish editing the new merged layer and see the result...