01: package com.vividsolutions.jtsexample.technique;
02:
03: import com.vividsolutions.jts.geom.*;
04: import com.vividsolutions.jts.io.WKTReader;
05: import java.util.*;
06:
07: /**
08: * Shows a technique for using a zero-width buffer to compute
09: * the union of a collection of <b>polygonal</b> geometrys.
10: * The advantages of this technique are:
11: * <ul>
12: * <li>can avoid robustness issues
13: * <li>faster for large numbers of input geometries
14: * <li>handles GeometryCollections as input (although only the polygons will be buffered)
15: * </ul>
16: * Disadvantages are:
17: * <ul>
18: * <li>may not preserve input coordinate precision in some cases
19: * <li>only works for polygons
20: * </ul>
21: *
22: * @version 1.7
23: */
24:
25: public class PolygonUnionUsingBuffer {
26:
27: public static void main(String[] args) throws Exception {
28: WKTReader rdr = new WKTReader();
29:
30: Geometry[] geom = new Geometry[3];
31: geom[0] = rdr
32: .read("POLYGON (( 100 180, 100 260, 180 260, 180 180, 100 180 ))");
33: geom[1] = rdr
34: .read("POLYGON (( 80 140, 80 200, 200 200, 200 140, 80 140 ))");
35: geom[2] = rdr
36: .read("POLYGON (( 160 160, 160 240, 240 240, 240 160, 160 160 ))");
37: unionUsingBuffer(geom);
38:
39: }
40:
41: public static void unionUsingBuffer(Geometry[] geom) {
42: GeometryFactory fact = geom[0].getFactory();
43: Geometry geomColl = fact.createGeometryCollection(geom);
44: Geometry union = geomColl.buffer(0.0);
45: System.out.println(union);
46: }
47:
48: }
|