# Calculating distances between buildings, plots and road network

I have three layers, see image below:

1. A polygon layer containing building outlines (Purple)
2. A polygon layer containing plot outlines (Yellow)
3. A polyline layer showing the road network ( Red)

What I want to do is calculate whether there is room for a driveway on the plot.

Therefore I want to calculate the distance between every building and the plot edge (plot polygon containing all or part of a building) that is closest to the road network.

• Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works Jan 5, 2021 at 14:09
• If you had the roads as polygons, you simply could run `join by nearest` with the buildings and the roads. But since your data quality varies this significantly, you'll have to break this down into several tasks. First step should be to try to get better data on the roads.
– Erik
Jan 5, 2021 at 14:16
• Maybe this helps: gis.stackexchange.com/a/383057/88814 Jan 5, 2021 at 14:20
• Thanks this is really useful.
– SVB
Jan 5, 2021 at 14:27
• Sorry but I am new to this so most probably something I am doing wrong. When I try to replicate that i get the following error message: " Parser Errors: Function is not known syntax error, unexpected ')', expecting \$end"
– SVB
Jan 5, 2021 at 15:10

If you just want to calculate the distance from each buidling the the outline of the plots, go to the bottom to see how to calculate a new attribute field for that. Here, I first give some more detailed information and also a visualization of the line you're looking for.

Let's supposte you have three layers named: `buildings` (polygon), `plots` (polygon) and `street` (line), as in the screenshot. If you use other names, you must change the expression below accordingly.

You can now generate the red arrows as lines connecting the buildings with the boundary of the plots, using the shortest line (small dotted line in black) that connects each building with the street and than cutting this dotted line where it intersects the boundary of the plots. The part of the line that remains is connecting line you're looking for (the red double arrows in my screenshot). You can measure it's length to get the distance from the building to the plot outline:

The expression to generate this line is below. To paste it, add an additional symbol layer on the buildings layer (click the green + symbol in the Layer styling panel on the upper right side, see screenshot), set it to `Geometry generator / Geometry type: line` and paste the following expression. Like this, you create lines for visualization purpose only: the red arrows in the screenshot.

To be able to measure their length, you must create actual geometries. For that, you can use the very same expression with `Menu Processing / Toolbox / Geometry by expression`. Select again the buildings layer as `Input layer` and set `Output geometry type` to line. Than paste again the same expression as `Geometry expression`:

``````shortest_line (
\$geometry,
intersection (
shortest_line (
collect_geometries (
overlay_nearest (
'street',
\$geometry
)
),
\$geometry
),
boundary (
aggregate(
'plots',
'collect',
\$geometry
)
)
)
)
``````

On the screenshot above, I used the geometries created with `geometry by expression` to set a label with that calculates the distance, introducing this expression: `format_number (length(\$geometry),2) || ' m'` for the definition of the label text. You could also create the length as a new attribute, using field calculator: `length(\$geometry)`.

You could also skip the creation of the geometry for the line you're interested in (be it for visualization only or as real geometries to get the length) and directly create a new attribute on the building layer for the length only. Click field calculator on the buildings layer to create a new field with the expression `length (expression)`, where instead of `expression`, you should paste the whole expression from above. This returns a new field with the distance you are looking for, see screenshot for how to do it:

• Thank you so much. This answer works perfectly
– SVB
Jan 6, 2021 at 13:18
• You're welcome. As a new user, please be adviced that this side tries to avoid "thank you" messages. Instead, it encourages you to accept the answer: click the checkmark right to the answer to accept it, see: meta.stackexchange.com/a/5235 and gis.stackexchange.com/help/someone-answers Jan 6, 2021 at 13:21