# How can I extract the parallel/perpendicular distance between two lines with QGIS?

I'm preparing a guide of my town for firemans and is very important to know the width of the streets but I don't know how to process this.

I have polygons that represents the block (islands) houses that simultaneously create the streeets.

• Do you actually want the distance between buildings? Or do you want the width of the pavement, ie the distance between curbs? – csk Sep 21 '16 at 14:12
• What have you tried? The workflow I see is - polygons to lines; explode lines at nodes; assign unique ids to the lines; create midpoint node for each line; nearest neighbour analysis of the midpoints => will yield a set of midpoints with a distance attribute to nearest midpoint, which will be the road width. – jbalk Oct 17 '16 at 1:19
• There is a plugin wich i belive that will halp you. Let me see where it is. – Diogo Caribé Oct 25 '16 at 1:34

It sounds like an interesting project. This script is partially based on another SE answer.

The QgsGeometry.distance() uses the GEOS function. The description specifies that it finds "the nearest points on the geometries" and measures the distance between those points.

buildings_pth = "F:\\path\\to\\buildings.shp"
buildings = QgsVectorLayer(buildings_pth, "buildings", "ogr")

buildingsList = [feat for feat in buildings.getFeatures()]
building_distances = {}
for building in buildingsList:
distanceMatrix = []
for bld in buildings.getFeatures():
if not building.id() == bld.id():
key = str(building.id()) + " to " + str(bld.id())
geom_plg = bld.geometry().asPolygon()
geom_plg_line = QgsGeometry.fromPolyline(geom_plg[0])
distances = [building.geometry().distance(geom_plg_line)]
distanceMatrix.append(distances[0])# print distances
building_distances[key] = min(distanceMatrix)


The minimum distances are stored in the python dictionary. The dictionary key mentions the feature IDs of the building polygons. The dictionary will contain double values, because it not only records the distance between A and B, but also between B and A. I hope this is useful.

• that looks like a promising and elegant approach. you might also want to choose the minimum non-zero distance, to avoid the case where buildings touch at the edge? – Steven Kay Nov 1 '16 at 20:04
• @StevenKay. That is a good point. This script assumes that none of the "buildings" are touching. Instead of comparing the polygon IDs one could use a geometry check to exclude any kind of overlap. However, a cul-de-sac between two touching buildings would not be included in the building_distances dictionary, even though there is a street. IMO it is preferable to have 0 values included, and visually inspect those situations afterwards. – Vincent_v_E Nov 2 '16 at 15:43