I am new to PostGIS.
How do I calculate the distance and direction between every site (approx 30,000 record) and the nearest entity in a gazetteer (approx 300,000 records)?
Ideally a separate table would be created that contains the site_id, entity_id, distance between, direction between (entity & site). I have looked at an answer supplied by Tobias Herrmann in late 2014 - paraphrased below.
This code was written to do the calculation of distance, but is a work in progress.
SELECT site.gid AS gid_1, gazetteer.gid AS gid_2, ST_Distance(site.geom, gazetteer.geom) AS mindist
FROM table site, table gazetteer WHERE site.gid != gazetteer.gid AND ST_Distance(site.geom, gazetteer.geom) != 0
ORDER BY ST_Distance(site.geom, gazetteeer.geom)
LIMIT 1;
<->operator. technically, this question is a close duplicate to most of these linked Q/As: https://gis.stackexchange.com/a/338328/93656, https://gis.stackexchange.com/a/333032/93656, https://gis.stackexchange.com/a/332103/93656, https://gis.stackexchange.com/a/278362/93656, https://gis.stackexchange.com/a/279397/93656 – geozelot Oct 30 '19 at 12:30