9

I am very new to working with GIS data. I am currently using the QGIS tool to work with the data.

I have two datasets:

  1. Buildings - polygon shapefile with information about different buildings in a city. There is a unique column called "propertyid".
  2. Roads - line shapefile with information about different roads in the city. There is no unique column, but we do know that each line item refers to one road, so we have created an index column based on "@row_no".

Objective: To find the distance between a given building and the nearest road.

What I have already tried:

  1. Converted buildings layer to points(centroids) and lines layer to points(extracted vertices) and run a distance matrix using these two layers. But this doesn't give me the desired result for some reason. Since I am very new, I could be doing something wrong.
  2. Using the same point layers mentioned above, ran the distance to the nearest hub algorithm. Again, I may have committed an error in doing this as well.

It would be great if someone could guide me in the right direction and tell me if

  • what I'm trying to achieve is possible, and
  • if it is, then how would you go about doing it.
1
  • 1
    Could you elaborate on the attempts you made? What results are you getting, and why exactly are they undesirable? If you're not getting results, but instead the tools are failing, can you include the error messages that result?
    – jcarlson
    Commented Jun 1, 2019 at 18:55

4 Answers 4

8

While in general this would be better handled in PostGIS, there are plug-ins which do the heavy lifting for you:

NNJoin

This is the preferable and correct way, however could be slow on large datasets. It has the non trivial advantage to compute the correct polygon-to-line distance, without converting buildings into points first (which could lead to erroneous results).

Hub Distance

Faster, but only works with point vector layers.

Since you are working with a local, projected system, make sure to check the Layer Units option.

An output layer will be created, with lines connecting each building with the closest road. Alternatively you can choose to have a point layer as output (one point per building)

2
  • Thanks for this reply. I am trying to download PostGIS. Is there any direct link for Windows 10? I saw their website and was quite confused as to which method to follow. Could you please direct me to the correct link? Thanks in advance. Commented Jun 2, 2019 at 9:59
  • 3
    Did you try with the two options outlined in the answer? Neither of those requires PostGIS. ;) Commented Jun 2, 2019 at 12:43
6

For qgis 3.8 and later you can use "join by nearest" from the processing toolbox.

3

I can suggest using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer...

Let's assume we have 10 features in 'buildings' and 16 in 'roads' accordingly, see image below.

example

With the following query, it is possible to find out the distance between a given building e.g. WHERE b.propertyid = '1' and the nearest road. Mind the CRS.

SELECT ST_ShortestLine(ST_Centroid(b.geometry),r.geometry),
       b.propertyid,
       r."@row_nr",
       ROUND(ST_Length(ST_ShortestLine(ST_Centroid(b.geometry),r.geometry)),2) AS length
FROM roads AS r, buildings AS b
WHERE b.propertyid = '1'
GROUP BY b.propertyid
ORDER BY MIN(ST_Length(ST_ShortestLine(ST_Centroid(b.geometry),r.geometry)))

The output Virtual Layer will generate the shortest line between indicated "propertyid" and "@row_no" with a length between them. The calculation was done by the usage of a building centroid.

result1


Alternatively, you may use the above query without ST_Centroid() statement.

SELECT ST_ShortestLine(b.geometry,r.geometry),
       b.propertyid,
       r."@row_nr",
       ROUND(ST_Length(ST_ShortestLine(b.geometry,r.geometry)),2) AS length
FROM roads AS r, buildings AS b
WHERE b.propertyid = '1'
GROUP BY b.propertyid
ORDER BY MIN(ST_Length(ST_ShortestLine(b.geometry,r.geometry)))

The output Virtual Layer will generate the shortest line between indicated "propertyid" and "@row_no" with a length between them. The calculation was done by the usage of the closest object vertex.

result2


References:

1

An alternative approach would be to convert the building polygons to lines, or nodes. Then multi buffer the roads, probably in increments of 1 metre. Spatial join the two files and export the attribute table to Excel. That way you could select the closest wall of a building. Bit of a clunky method, but is one way of handling large buildings.

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.