3

I have 7 shapefiles over an agricultural area. All of them have the same attributes (province code, polygon code, crop, etc.). I want to combine them to have only 1 shapefile. I have used the "union" tool of QGIS. The result looks good but the attribute table of the output is not what I want.

This tool copy and paste each input attribute table so in the output table there area a lot of duplicates. Is there a way to have only one time the colums and then all the values?

As you can see in the image, attributes like "ID_RECINTO" are duplicated.

enter image description here

2

2 Answers 2

2

If you want to remove all duplicated fields (assuming the field names end with either an underscore or an underscore followed by an integer), you could use the following in the Python Console:

import re

layer = iface.activeLayer()
field_names = [field.name() for field in layer.fields()]
for index, name in reversed(list(enumerate(field_names))):
    if name.endswith('_') or re.search('_'+'[0-9]', name):
        with edit(layer):
            layer.deleteAttribute(index)
1

From the image, the field ID_RECINTO does not have a duplicate. I only see one field with that name.

But:

If id_recin_1 is a similar field then the problem is simply that the two field have different names: id_recin_1 != ID_RECINTO

A union will only concatenate fields/row with identical names. QGIS cannot reason that id_recin_1 and ID_RECINTO contain the same data. At the moment, only humans and some learning algorithms can do that.

To get the clean join you want, you need to open each layer and edit its fields so the similar fields all share identical names (I cannot remember off-hand if QGIS or shapefiles are cap sensitive or not). Also, shapefile field names have character limit of 10 characters, anything more will be truncated and sometimes mangled. Make sure your data schema accounts for that.

1
  • That's not what QGIS is doing. I have two shapefiles with exactly the same field names. The result of the union duplicated all the fields, adding a "_1" or "_2" to one of each pair. Then the values that should be in the same field are assigned to different fields, rendering the final shapefile useless.
    – Rodrigo
    Commented Apr 17, 2020 at 19:23

Your Answer

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

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