001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * Created on Apr 27, 2004
018: */
019: package org.geotools.validation.relate;
020:
021: import java.io.IOException;
022: import java.util.Map;
023: import java.util.logging.Logger;
024:
025: import org.geotools.feature.FeatureIterator;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.data.FeatureSource;
028: import org.geotools.feature.Feature;
029: import org.geotools.feature.FeatureCollectionIteration;
030: import org.geotools.filter.Filter;
031: import org.geotools.filter.FilterFactory;
032: import org.geotools.filter.FilterFactoryFinder;
033: import org.geotools.validation.ValidationResults;
034:
035: import com.vividsolutions.jts.geom.Envelope;
036: import com.vividsolutions.jts.geom.Geometry;
037:
038: /**
039: * Tests to see if a Geometry is disjoint with another Geometry.
040: *
041: * <p>
042: * If only one Geometry is given, then this test checks to see if it
043: * is disjoint with itself.
044: * </p>
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/relate/DisjointIntegrity.java $
046: */
047: public class DisjointIntegrity extends RelationIntegrity {
048: private static final Logger LOGGER = org.geotools.util.logging.Logging
049: .getLogger("org.geotools.validation");
050:
051: /**
052: * OverlapsIntegrity Constructor
053: *
054: */
055: public DisjointIntegrity() {
056: super ();
057: }
058:
059: /* (non-Javadoc)
060: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map, com.vividsolutions.jts.geom.Envelope, org.geotools.validation.ValidationResults)
061: */
062: public boolean validate(Map layers, Envelope envelope,
063: ValidationResults results) throws Exception {
064: LOGGER.finer("Starting test " + getName() + " ("
065: + getClass().getName() + ")");
066: String typeRef1 = getGeomTypeRefA();
067: LOGGER.finer(typeRef1 + ": looking up FeatureSource ");
068: FeatureSource geomSource1 = (FeatureSource) layers
069: .get(typeRef1);
070: LOGGER.finer(typeRef1 + ": found "
071: + geomSource1.getSchema().getTypeName());
072:
073: String typeRef2 = getGeomTypeRefB();
074: if (typeRef2 == EMPTY || typeRef1.equals(typeRef2))
075: return validateSingleLayer(geomSource1, isExpected(),
076: results, envelope);
077: else {
078: LOGGER.finer(typeRef2 + ": looking up FeatureSource ");
079: FeatureSource geomSource2 = (FeatureSource) layers
080: .get(typeRef2);
081: LOGGER.finer(typeRef2 + ": found "
082: + geomSource2.getSchema().getTypeName());
083: return validateMultipleLayers(geomSource1, geomSource2,
084: isExpected(), results, envelope);
085: }
086:
087: }
088:
089: /**
090: * <b>validateMultipleLayers Purpose:</b> <br>
091: * <p>
092: * This validation tests for a geometry is disjoint with another geometry.
093: * Uses JTS' Geometry.disjoint(Geometry) method.
094: *
095: * </p>
096: *
097: * <b>Description:</b><br>
098: * <p>
099: * The function filters the FeatureSources using the given bounding box.
100: * It creates iterators over both filtered FeatureSources. It calls disjoint() using the
101: * geometries in the FeatureSource layers. Tests the results of the method call against
102: * the given expected results. Returns true if the returned results and the expected results
103: * are true, false otherwise.
104: *
105: * </p>
106: *
107: * Author: bowens<br>
108: * Created on: Apr 27, 2004<br>
109: * @param featureSourceA - the FeatureSource to pull the original geometries from. This geometry is the one that is tested for disjoint with the other
110: * @param featureSourceB - the FeatureSource to pull the other geometries from - these geometries will be those that may be disjoint with the first geometry
111: * @param expected - boolean value representing the user's expected outcome of the test
112: * @param results - ValidationResults
113: * @param bBox - Envelope - the bounding box within which to perform the disjoint()
114: * @return boolean result of the test
115: * @throws Exception - IOException if iterators improperly closed
116: */
117: private boolean validateMultipleLayers(
118: FeatureSource featureSourceA, FeatureSource featureSourceB,
119: boolean expected, ValidationResults results, Envelope bBox)
120: throws Exception {
121: boolean success = true;
122:
123: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
124: Filter filter = null;
125:
126: //JD: fix this !!
127: //filter = (Filter) ff.createBBoxExpression(bBox);
128:
129: FeatureCollection FeatureCollectionA = featureSourceA
130: .getFeatures(filter);
131: FeatureCollection FeatureCollectionB = featureSourceB
132: .getFeatures(filter);
133:
134: FeatureIterator fr1 = null;
135: FeatureIterator fr2 = null;
136: try {
137: fr1 = FeatureCollectionA.features();
138:
139: if (fr1 == null)
140: return false;
141:
142: while (fr1.hasNext()) {
143: Feature f1 = fr1.next();
144: Geometry g1 = f1.getDefaultGeometry();
145: fr2 = FeatureCollectionB.features();
146:
147: while (fr2 != null && fr2.hasNext()) {
148: Feature f2 = fr2.next();
149: Geometry g2 = f2.getDefaultGeometry();
150: if (g1.disjoint(g2) != expected) {
151: results.error(f1, f1.getDefaultGeometry()
152: .getGeometryType()
153: + " "
154: + getGeomTypeRefA()
155: + " is disjoint from "
156: + getGeomTypeRefB()
157: + "("
158: + f2.getID()
159: + "), Result was not " + expected);
160: success = false;
161: }
162: }
163: }
164: } finally {
165: FeatureCollectionA.close(fr1);
166: FeatureCollectionB.close(fr2);
167: }
168:
169: return success;
170: }
171:
172: /**
173: * <b>validateSingleLayer Purpose:</b> <br>
174: * <p>
175: * This validation tests for a geometry that is disjoint with itself.
176: * Uses JTS' Geometry.disjoint(Geometry) method.
177: * </p>
178: *
179: * <b>Description:</b><br>
180: * <p>
181: * The function filters the FeatureSource using the given bounding box.
182: * It creates iterators over the filtered FeatureSource. It calls disjoint() using the
183: * geometries in the FeatureSource layer. Tests the results of the method call against
184: * the given expected results. Returns true if the returned results and the expected results
185: * are true, false otherwise.
186: *
187: * </p> *
188: * Author: bowens<br>
189: * Created on: Apr 27, 2004<br>
190: * @param featureSourceA - the FeatureSource to pull the original geometries from. This geometry is the one that is tested for being disjoint with itself
191: * @param expected - boolean value representing the user's expected outcome of the test
192: * @param results - ValidationResults
193: * @param bBox - Envelope - the bounding box within which to perform the disjoint()
194: * @return boolean result of the test
195: * @throws Exception - IOException if iterators improperly closed
196: */
197: private boolean validateSingleLayer(FeatureSource featureSourceA,
198: boolean expected, ValidationResults results, Envelope bBox)
199: throws Exception {
200: boolean success = true;
201:
202: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
203: Filter filter = null;
204:
205: //JD: fix this!!
206: //filter = (Filter) ff.createBBoxExpression(bBox);
207:
208: FeatureCollection collection = featureSourceA
209: .getFeatures(filter);
210:
211: FeatureIterator fr1 = null;
212: FeatureIterator fr2 = null;
213: try {
214: fr1 = collection.features();
215:
216: if (fr1 == null)
217: return false;
218:
219: while (fr1.hasNext()) {
220: Feature f1 = fr1.next();
221: Geometry g1 = f1.getDefaultGeometry();
222: fr2 = collection.features();
223:
224: while (fr2 != null && fr2.hasNext()) {
225: Feature f2 = fr2.next();
226: Geometry g2 = f2.getDefaultGeometry();
227: if (!f1.getID().equals(f2.getID())) // if they are the same feature, move onto the next one
228: {
229: if (g1.disjoint(g2) != expected) {
230: results.error(f1, f1.getDefaultGeometry()
231: .getGeometryType()
232: + " "
233: + getGeomTypeRefA()
234: + " is disjoint from "
235: + getGeomTypeRefA()
236: + "("
237: + f2.getID()
238: + "), Result was not "
239: + expected);
240: success = false;
241: }
242: }
243: }
244: }
245: } finally {
246: collection.close(fr1);
247: collection.close(fr2);
248: }
249:
250: return success;
251: }
252: }
|