I have a dataset of roads in a city stored in a PostGIS enabled database. I would like to identify and remove any feature which is not connected to the rest of the road network. This behavior is common around the edges of networks, where the data was clipped.

enter image description here

I have found other answers on the GIS SE that do this, but they all appear to use ArcGIS, and I would like to solve the problem using PostGIS, QGIS, or a Python script.

My datasets can include over a million features, so performance is a concern.

4 Answers 4


You can easily use PostGIS to select roads that don't intersect any other road:

SELECT id, geom FROM roads a
     (SELECT 1 FROM roads b 
      WHERE a.id != b.id
      AND   ST_Intersects(a.geom, b.geom))

You can use this QGIS Python script to detect lines that are not connected to anything:

from qgis.utils import iface

layer = iface.mapCanvas().currentLayer() # Selected layer

featureList = list( layer.getFeatures() ) # Gets all features
allFeatures = { feature.id(): feature for feature in featureList }

# Spatial indexing
spatialIdx = QgsSpatialIndex()
map( spatialIdx.insertFeature, featureList )

resList = [] # ids of features not connected to anything

for f in featureList:

    # List of potentially connected features from spatial indexing
    ids = spatialIdx.intersects( f.geometry().boundingBox() )

    hasNeighbor = False

    for id in ids:
        ifeature = allFeatures[id]

        if ifeature.id() == f.id():

        # Checks if f is really intersecting with ifeature
        if f.geometry().intersects(ifeature.geometry()):
            hasNeighbor = True
            break # Exit current for loop

    if (not hasNeighbor) and (not f.id() in resList):
        resList.append( f.id() )

print resList

Note that this won't work on multipart lines. I don't think it could be made much faster...

  • I am having the same problem as the question in this topic. Thanks for your solution, I enjoyed it. I got error "'continue' not properly in loop". I use qgis3.4 and python3.7, because I am a beginner with python, I hope you can help me. Thanks very much..
    – User0AB
    Commented Apr 5, 2020 at 8:10

I had a similar problem, so I developed a QGIS plugin which is now available in the official QGIS plugins repository and can be found in the QGIS menu: Plugins / Manage and Install Plugins... and search for Disconnected Islands.


This plugin runs on a polyline layer, building up a road (or rail, etc.) network graph of connected links. It then analyses connected subgraphs, ones that are connected to each other, but not connected to isolated or floating links. It creates an additional attribute containing the group ID of the subgraph. This can then be used to style the layer with Categorised styles, or Zoom to selection. The disconnected links can then be fixed or removed.

Source code can be forked from: https://github.com/AfriGIS-South-Africa/disconnected-islands

My dataset has about 2 million links and it was analysed in less than 15 minutes, using 55 GB of RAM.

  • This sounds perfect but just so you know when I dl it I get a broken plugin error :(
    – wmebane
    Commented Apr 6, 2016 at 18:04
  • Thanks for the feedback @wmebane. I'd love to fix the broken plugin issue for you. Please will you report the error message at hub.qgis.org/projects/disconnected-islands/issues/new or via email (click on Author inside the plugin manager).
    – PeterS
    Commented Apr 8, 2016 at 12:14

You can strip out the easy roads with @dbaston's method first, then use pgRouting to find the more complicated cases, such as when you have a network of roads that is not connected to some other network.

Choose a road segment which is definitively within the main network, then try to find a route from each other segment to that one. If no route exists, delete the segment.

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.