# Finding average distance to nearest point

I would like to find out the average distance from high schools (represented as a point in QGIS) to libraries (represented as another point in QGIS) in New York's 32 districts.

I have already calculated the distance to nearest library for each individual high school, but I need the result to be averaged at the district level. in other words, the result should be something like: "in district 5, the average distance between a high school and a library is 2 miles".

Is there any way to do this without manually calculating the average distance for each district, plotting this average into a table, and joining this table to the vector layer for districts?

• Is there a chance you have ArcGIS with an advanced license? In that software I would generate a near table then use summary statistics to find the average (mean) distance as the crow flies. There could be a QGIS equivalent gis.stackexchange.com/questions/168120/… and gis.stackexchange.com/questions/126639/… could both be helpful, and for statistics gis.stackexchange.com/questions/266303/… might help. Commented Nov 14, 2019 at 0:34
• you can also do this with QGIS, see this nice tutorial from Ujaval: qgistutorials.com/en/docs/3/nearest_neighbor_analysis.html Commented Nov 14, 2019 at 7:49
• Use the aggregate functions in the Field Calculator to add a field to the district layer. Something like `aggregate('High Schools', 'mean', "Distance to Library",intersects(\$geometry,@parent))`. Substitute the name of your High Schools layer and the name of the "distance to library" field in that layer.
– csk
Commented Nov 14, 2019 at 17:00

This question was asked several times, always with a different nuance.

The same, as there are many possible solutions, can be answered in several ways.

The solution provided by @csk seems to me the best if you want to include the information in the existing Districts layer.

In the links provided by @MichaelStimson there are also very good solutions. The virtual layer solution is very good for cases where the origin values vary frequently. The answers that included plugins were very good at the time, but the constant development of QGIS was including many functionalities that began as a plugin.

The tutorial provided by @eurojam is good to start the spatial analysis, although it does not answer your question exactly.

Which is best for your case, depends on the functionality you want.

If the source data does not change constantly (that is, you want to perform the calculation only once), and you also prefer to obtain the result in a new layer, which includes the geometries and attribute table of the Districts layer, with one or more new fields , which are some statistical function of one or more fields of the HighScools layer, spatially related under the predicate contains (District cointains High Schools), the Join attributes by location (summary) tool seems to me the most appropriate: