[postgis-users] Does ST_AsText truncate or round

Kevin Neufeld kneufeld at refractions.net
Mon May 19 19:57:26 PDT 2008


Hi Regina,
Sorry I didn't respond sooner ... I was out camping for the weekend. 
Fun, fun :)

Right, this is what I was thinking with my previous post recommending 
snaptogrid - force your geometries through WKT : 
geomfromtext(astext(the_geom)).  Essentially, this would make all your 
geometries only as precise as their WKT representation.  In a way, this 
enforces a very fine precision model on all your geometries.  This 
should elicit the data consistency you were after ... assuming of course 
that all your table edits are performed using geomfromtext().  Bear in 
mind, though, that if you have a client program that modifies the table 
using WKB, you may be left with data in an inconsistent state.  If this 
is the case, put a simple trigger on your tables forcing all UPDATES and 
INSERTS through WKT.

I agree with Paul R, however, that a client application should not be 
transiting data using WKT, but rather WKB.  It may take some work, but I 
would recommend that you upgrade your apps to perform all data edits 
using WKB. 

He was right when he mentioned that I fought with this for a while.  I 
had a large database that utilized the full precision of PostGIS. I did 
some data editing using a simple WKT editor and suddenly found that 
geometries that used to touch no longer touch or they intersect 
incorrectly.

I guess it boils down to:
* A database that does not enforce any precision model, by default uses 
full precision - thus, all data edits should be done using full precision. 
* A database that enforces a precision model (including the one you 
suggested where X = GeomFromText(AsText(X)) must be true), could be 
edited using a editor with an equivalent precision model.  In your case, 
a WKT enforced database could be edited using a WKT editor.  However, if 
you can't guarantee that all editing is performed using WKT (including 
updating a geometry using transform() ), then triggers should probably 
be constructed enforcing the model on your spatial tables.


Cheers,
Kevin



Paragon Corporation wrote:
> Kevin,
>
> Actually this would help me with the second phase, but for the first phase I
> just need consistency which I was hoping AsText would do for me.  It doesn't
> even need to be that good since I'm sure the other side is using AsText in
> some fashion.  So I just need consistency more than correctness.
>
> Where I start off with 
>
> Orginal geometry X 
>
> I think I need to guarantee that 
>
> X =  GeomFromText(AsText(X))
>
> Seemed like the easiest way to assure that was to just force all my Xs to X
> = ST_GeomFromText(AsText(X), somesrid)    which I presume should be the same
> as
>
> ST_SetSRID(AsText(X), somesrid)   
>
> The back end editor I'm assuming is just taking the AsText(geometry)
> representation and just returning that back when no edits on a particular
> point,  and so its being poisoned by its own database query.  I'm not
> working on that part so that's just my general observation.
>
> Thanks,
> Regina
>
> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net
> [mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Kevin
> Neufeld
> Sent: Friday, May 16, 2008 11:15 AM
> To: PostGIS Users Discussion
> Subject: Re: [postgis-users] Does ST_AsText truncate or round
>
> 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
>>     
> _______________________________________________
> 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