001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.plugin.analysis;
034:
035: import java.util.*;
036: import com.vividsolutions.jts.algorithm.*;
037: import com.vividsolutions.jts.geom.*;
038: import com.vividsolutions.jts.simplify.*;
039: import com.vividsolutions.jump.qa.diff.BufferGeometryMatcher;
040: import com.vividsolutions.jump.workbench.ui.GenericNames;
041:
042: /**
043: * A function object for {@link Geometry} functions (which return a Geometry).
044: * Provides metadata about the function.
045: *
046: * @author Martin Davis
047: * @version 1.0
048: */
049: public abstract class GeometryPredicate {
050: static GeometryPredicate[] method = { new IntersectsPredicate(),
051: new ContainsPredicate(), new CoversPredicate(),
052: new CoveredByPredicate(), new CrossesPredicate(),
053: new DisjointPredicate(), new EqualsPredicate(),
054: new OverlapsPredicate(), new TouchesPredicate(),
055: new WithinPredicate(), new WithinDistancePredicate(),
056: new SimilarPredicate(), };
057:
058: static List getNames() {
059: List names = new ArrayList();
060: for (int i = 0; i < method.length; i++) {
061: names.add(method[i].name);
062: }
063: return names;
064: }
065:
066: static GeometryPredicate getPredicate(String name) {
067: for (int i = 0; i < method.length; i++) {
068: if (method[i].name.equals(name))
069: return method[i];
070: }
071: return null;
072: }
073:
074: private String name;
075: private int nArguments;
076: private int nParams;
077: private String description;
078:
079: public GeometryPredicate(String name, int nParams) {
080: this (name, 2, nParams, null);
081: }
082:
083: public GeometryPredicate(String name) {
084: this (name, 2, 0, null);
085: }
086:
087: public GeometryPredicate(String name, int nArgs, int nParams) {
088: this (name, nArgs, nParams, null);
089: }
090:
091: public GeometryPredicate(String name, int nArgs, int nParams,
092: String description) {
093: this .name = name;
094: this .nArguments = nArgs;
095: this .nParams = nParams;
096: this .description = description;
097: }
098:
099: public String getName() {
100: return name;
101: }
102:
103: public int getGeometryArgumentCount() {
104: return nArguments;
105: }
106:
107: public int getParameterCount() {
108: return nParams;
109: }
110:
111: public abstract boolean isTrue(Geometry geom0, Geometry geom1,
112: double[] param);
113:
114: private static class IntersectsPredicate extends GeometryPredicate {
115: public IntersectsPredicate() {
116: super (GenericNames.INTERSECTS);
117: }
118:
119: public boolean isTrue(Geometry geom0, Geometry geom1,
120: double[] param) {
121: return geom0.intersects(geom1);
122: }
123: }
124:
125: private static class ContainsPredicate extends GeometryPredicate {
126: public ContainsPredicate() {
127: super (GenericNames.CONTAINS);
128: }
129:
130: public boolean isTrue(Geometry geom0, Geometry geom1,
131: double[] param) {
132: return geom0.contains(geom1);
133: }
134: }
135:
136: private static class CoversPredicate extends GeometryPredicate {
137: public CoversPredicate() {
138: super (GenericNames.COVERS);
139: }
140:
141: public boolean isTrue(Geometry geom0, Geometry geom1,
142: double[] param) {
143: return geom0.covers(geom1);
144: }
145: }
146:
147: private static class CoveredByPredicate extends GeometryPredicate {
148: public CoveredByPredicate() {
149: super (GenericNames.COVEREDBY);
150: }
151:
152: public boolean isTrue(Geometry geom0, Geometry geom1,
153: double[] param) {
154: return geom0.coveredBy(geom1);
155: }
156: }
157:
158: private static class CrossesPredicate extends GeometryPredicate {
159: public CrossesPredicate() {
160: super (GenericNames.CROSSES);
161: }
162:
163: public boolean isTrue(Geometry geom0, Geometry geom1,
164: double[] param) {
165: return geom0.crosses(geom1);
166: }
167: }
168:
169: public static class DisjointPredicate extends GeometryPredicate {
170: public DisjointPredicate() {
171: super (GenericNames.DISJOINT);
172: }
173:
174: public boolean isTrue(Geometry geom0, Geometry geom1,
175: double[] param) {
176: return geom0.disjoint(geom1);
177: }
178: }
179:
180: private static class EqualsPredicate extends GeometryPredicate {
181: public EqualsPredicate() {
182: super (GenericNames.EQUALS);
183: }
184:
185: public boolean isTrue(Geometry geom0, Geometry geom1,
186: double[] param) {
187: return geom0.equals(geom1);
188: }
189: }
190:
191: private static class OverlapsPredicate extends GeometryPredicate {
192: public OverlapsPredicate() {
193: super (GenericNames.OVERLAPS);
194: }
195:
196: public boolean isTrue(Geometry geom0, Geometry geom1,
197: double[] param) {
198: return geom0.overlaps(geom1);
199: }
200: }
201:
202: private static class TouchesPredicate extends GeometryPredicate {
203: public TouchesPredicate() {
204: super (GenericNames.TOUCHES);
205: }
206:
207: public boolean isTrue(Geometry geom0, Geometry geom1,
208: double[] param) {
209: return geom0.touches(geom1);
210: }
211: }
212:
213: private static class WithinPredicate extends GeometryPredicate {
214: public WithinPredicate() {
215: super (GenericNames.WITHIN);
216: }
217:
218: public boolean isTrue(Geometry geom0, Geometry geom1,
219: double[] param) {
220: return geom0.within(geom1);
221: }
222: }
223:
224: public static class WithinDistancePredicate extends
225: GeometryPredicate {
226: public WithinDistancePredicate() {
227: super (GenericNames.WITHIN_DISTANCE, 1);
228: }
229:
230: public boolean isTrue(Geometry geom0, Geometry geom1,
231: double[] param) {
232: return geom0.isWithinDistance(geom1, param[0]);
233: }
234: }
235:
236: public static class SimilarPredicate extends GeometryPredicate {
237: public SimilarPredicate() {
238: super (GenericNames.SIMILAR, 1);
239: }
240:
241: public boolean isTrue(Geometry geom0, Geometry geom1,
242: double[] param) {
243: return BufferGeometryMatcher
244: .isMatch(geom0, geom1, param[0]);
245: }
246: }
247: }
|