01: /***********************************************
02: * created on 14.09.2005
03: * last modified:
04: *
05: * author: sstein
06: *
07: * description:
08: * merges two polygon and returns the result via
09: * getOutPolygon() if the result is not of another
10: * geometry type. The merge is done with the JTS union()
11: * method.
12: ***********************************************/package org.openjump.core.geomutils.algorithm;
13:
14: import com.vividsolutions.jts.geom.Geometry;
15: import com.vividsolutions.jts.geom.IntersectionMatrix;
16: import com.vividsolutions.jts.geom.Polygon;
17:
18: /**
19: * @description:
20: * merges two polygon and returns the result via
21: * getOutPolygon() if the result is not of another
22: * geometry type. The merge is done with the JTS union()
23: * method.
24: *
25: * @author sstein
26: *
27: */
28: public class PolygonMerge {
29:
30: private boolean polys = false;
31: private int mergeSuccesfull = 0;
32: private Geometry outPolygon = null;
33:
34: public PolygonMerge(Geometry poly1, Geometry poly2) {
35: if ((poly1 instanceof Polygon) && (poly2 instanceof Polygon)) {
36: this .polys = true;
37: //check if polygons do touch
38: IntersectionMatrix myIM = poly1.relate(poly2);
39: /*
40: System.out.println("IntersectionMat: top right-bottom left : "
41: + myIM.get(0,0) + " " + myIM.get(0,1) + " " + myIM.get(0,2) + ","
42: + myIM.get(1,0) + " " + myIM.get(1,1) + " " + myIM.get(1,2) + ","
43: + myIM.get(2,0) + " " + myIM.get(2,1) + " " + myIM.get(2,2) );
44: */
45: if (myIM.matches("2********")
46: || (myIM.matches("****1****"))) {
47: //System.out.println("PolygonMerge.constructor: calc merge");
48: Geometry geom = poly1.union(poly2);
49: this .outPolygon = geom;
50: if (geom instanceof Polygon) {
51: this .mergeSuccesfull = 1;
52: } else {
53: //System.out.println("PolygonMerge.constructor: merged object would be multi-polygon");
54: this .mergeSuccesfull = 2;
55: }
56: } else {
57: //System.out.println("PolygonMerge.constructor: polygons don't touch");
58: }
59: }
60: }
61:
62: /**
63: * @return Returns the mergeSuccesfull.<p>
64: * 0: polys dont touch, 1: sucessfull, 2: polys touch but only with one point on line. thus
65: * a multipolygon is created.
66: */
67: public int isMergeSuccesfull() {
68: return mergeSuccesfull;
69: }
70:
71: /**
72: * @return Returns the outPolygon.
73: */
74: public Geometry getOutPolygon() {
75: return outPolygon;
76: }
77:
78: /**
79: * @return Returns if both are polys.
80: */
81: public boolean isPolys() {
82: return polys;
83: }
84: }
|