# Finding distance between points inside a region

I have several points (long, lat in EPSG:4326) in a region like this: and I would like to find a way to estimate the average distance between all points inside the region with a command-line tool solution (eg. `awk`, `postgis`, `python`, `saga`, `gdal` `grass`, `gmt`). About the distance, I am not worried about the precision of the estimated distance (I know the longitude/latitude thing between 4326 and UTM), just need to have a distance estimate (degrees or meters) from all points inside the region. Below you can find a zoom area of the region previously shown: Just as an example from the image above, distance between points in the x-direction is of 2-5 m in the along-line direction (ie. on each line segment) while it is of 20-30 m in the y-direction (ie. between line segments).

I just found the SAGA tool `Point Distance` but after using it I got `1%Terminado (killed)` in a small data set of 10k points.... Here the link to this small sample data set https://ufile.io/e3vcp0xx

You can use postgis to measure each points distance to all others.

``````select avg(st_distancesphere(t1.geom, t2.geom)) from pointswgs84 t1
cross join pointswgs84 t2
where t1.id>t2.id
``````

You can use psql command to execute:

``````psql -U postgres -d testdb -c "select avg(st_distancesphere(t1.geom, t2.geom)) from pointswgs84 t1 cross join pointswgs84 t2 where t1.id>t2.id"

avg
------------------
4531.12184477769
(1 row)
``````

With 10000 point this will be ~100 million distance calculations, so it is going to take some time, if it doesnt fail. Maybe you should reconsider and measure point density instead? This query takes ~10 s for 300000 points:

``````SELECT count(*)/st_area( st_convexhull( ST_Multi( ST_Union(wkb_geometry)))) point_density
FROM ok_bs_riks
``````
• Thanks a ton, I will test it. When I referred to the "region" in the first image I was talking about the "invisible" rectangle created by the distribution of points.
– Gery
Oct 11 at 19:36