I have a shapefile of lines and another of polygons. When two or more lines intersect a polygon they should be connected (snapped), but sometimes during manual digitization this doesn't occur. I need to find and correct all these errors. I used v.clean from GRASS GIS and it automatically snapped a lot of vertices, which was good, but some of those disconnected lines are further from the tolerance I gave and if I increase the tolerance too much it begins to generate other types of errors.

enter image description here

Any ideas?

  • 1
    Spatially join the polyline endpoints to the polygons and summarize the counts by polygon. For each polygon containing more than one joined endpoint, create a polyline consisting of segments radiating from the centroid of those endpoints out to the endpoints themselves (a "star"). Merge the stars with the original polylines. Clean the result to remove any self-intersections that arise. Each of these steps is supported (and can be automated) by fully capable GISes. Because I am not familiar with the details of QGIS or GRASS, I offer this solution only as a comment.
    – whuber
    Jan 5, 2012 at 16:15
  • but sometimes all the lines end outside the polygon... Jan 5, 2012 at 16:46
  • 1
    What do these lines represent? If they are rivers are they encoded with extra information such as name or Stream Order? Is the network directed (such as a river network) as say two first order streams must flow into a second order stream. I guess I'm trying to say you may want to put some intelligence behind the snapping process to overcome the issue with dodgy snapping with increasing search tolerance.
    – Hornbydd
    Jan 5, 2012 at 16:51
  • 1
    Your comment seems to contradict the question. The question seems to say you want to snap vertices that fall within a common polygon. If a vertex falls outside a polygon, you don't give any criteria for how it should be treated! So, what exactly is the relationship between the polygons and the polylines?
    – whuber
    Jan 5, 2012 at 17:24
  • 1
    I agree with whuber; perhaps you should clarify the question. Sounds like you want lines that are 'in' the polygon, as well as 'close' but excluding ones that are 'in' but are properly joined? Jan 5, 2012 at 19:45

4 Answers 4


My understanding of the problem is as follows:

If a polyline endpoint intersects a polygon then the polyline needs to be connected (by adding or adjusting vertices) to all additional polyline endpoints that intersect the same polygon.

Some polyline endpoints don't intersect a polygon, being undershoots, but these should be connected as above.

This answer builds on some already given, but isn't complete as I'm not aware of all the QGIS commands - perhaps someone else can add to this?

  1. For each polyline, extract the endpoint coordinates (Vector > Geometry Tools > Extract nodes then tidy up) and calculate whether or not they are within a polygon (Vector > Research Tools > Select by Location).

  2. For polyline endpoints that are within a polygon, extend that end of the polyline to the centre of the polygon (Extract the polygon centre using Vector > Geometry Tools > Polygon centroid, not sure about the line modification, though you might be able to work with the polyline vertices as points and add additional ones with appropriate numbering before reconstructing the modified polyline using the Points to Paths plugin)

    You mention that there may be situations where some or all of the lines have been correctly connected, in these situations (identifiable by coincident polyline end points) you might choose to use the coincident end point location instead of the polygon centroid as the point to extend any additional unconnected polylines to for that polygon.

  3. For polyline endpoints falling outside a polygon, extend that end of the polyline to the centre of the nearest polygon. Again, I'm not sure about calculating the nearest polygon, though the GRASS v.distance module looks to do the trick .

  • thats a good rationale for the problem solution. I also got to something like that with the answer from alexgleith. But i was stuck in the operation to " extend that end of the polyline to the centre of the nearest polygon". The question now is how do I do that? If I use v.clean.snap to the centroids of the polygons it will still move any vertices which are not endpoints... Jan 6, 2012 at 14:30
  • One approach would be to convert the polylines to points as described and if necessary add an additional point at the start or end of a given line as dictated by the location of the existing start or end point. The location of the point added would be the centroid of the polygon, and the point would be attributed with a vertex sequence number of -1 or 1000000 to place it at the start or end of the polyline. This would then allow the polylines to be reconstructed using the Points to Path tool Jan 6, 2012 at 14:43

I think that I could do it with some custom algorithm... Note sure how you would like to implement it, but I think it would work.

For each line, for each end point, do a point in polygon for the vertex at the endpoint and link it to the polygon.

Then, for each polygon, if there is more than one line's end point linked, I'd merge (move) them all to the average coordinate, or to the centroid of the polygon perhaps.

That is pretty simple. But not available in standard operators. I think it could be done in ArcGIS with a days coding time.

Another note, in the image you posted, one of the lines is outside the polygon... You could handle this in a simpler way by simply finding the closest vertex to each end vertex, ensuring that the closeness is mutual (I think that is necessary, so that you have vertices snapping only in one way), then you can move them to the average position of the vertices.


Interesting problem! I can't think of a way to automatically perform the snapping, but here's a step in identifying areas to at least look at for manual correction:

  • Add a new column to your line layer, and set it equal to '1' for all features.
  • Under Vector > Join Attributes by Location, set your Target layer to your polygon layer, and the line layer as the join layer. Then select "Take Summary of Intersecting Features" and click "Sum." Give a location for the output file, and "OK."
  • The resulting polygon file will be your original polygon layer, but with some additional fields. The SUM field is what you want; when it's 2, it means that the then you can zoom to it and manually snap the features.

Depending on the situation (number of features, number that are in this situation vs. those that aren't, etc.) this may save you some time.

EDIT: You may also want to use the polygons with a sum of 2 to create a sub-set of your line layers, then run the cleanup on those with a larger tolerance so as not to change the other features.

  • I had done something like that already. I even used v.net to retrieve the nodes for the lines and made a spatial selection of the polygons matching the nodes. I find a lot of errors to correct using this, but i still have 3 big problems: 1) there are thousands of errors to correct manually, which takes a loong time; 2) I don't get all the errors cause in some cases the lines or the nodes don't even 'touch' the polygons; 3) i get errors that are not errors, cause when two lines connect properly they are also selected. Jan 5, 2012 at 17:16

once you have the endpoints, I would do a spider diagram taking polygons centroids as facilities and endpoints of nearby lines as targets. You perform the spider diagram analysis and as a result you would have a segment for each polyline.

This would work even for those ending outside the polygons. How to do that in QGIS I don't know, sorry, but I would do this in ESRI's products that I know well. That's what I understood when whuber, in the first reply, suggested to build a "star".

I'm pretty sure there are people here who know how to perform such analysis in QGIS

  • 4
    Welcome to GIS Stack Exchange! I noticed that your answer has attracted some downvotes and suspect that is because you have suggested an alternative to the technology the asker is using (GRASS/QGIS). My recommendation would be to float such suggestions as comments rather than answers and if the asker seems interested then transfer the information to an answer.
    – PolyGeo
    Jun 15, 2013 at 23:47

Your Answer

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

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