I have two layers in my QGIS project: 1x shapefile with country borders, and 1x geojson which plots segment features.

Is there a way to only select/show those segments which are transboundary, i.e. which are crossing AT LEAST one border of the features (countries) in the shapefile?

To explain in words: I need to select only those electricity grids which are connecting two or more countries, not domestic ones.


Here is an example with:

  • Country polygon= Countries layer with id, country fields
  • Electric cable line = Lines layer with id, cable fields

Before start, please download and install refFunctions plugin.

(1) First step is extracting nodes of your cable lines.

Processing Toolbox | QGIS geoalgorithms | Vector geometry tools | Extract nodes

enter image description here

(2) It will return Nodes layer (green points at each node of line). Each point (node) is expressed as a row in the attribute table.

enter image description here

(3) Open the attribute table of this Nodes layer, and create a new field (let's say straddle) by expression below:

WHEN count_distinct(geomwithin('Countries', 'country'), group_by:= cable) >= 2
THEN 'Yes'

geomwithin() function is provided by refFunctions plugin.

enter image description here

This will distinguish lines which straddle multiple countries.

enter image description here

(4) Final task is to Join these tables.

Open Layer Properties | Joins and join Lines and Nodes using cable (cable name) as join-target field. Now you can filter your cable layer (straddle = 'Yes').

enter image description here

......there must be more sophisticated approaches.

  • Isn't a fomula like geomwithin('countries','id') is null a one step, direct answer to giacomo question ? Works perfectly to me ... – snaileater Nov 21 '17 at 6:42
  • This is really great, I am very thankful for your help and the time you took to show a step-by-step procedure! Thanks a lot! – Giacomo Nov 21 '17 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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