I was an intermediate user of MapInfo and have recently converted to QGIS. I would often use MapInfo to add a column in boundary file A saying which zone in boundary file B it is contained in. However, I cannot work out how to do this in QGIS.

Sorry, this is the error message I received when I tried to run the function.

Error Message

  • I've a nasty feeling that this is going to be one of those... "you can do this...if you install a PostGIS server" (but in the meantime, MapInfo SQL is actually quite good) questions. Commented Jan 28, 2016 at 11:13

2 Answers 2


To add a column containing some attribute of a feature of another layer, with which there exists a topological relation:

  1. Define a function: layer properties -> attributes -> field calculator -> function editor

  2. Input code which does the work

Eg is_contained_in() returns the value from column of feature from B which contains feature A

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def is_contained_in(layername, column, feature, parent):
    layer = QgsMapLayerRegistry.instance().mapLayersByName(layername)[0]
    for feat in layer.getFeatures():
        if feature.geometry().within(feat.geometry()):
            return feat[column]


  1. Run the script and go to tab Expression
  2. Create a new field, a virtual one if you like, and type in an expression like this:


  1. new field contained_in is added to the attribute table and hold the fid of a polygon of layer B

new field

Tested with this situation (labels - red: layer B/fid, black: layer A/contained_in):

test case

  • Thanks for the responses. I decided to go with the approach of creating a new function. However, it does not appear to work. I am trying to attached the field 'NAME' from the layer 'B'. However, I get the following error: Commented Feb 9, 2016 at 10:18
  • @James it seems that you haven't give a name for the new column. Please give a field name in the output field name text edit widget, and an appropriate field type. Then try again. If this doesn't help Post some details of your script please . Maybe there is an type error.
    – Detlev
    Commented Feb 9, 2016 at 10:57
  • this did not work for me. Qgis keeps complaining that the function is being invoked with the wrong number of arguments
    – fccoelho
    Commented Feb 10, 2017 at 17:17
  • I tested this on QGIS 2.18.15. It should work since calculations are done on the fly. But it was only possible to me to label this informations, retrieving attribute table was only showing NULL values.
    – Nono
    Commented Jan 10, 2018 at 15:34

You can use the Join attributes by location tool.

Say you have the following layers:

  • layer_a : the layer to which you want to attach attribute values from layer_b
  • layer_b : the layer from which you want to take attribute values

Go to Vector > Data Management Tools > Join Attributes by Location.

Then, set the parameters as below:

enter image description here

The tool will create a third layer (named layer_c here) which will be layer_a features combined to layer_b attributes. You eventually have to remove columns that you don't need and the job is done!

Feel free to test Attribute Summary and Output table options depending on what you want to achieve.

If you are intersecting a lines or polygons layer with a polygons layer, the program might give you 'bad' results for overlapping features (i.e. a line intersecting 2 polygons or a polygon intersecting 2 polygons). In that case, the program will select attributes from the polygon that is intersected by the biggest part of the line / polygon.

  • Attribiutes pulled in were all null...
    – fccoelho
    Commented Feb 10, 2017 at 17:26

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.