001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jtsexample.geom;
034:
035: import com.vividsolutions.jts.geom.*;
036: import com.vividsolutions.jts.io.*;
037:
038: /**
039: * An example showing the results of using different precision models
040: * in computations involving geometric constructions.
041: * A simple intersection computation is carried out in three different
042: * precision models (Floating, FloatingSingle and Fixed with 0 decimal places).
043: * The input is the same in all cases (since it is precise in all three models),
044: * The output shows the effects of rounding in the single-precision and fixed-precision
045: * models.
046: *
047: * @version 1.7
048: */
049: public class PrecisionModelExample {
050: public static void main(String[] args) {
051: PrecisionModelExample example = new PrecisionModelExample();
052: try {
053: example.run();
054: } catch (Exception ex) {
055: ex.printStackTrace();
056: }
057: }
058:
059: public PrecisionModelExample() {
060: }
061:
062: public void run() throws ParseException {
063: example1();
064: example2();
065: }
066:
067: public void example1() throws ParseException {
068: System.out
069: .println("-------------------------------------------");
070: System.out
071: .println("Example 1 shows roundoff from computing in different precision models");
072: String wktA = "POLYGON ((60 180, 160 260, 240 80, 60 180))";
073: String wktB = "POLYGON ((200 260, 280 160, 80 100, 200 260))";
074: System.out.println("A = " + wktA);
075: System.out.println("B = " + wktB);
076:
077: intersection(wktA, wktB, new PrecisionModel());
078: intersection(wktA, wktB, new PrecisionModel(
079: PrecisionModel.FLOATING_SINGLE));
080: intersection(wktA, wktB, new PrecisionModel(1));
081: }
082:
083: public void example2() throws ParseException {
084: System.out
085: .println("-------------------------------------------");
086: System.out
087: .println("Example 2 shows that roundoff can change the topology of geometry computed in different precision models");
088: String wktA = "POLYGON ((0 0, 160 0, 160 1, 0 0))";
089: String wktB = "POLYGON ((40 60, 40 -20, 140 -20, 140 60, 40 60))";
090: System.out.println("A = " + wktA);
091: System.out.println("B = " + wktB);
092:
093: difference(wktA, wktB, new PrecisionModel());
094: difference(wktA, wktB, new PrecisionModel(1));
095: }
096:
097: public void intersection(String wktA, String wktB, PrecisionModel pm)
098: throws ParseException {
099: System.out.println("Running example using Precision Model = "
100: + pm);
101: GeometryFactory fact = new GeometryFactory(pm);
102: WKTReader wktRdr = new WKTReader(fact);
103:
104: Geometry A = wktRdr.read(wktA);
105: Geometry B = wktRdr.read(wktB);
106: Geometry C = A.intersection(B);
107:
108: System.out.println("A intersection B = " + C);
109: }
110:
111: public void difference(String wktA, String wktB, PrecisionModel pm)
112: throws ParseException {
113: System.out
114: .println("-------------------------------------------");
115: System.out.println("Running example using Precision Model = "
116: + pm);
117: GeometryFactory fact = new GeometryFactory(pm);
118: WKTReader wktRdr = new WKTReader(fact);
119:
120: Geometry A = wktRdr.read(wktA);
121: Geometry B = wktRdr.read(wktB);
122: Geometry C = A.difference(B);
123:
124: System.out.println("A intersection B = " + C);
125: }
126: }
|