2

This question already has an answer here:

The default tolerance of PostGIS ST_Intersects query is "0.00001 meters" as indicated in the doc. I need to use the same query with another tolerance value but ST_Intersects takes only 2 input parameters (geom A and geom B).

Is there a way to change this default value?

marked as duplicate by mgri, whyzar, Andre Silva, Kersten, jbchurchill Apr 13 '17 at 13:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Have you checked this gis.stackexchange.com/questions/157092/set-tolerance-in-postgis ? – Shiko Apr 13 '17 at 7:22
  • Yes I have checked this discussion, and have tried ST_Dwithin but it takes much more time than ST_Intersects – Adam Adam Apr 13 '17 at 7:31
  • There is solution but not straightforward, PostGIS is opensource so you can update the value and compile github.com/postgis/postgis/blob/… – Shiko Apr 13 '17 at 8:11
  • 1
    Use ST_SnapToGrid first, which effectively rounds the geometries, before ST_Intersects. Note, ST_Intersects and ST_Within are not the same thing, and the latter has to do more work, so would take longer. – John Powell Apr 13 '17 at 8:15
  • 1
    This is not a duplicate, because this is about the geography type, which does use a tolerance. The linked question is about the geometry type which does not. Please don't vote to close questions that you don't understand. – dbaston Apr 18 '17 at 13:00
3

PostGIS is opensource, indeed you can create a new function for ST_INTERSECT with Tolerance parameter as input or update the function itself with value you are interested in, then run it.
Here is the code snippet where tolerance is dynamic, you can run below script in your DB and it will be create under PUBLIC schema :

CREATE OR REPLACE FUNCTION ST_Intersects(geography, geography,float8)
    RETURNS boolean
    AS 'SELECT $1 && $2 AND _ST_Distance($1, $2, 0.0, false) < $3'
    LANGUAGE 'sql' IMMUTABLE ;

Link for original function of ST_Intersects(geo,geo):
https://github.com/postgis/postgis/blob/c0998a4e638048196a6abfab1504eca6b1a1462f/postgis/geography.sql.in#L720

  • Except either remove @extschema@. or replace it with the extension schema e.g. public – Mike T Apr 17 '17 at 21:44
  • @MikeT Thanks, I updated to public and rectify the function. – Shiko Apr 18 '17 at 4:43
  • @Shiko thank you for the detailed reply. I have not tested the solution yet. I have another problem with GEOS (self-intersection and OGC Simple Feature specification..) I'll give the result as soon as possible – Adam Adam Apr 18 '17 at 15:05
  • I did this : CREATE OR REPLACE FUNCTION public.st_intersects(geometry, geometry, double precision) RETURNS boolean AS 'SELECT $1 OPERATOR(public.&&) $2 AND public._ST_Distance($1, $2, 0.0, false) < $3' LANGUAGE sql IMMUTABLE I got this error message when I launch a query: Only lon/lat coordinate systems are supported in geography Note that my data are in metric projection – Adam Adam Apr 19 '17 at 11:46
  • Sorry I did not manage to format my comment – Adam Adam Apr 19 '17 at 11:57

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