Tuesday, 13 September 2016

ST_Equals Postgis problems


I am trying to find overlapping lines using ST_EQUALS on two tables



This query returns 0 rows:


SELECT * FROM geom1 c, geom2 h WHERE st_equals(c.simple_geom, h.simple_geom);

This query runs correctly and returns overlapping lines:


SELECT * FROM geom1 c, geom2 h WHERE st_astext(c.simple_geom)=st_astext(h.simple_geom);

I am not sure why ST_equals fails. Both colums are indexed, I thought it was due to geom being multilinestring, but after converting to simple_geom with just LineString (using St_geometryN(geom,1)) it is still giving unexpected results.


So it is not indexes, and neither MultiLineString column.


What are other possible sources of this behaviour?



Answer




You probably have a single-bit difference between the coordinates, small enough it doesn't show up in the text output, but large enough that it causes the ordinates to actually differ.


SELECT * FROM geom1 c, geom2 h WHERE 
st_equals(
ST_SnapToGrid(c.simple_geom, 0.01),
ST_SnapToGrid(h.simple_geom, 0.01)
);

If you force them to be identical, it should work.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...