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?
-
Some more information on your current data would be helpful, e.g. how many layers, if they overlap, etc.– ErikCommented Jan 8, 2019 at 9:30
-
I have 12 layers, same coordinate system, and they do not overlap. Each country has population points representing towns, villages and cities. Those points contain population densities, employment rates etc. My aim is to merge them all so that I can visually represent the differences between countries, in terms of densities, employment rate etc.– Tim56Commented Jan 8, 2019 at 9:34
3 Answers
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
concat()
andreplace()
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.
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...
-
Thanks for your input. However I am unsure of how to conduct method 2. Could you give me additional info?– Tim56Commented Jan 8, 2019 at 10:43
-
The process seems to be working, but I have only one field that is being copied in the attribute table (population densities field). My remaining 7 fields do not appear in the new shapefile. These fields (pop densities, employment rates etc) are joined with an excel spreadsheet for each country (i.e each country has it's own spreadsheet). Could that represent an issue?– Tim56Commented Jan 8, 2019 at 13:28
-
1in the new layer, create a structure corresponding to your source data in the window using "add field to list"... Commented Jan 8, 2019 at 13:54
-
1the structure of your 12 tables must be identical, otherwise you will lose the fields ... Commented Jan 8, 2019 at 14:01