5

How do I conduct a temporal nearest neighbour analysis in QGIS?

Not sure if that's the right terminology, but basically, I have a dataset with ~100,000 house price transactions over a 5 year period. Over this period, there were new train stations being built. I know how to perform a basic nearest neighbour analysis, but I’d like to perform a temporal NNA to determine the nearest train station at the time of the transaction.

6

Taking inspiration from this answer here https://gis.stackexchange.com/a/335914/88814, you can create a virtual layer.

Unfortunately, Processing Toolbox / Vector analysis / Distance to nearest hub (line to hub) does not allow for any advanced settings, filters, conditions and so on.

Let's say you have two points layers called transactions and stations. They both contain a timestamp: the date the transaction took place as time_of_transaction, the time the train station opened as timestamp_stations.

Create a virtual layer and paste this query:

SELECT trs.fid as id_transaction, st.fid as id_station,
       ROUND(MIN(ST_Distance(trs.geometry, st.geometry)),2) AS distance
FROM transactions AS trs, stations AS st
WHERE st.timestamp_stations < trs.time_of_transaction
GROUP BY trs.fid
ORDER BY distance DESC

This gives you a list of the transactions with the nearest train station that was in existence at the time the transaction took place. The result of the query looks like this - it's a list of transaction_id with the matching nearest trains station id:

qgis virtual layer result

You can check it on the next screenshot: red dots are transactions (houses), blue one train stations. Compare the results:

Red dot 11 (at the bottom) has blue dot 18 (in the middle on the left) as nearest point - there are some blue dots closer (like no. 3), but they are newer than the transaction, thus they did not yet exists when the transaction took place.

Red dot 10 (in the middle) does not appear at all, since this transaction took place earlier than the date associated with all the blue (train station) dots: no train station existed at this point of time, so no nearest one.

Red dot 2 (in the middle) does not have blue dot 21 as nearest, but no. 11 that is farther away, but older: it's the one that existed already at the time of transaction.

qgis nearest neighbour temporal analysis

Using the new temporal controller (available from QGIS 3.14 - for older versions, use the timemanager plugin), you can generate an animated time series to check the results - see a screenshot that shows for the current timeframe which train stations existed at this point in time (time settings accumulate in the stations layer's temporal tab) and that brings to visibility the transactions during a time slot of 4 months (setting event duration to 4 month in transactions layer's temporal tab). You see clearly that the connections generated by the query really correspond to the nearest train station at the time being:

qgis temporal controller animation

3
  • Hi Babel, thank you very much! Wish I could upvote you but I don't have enough points. You're right, I do have two layers - 1) house transactions (events; with coordinates as well as transaction dates) and 2) train stations (also with coordinates and opening dates). Could you tell me a bit more about how to implement the query/ pseudocode please? Would that require PyQGIS? Thanks again.
    – Vanessa
    Nov 30 '20 at 16:28
  • 2
    How do you do the nearest neighbour analysis?
    – Babel
    Nov 30 '20 at 16:48
  • I used this function in QGIS: Processing Toolbox ‣ Vector analysis ‣ Distance to nearest hub (line to hub) (I followed this very basic tutorial: www.qgistutorials.com/en/docs/3/nearest_neighbor_analysis.html)
    – Vanessa
    Nov 30 '20 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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