PrecisionModel

210 views
Skip to first unread message

ivy

unread,
Oct 24, 2011, 10:59:37 AM10/24/11
to JSTS devs
Hi,

Can anyone explain to me how to use the PrecisionModels? Let's say
I've split a polygon p in two parts (pa, pb). I store and retrieve pa
in/from a database and projection transformations introduce very small
numerical errors. When I subtract p1 from the original polygon p
( p.difference(pa) ) I end up with multiple polygons, one with very
small area (due to the numerical error).

I tried to couple a GeometryFactory with a fixed PrecisionModel to the
WKTReader, but this doesn't work:

> var wkt1="POLYGON((10 10, 11 10, 11 11, 10 11, 10 10))"; // this is a square
> var wkt2="POLYGON((10 10, 11 10.0000000000001, 11 11, 10 10))"; // this is a triangle inside the square, with a 0.0000000000001 rounding error
>
> var precision = new jsts.geom.PrecisionModel(1000000);
> var rd = new jsts.io.WKTReader(new jsts.geom.GeometryFactory(precision));
>
> var p = rd.read(wkt1), pa = rd.read(wkt2);
> var dif = p.difference(pa);
>
> var wr = new jsts.io.WKTWriter();
> print(wr.write(p));
> print(wr.write(pa));
> print(wr.write(dif)); // this prints MULTIPOLYGON(((11 10.0000000000001,11 10,10 10,11 10.0000000000001)),((10 10,10 11,11 11,10 10)))

When I manually map the coordinates to my fixed grid, it does work:

> function PrecisionFilter(precisionModel) {
> this.filter = function(coord) {
> precisionModel.makePrecise(coord);
> }
> }
> PrecisionFilter.prototype = new jsts.geom.CoordinateFilter;
> var filter = new PrecisionFilter(precision);
>
> p.apply(filter);
> pa.apply(filter);
>
> dif = p.difference(pa);
> print(wr.write(dif)); // this prints POLYGON((10 10,10 11,11 11,10 10))

This still feels a bit ugly. What is the correct way of working with
precisionmodels? Can I couple them to the created polygons?

With kind regards,
Ivo

Björn

unread,
Oct 25, 2011, 4:21:09 PM10/25/11
to JSTS devs
PrecisionModel isn't enforced on input data automatically. I guess for
performance reasons.

So the manual stuff your doing is the right way. There is a helper
class in the original JTS which has not yet been ported to JSTS:

http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/precision/SimpleGeometryPrecisionReducer.html

/Björn
Reply all
Reply to author
Forward
0 new messages