2

I have a table with a lat and a long float columns and I'm trying to use the ST_PointInsideCircle PostGIS method which is working but the radius I enter is not in the correct units. I'm trying to config the radius to be in meters, all of the configuration queries I saw with setting a different SRID was for tables with geom point var columns . I tried running something like this query but it does not work for me the units are still probably in feet or degrees for example

SELECT * 
FROM box nbe 
WHERE ST_PointInsideCircle(ST_Transform(st_point(nbe.longitude,nbe.latitude),3857), -91.973998, 29.978479, 1000)

those are example coordinates they are random

3
  • Distance properties are useless in Web Mercator, since distance is always wrong in that projection. And you seem to be mapping 100 meters off the origin of Web Mercator (Null Island).
    – Vince
    Commented Feb 21, 2023 at 13:54
  • 3
    Have a look at ST_DWithin with geographies instead
    – JGH
    Commented Feb 21, 2023 at 14:05
  • i tried using the ST_DWithin function but the last argument aka the distance still not seems to be in meters how can i configure this?
    – ab_dev _j
    Commented Feb 28, 2023 at 14:51

1 Answer 1

1

Use st_dwithin instead. Geometry in a geographic coordinate system is using degrees as the units, which is why your 1000 is not working. Since you want to use meters, cast the geometry to geography, like this:

SELECT * 
FROM box nbe 
WHERE ST_DWithin(ST_Transform(st_point(nbe.longitude,nbe.latitude,3857),4326)::geography, st_setsrid(st_makepoint(-91.973998, 29.978479),4326)::geography, 1000)

If nbe.longitude / nbe.latitude are already srid 4326 (WGS 84) then you would do this instead:

SELECT * 
FROM box nbe 
WHERE ST_DWithin(st_point(nbe.longitude,nbe.latitude,4326)::geography, st_setsrid(st_makepoint(-91.973998, 29.978479),4326)::geography, 1000)

Regarding casting, according to the docs

The CAST syntax conforms to SQL; the syntax with :: is historical PostgreSQL usage.

Cast to geography using cast syntax:

st_dwithin(cast(st_point(nbe.longitude,nbe.latitude,4326) as geography),cast(st_point(-91.973998, 29.978479, 4326) as geography),1000)
7
  • i get this error when trying to execute that line " Only lon/lat coordinate systems are supported in geography"
    – ab_dev _j
    Commented Mar 5, 2023 at 12:21
  • i think the problem is that my lat and long are just float8 values they are not a geom value is there a way that could still work around with that?
    – ab_dev _j
    Commented Mar 5, 2023 at 12:40
  • Oh that's my mistake. I forget that 3857 is a projected coordinate system. Change that to 4326 in your st_transform(st_point(...),4326). Also, if you know that the lat/lon are using 3857, do this instead: st_transform(st_point(nbe.longitude, nbe.latitude, 3857),4326)
    – jbalk
    Commented Mar 6, 2023 at 17:59
  • i tried to work with the "::geography" it worked when i run the query directly into the postgres server but im having troublle when trying to compile the query on spring query but st_distancesphere method seems to work too is it ok? i red on the docs that is pretty much the same
    – ab_dev _j
    Commented Mar 8, 2023 at 11:27
  • st_distancespheroid could also work. It's slower than st_distancesphere but is more accurate. You would have to use either function like this where st_distancesphere(st_point(nbe.longitude,nbe.latitude,4326),st_point(-91.973998, 29.978479, 4326)) <= 1000
    – jbalk
    Commented Mar 8, 2023 at 17:27

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.