enter image description here

I would like to style polyline segments differently whether they are inside, on the boundary or outside a polygon. So based on the image above the red and green line segments inside the boundary would be styled differently

Some preliminary searches have shown that rules based rendering might be suitable but i'm not sure if I can use rules based on spatial queries like select by location.

My long way round would be to copy the layer, select by location, apply some filters and then style selected segments how I want them.

What I'm looking for is a way to automatically style those segments based on location so any line that goes inside that boundary get's styled in a standardized way. Currently using 2.14.2 Essen

  • Can you add a sample image to clarify your question?
    – ahmadhanb
    Aug 3, 2016 at 1:02
  • But you need to style only the part of the line the goes inside the boundary? Or all the line (even if the line is half inside and half outside)?
    – Sergio
    Aug 3, 2016 at 7:20
  • @Sergio To start with all the line if it goes inside the boundary, but i would be interested if there were a way to only style the part inside.
    – hselbie
    Aug 3, 2016 at 14:33

1 Answer 1


My suggestion is to use postgresql+postgis and create a view of your layers with this code:

CREATE OR REPLACE VIEW intersecting_lines AS 
 SELECT line.*
 FROM line_layer as line, polygon_layer as polygon
 WHERE st_intersects(line.geom, polygon.geom);

With this code you obtain a new view that you can load in qgis over your line layer using a different style. If you change the line layer the view is automaticaly updated. You still see your original line layer under the view in qgis.

I suggest this solution because I think it's the fastest way.

If you want to create a view with the line intersecting the polygon use this code.

CREATE OR REPLACE VIEW intersecting_lines AS 
 SELECT row_number() OVER (ORDER BY line.gid) AS gid, st_intersecting(line.geom, polygon.geom) as geom
 FROM line_layer as line, polygon_layer as polygon
 WHERE st_intersects(line.geom, polygon.geom);

This is heavy for your pc. And as you can see rendering of the layer will be very slow.

Using only Qgis you can do something really similar (but less performing) with Virtual Layers

enter image description here As you see the sql code is the same as the view of postgis but refers to shapefiles in this case.

enter image description here And this is the result.

Another possible way, is writing some phyton code, but I can't help you in this case.

Just remember that in any case (postgresql, qgis, phyton) you are searching for a spatialquery result or worst a spatial intersection. This are quite slow operation in any case, especially with big layers.

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.