[postgis-users] Does ST_AsText truncate or round

Kevin Neufeld kneufeld at refractions.net
Fri May 16 08:15:25 PDT 2008


Have you considered using ST_SnapToGrid() and make all geometries in you 
database respect a specified tolerance?

Warning, snaptogrid may not behave as you might expect - this is not a 
simple precision_reducer method (which I think would be valuable).

Consider:
select
   'POINT(0.877 0.778)'::geometry ~=
   snaptogrid('POINT(0.87689017271623 0.777984935324639)'::geometry, 0.001);

  ?column?
----------
  t
(1 row)

No problem there, but then there's this:
select
   'POINT(0.956 0.66)'::geometry ~=
   snaptogrid('POINT(0.956079571507871 0.659502930939198)'::geometry, 
0.001);

  ?column?
----------
  f
(1 row)


To deal with this (I know it looks like a hack, but it works), I convert 
to text and then back to a geometry.  Wrapping this in a trigger on your 
geometry tables will guarantee any geometry edits are automatically 
reduced to a certain precision.

select
    'POINT(0.956 0.66)'::geometry ~=
    geomfromtext(astext(snaptogrid('POINT(0.956079571507871 
0.659502930939198)'::geometry, 0.001)), -1);

  ?column?
----------
  t
(1 row)


Cheers,
Kevin

Paragon Corporation wrote:
> Slight clarification about what I am trying to solve.
> 
> After edits I am coming back with lines that used to be intersecting no
> longer intersecting and that still have the same AsText representation.
> 
> My hope is to reduce the precision of my geometry in the database so the
> edits don't break the topology of my lines by doing something like this
> 
> 
> Update sometable
> 	SET the_geom = ST_SetSRID(ST_AsText(the_geom), mysrid)
> 
> 
> But I fear that if AsText is doing a rounding rather than a truncation, that
> I run the risk of breaking things that used to intersect.  I am more
> concerned about under intersecting than over intersecting.  Although I
> probably haven't thought this out enough so maybe it's a non-issue.
> 
> Thanks,
> Regina
> 
>  
> 
> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net
> [mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Paragon
> Corporation
> Sent: Friday, May 16, 2008 12:01 AM
> To: 'PostGIS Users Discussion'
> Subject: [postgis-users] Does ST_AsText truncate or round
> 
> Hopefully this is a simple question.  I know that ST_AsText returns a
> geometry that is not necessarily as prescise as what is actually stored in
> the geometry field.  I have an editor that relies on WKT representation.
> 
> Anyrate just wanted to know if the ST_AsText does a rounding of the points
> or it does a truncation of the points.  Also would be nice to know if I
> could control the precision of this since I will need to truncate my actual
> geometry accordingly so that both are in agreement.
> 
> Thanks,
> Regina
> 
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
> 
> 
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users



More information about the postgis-users mailing list