Postgis - one circles contains another

306 views Asked by At

I'm using postgis with gps data, and trying to figure out if one circle with a GPS coordinate and radius (in meters), contains another.

I'm able to do it if I'm not using GPS coordinates, but just points on a graph, but this doesn't work if I substitute with lat and lon points:

-- A circle within a circle
SELECT ST_Contains(bigc,smallc) As bigcontainssmall
FROM (SELECT ST_Buffer(ST_MakePoint(21, 38)::geography, 40) As smallc,
         ST_Buffer(ST_MakePoint(21, 39)::geography, 400) AS bigc) foo;

Thoughts?

1

There are 1 answers

0
sal On BEST ANSWER

My approach would be to:

  1. Assign a CRS (Coordinate Reference System) to the gps data (I assume they are in WGS84, thus srid 4326) via SRID (ST_SetSRID(your_geom, 4326) does this)
  2. Then (Only possible if you assigned the srid 4326) cast them to geography (::geography does this) to enable setting the buffer radiae in meters,
  3. And cast them back to geometry (::geometry does this) for the st_contains function to work

Query:

    SELECT ST_Contains(bigc::geometry,smallc::geometry) as bigcontainssmall 
from (select 
st_buffer(ST_SetSRID(ST_MakePoint(-71.10434, 42.31506),4326)::geography,40) as smallc,
st_buffer(ST_SetSRID(ST_MakePoint(-71.10434, 42.31507),4326)::geography,400) as bigc) foo