[GIS] Using field calculator in QGIS to return name of country from different shapefile

field-calculatorqgis

I have a shapefile that consists of a list of archaeological sites with the x and y coordinates.

I have an Natural Earth shapefile for the countries of world.

I was wondering how I go about creating a new field for the country of each archaeological site using the field calculator?

I am relatively new to QGIS and not that familiar with the field calculator.

Best Answer

Two options.

1.) Perform an intersect of the points with the countries. Use Vector > Geoprocessing Tools > Intersect...

For the intersect tool, make the input your arch sites (points) and your intersect layer as the countries (polygons).

That will produce a point file with the attributes from the countries attached to each point.

2.) If you know that the points do not overlap more than one polygon, then it's also possible to use the field calculator to do that quickly.

Install the refFunctions plugin for QGIS.

You can calculate a new field quickly in QGIS 2.8.1 by highlighting your arch points layer in the layer list, then pressing the Field Calculator button. Or do it from the attribute table.

field calculator button

Then use the new geomintersects function in the Reference list:
geomintersects('target layer','target field')

Where target layer is the name of the countries layer, and target field is the field of the named countries to attach to the points.

calculate intersects field

There are some other functions like geomwithin that may work in this case too.