1

I have a database of points coordinates taken from OSM. My geometry column is called wkb_geometry. I am trying to find the most remote point (by distance to the next nearest point). I was hoping to use the new ST_MaximumInscribedCircle function and then order by radius to find the most remote point. Below is the code I have so far. It runs but returns 0 for the radius. The example on the PostGIS website is for a polygon.

SELECT
  osm_id, name,
  geom.radius,
  geom.center,   
  geom.nearest   
FROM
  australian_points AS t,
  LATERAL ST_MaximumInscribedCircle(t.wkb_geometry) AS geom
  ORDER BY geom.radius ASC
;
3
  • The table australian_points is a points layer not a polygon layer
    – Dunuts
    Commented Sep 1, 2022 at 13:40
  • 1
    You need to collect the points into a MULTIPOINT using ST_MaximumInscribedCircle( ST_Collect( t.wkb_geometry))
    – dr_jts
    Commented Sep 1, 2022 at 15:31
  • 1
    It's probably more efficient (and more correct?) to find the nearest point to each point with a NN query, and then take the one with maximum distance.
    – dr_jts
    Commented Sep 1, 2022 at 15:32

1 Answer 1

2

With an index structure supporting (spatial) (K)NN searches like the PostGIS GIST extension, this is trivially executed with

SELECT
  *
FROM (
  SELECT
    p1.id,
    p1.geom,
    ST_Distance(p1.geom, pn.geom) AS dist
  FROM
    australian_points AS p1
    CROSS JOIN LATERAL (
      SELECT
        p2.geom
      FROM
        australian_points AS p2
      WHERE
        p1.id <> p2.id
      ORDER BY
        p1.geom <-> p2.geom
      LIMIT
        1
    ) AS pn
) q
ORDER BY
  dist DESC
LIMIT
  1
;

Here

  • we find the nearest neighbor to each point using a (K)NN query (a CROSS JOIN LATERAL "loop" in conjunction with the index-optimized <-> distance operator)
  • and then filter for the highest dist value (ORDER BY dist DESC LIMIT 1)

If you need true geoidal surface distances, you'd want to implement a CAST to GEOGRAPHY; change the query to use

  • ST_Distance(p1.geom::GEOGRAPHY, p2.geom::GEOGRAPHY) (spheroidal calculation)
  • p1.geom::GEOGRAPHY <-> p2.geom::GEOGRAPHY (spherical calculation) respectively.

(Note that a CAST to GEOGRAPHY is possible only for geographic reference systems!)


But beware:

This can only ever be as performant with an appropriate index on the CAST, e.g.

CREATE INDEX
  ON australian_points
  USING ( (geom::GEOGRAPHY) )
;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.