# Calculating minimum distance between all points of 2 different point layers using QGIS? [closed]

I use QGIS 2.8.3 or 3.4 whatever works better.

I need to calculate the mimumum distance between all points of 2 different point layers. I was reading this post https://gis.stackexchange.com/a/62212/147194 and used the distance matrix tool accordingly. Now I am unsure what the output layer contains. The point input layer had 488 feautres and the target point layer had 605 features.

But why does the output layer contain of 3801 ?

The last column of the output layer probably gives the distance but in which measure unit?

I need to 2 this process for 2 other point layers and want to be sure that I am on the right track before I continue.

Is 'Distance Matrix' the easiest way to calculate the minimum distance between all points of 2 different point layers?

And what does the number at 'use only nearest (k) target point' stand for?

Following the instructions (see link above) I put '1' but without knowing why?

• Are you expecting that the output will have 488 * 605 = 295,240 records? – Stu Smith Aug 8 '19 at 3:00
• Remember that your two input layers MUST have the same Coordinate Reference System, and the chosen CRS must be in feet or meters, NOT degrees. If necessary, project one or both to the same CRS. Accept no substitute! – Stu Smith Aug 8 '19 at 3:07
• I had no idea what the outcome would look like but I was just surprised about the big number of outcome features. The CRS for both Input layers is: EPSG:4326 - WGS 84. In which scale measure will this CRS give me the distance and if not suitable which alternative CRS might work? My project is in British National Grid EPSG 2770 on the fly, but the layers wont be displayed with this therefore I chose EPSG:4326 - WGS 8.4 – Tina Aug 8 '19 at 10:46
• As per the Tour there should be only one question asked per question. – PolyGeo Aug 8 '19 at 20:06

Welcome to Stack Exchange GIS, Tina! Without seeing a screenshot of your situation, I think you need to use the NNJoin tool. It can be activated by going to Pugins > Manage and Install Plugins > and installing NNJoin. Once installed, it can be accessed by selecting Vector > NNJoin.

NNJoin calculates the distance from each feature in your first point layer to its nearest feature in the second point layer. This sounds like what you want.

Note that the NNJoin dialog will accept an input layer and a join layer. These will be your two point layers.

NNJoin calculates distances between any two geometry types: points, lines or polygons.

NNJoin outputs a temporary (in-memory) layer that is added to the QGIS table of contents. This output layer is of the same feature type (point, line, polygon) as the input layer. If desired, save the temporary layer to your disk.

The temporary layer's attribute table will contain three columns of interest. For each output record you'll see

1. The input layer id

2. The join layer id that is closest to that record's input layer id

3. The distance, in CRS units, between the two features.

If your 488-point layer is the tool's input layer, the output will have 488 records. If your 605-point layer is the tool's input layer, the output will have 605 records.

FYI, the Distance Matrix Tool generates all pairwise distances between the features in both layers (hence my 295,240 comment!), not just the minimum distance.

• Sounds great. Will it be necessary for this tool to convert a multipoint layer in a single point layer`? – Tina Aug 8 '19 at 10:47
• Sounds great. It worked well and I did not even had to convert my multipoint layer in a single point layer. The distance will be in CRS units what does this mean in case of EPSG:4326 - WGS 84, which is the CRS of both layers. I will do the same process with 2 more different join layers and the aim will be to compare the distances. What would be the best approach for this next step? – Tina Aug 8 '19 at 11:43
• I'm happy that it worked for you! Regarding the output distance values found in the third column that I mentioned in my answer: do those values make sense? For example, if you expect that the points are all within a few hundred meters of each other, those values should reflect that. They should not contain values outside that range. If you aren't familiar with the point data (perhaps you received it from a third party), you can become familiar with it by viewing it with a basemap displayed underneath (use the QuickMapServices plugin for a great selection of basemaps). – Stu Smith Aug 8 '19 at 15:10
• Regarding the CRS of my point layers will the distance be in CRS units or I guess I could call them map units? What can I call it and is there a way to transer those values into metres? I did it with 3 different join layers and than made a 'basic statisic for fields' with each distance column. When I compare the outcome of those statistics the results reflect the impression I get from the map > the points of one join layer are closer to the input layer than the ones of the other 2 join layers. So thanks a lot. – Tina Aug 8 '19 at 15:41
• Regarding EPSG:4326... I don't have any experience with it (thus I will defer to others who know more about it), but my understanding is that it stores values in degree units, not meter or feet units. Therefore, it may not be appropriate for your needs. I suspect you want some sort of equidistant projection. There are many online sources for projection info, here's one: geokov.com/education/map-projection.aspx – Stu Smith Aug 8 '19 at 15:52