I’m working with a very large dataset and I need to calculate the shortest distance between a single point and the closest edge from multiple polygons. I need to use PostGIS because such a task with a take a very long time in QGIS or GRASS GIS.
Unfortunately, I’m relatively new to PostGIS. I’ve been googling and found a QGIS plugin that has code for doing similar tasks in PostGIS (https://github.com/NaturalGIS/naturalgis_postgis_geoprocessing). Based on its “minimum distance” algorithm, I wrote the following query that unfortunately does not work:
My schema is called "sar", the point is a vector called "point" and the polygons are in a vector called "rep".
CREATE TABLE sar.distances;
WITH sar.distances AS (SELECT ST_Union(geom) AS geom FROM sar.rep_ex)
SELECT ST_ShortestLine(g1.geom,g2.geom)::geometry(LINESTRING,3857) AS geom, ST_Distance(g1.geom,g2.geom) AS distance, g1.id AS id from
FROM sar.point_ex AS g1, sar.distances AS g2
The error is message is:
[ERROR: syntax error at or near ";"
LINE 1: CREATE TABLE sar.distances; WITH sar.distances AS (SELECT S...
^
]
What am I doing wrong?
I'm attaching a small sample of the data I'm working with (point_ex.shp and rep_ex.shp).
https://figshare.com/articles/dataset/example_data_zip/21836508