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.filter.Filter;
030: import org.geotools.filter.FilterFactory;
031: import org.geotools.filter.FilterFactoryFinder;
032: import org.geotools.validation.ValidationResults;
033:
034: import com.vividsolutions.jts.geom.Envelope;
035: import com.vividsolutions.jts.geom.Geometry;
036:
037: /**
038: * Tests to see if a Geometry intersects with another Geometry.
039: *
040: * <p>
041: * If only one Geometry is given, then this test checks to see if it
042: * intersects part of itself.
043: * </p>
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/relate/IntersectsIntegrity.java $
045: */
046: public class IntersectsIntegrity extends RelationIntegrity {
047: private static final Logger LOGGER = org.geotools.util.logging.Logging
048: .getLogger("org.geotools.validation");
049:
050: /**
051: * OverlapsIntegrity Constructor
052: *
053: */
054: public IntersectsIntegrity() {
055: super ();
056: }
057:
058: /* (non-Javadoc)
059: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map, com.vividsolutions.jts.geom.Envelope, org.geotools.validation.ValidationResults)
060: */
061: public boolean validate(Map layers, Envelope envelope,
062: ValidationResults results) throws Exception {
063: LOGGER.finer("Starting test " + getName() + " ("
064: + getClass().getName() + ")");
065: String typeRef1 = getGeomTypeRefA();
066: LOGGER.finer(typeRef1 + ": looking up FeatureSource ");
067: FeatureSource geomSource1 = (FeatureSource) layers
068: .get(typeRef1);
069: LOGGER.finer(typeRef1 + ": found "
070: + geomSource1.getSchema().getTypeName());
071:
072: String typeRef2 = getGeomTypeRefB();
073: if (typeRef2 == EMPTY || typeRef1.equals(typeRef2))
074: return validateSingleLayer(geomSource1, isExpected(),
075: results, envelope);
076: else {
077: LOGGER.finer(typeRef2 + ": looking up FeatureSource ");
078: FeatureSource geomSource2 = (FeatureSource) layers
079: .get(typeRef2);
080: LOGGER.finer(typeRef2 + ": found "
081: + geomSource2.getSchema().getTypeName());
082: return validateMultipleLayers(geomSource1, geomSource2,
083: isExpected(), results, envelope);
084: }
085:
086: }
087:
088: /**
089: * <b>validateMultipleLayers Purpose:</b> <br>
090: * <p>
091: * This validation tests for a geometry crosses another geometry.
092: * Uses JTS' Geometry.crosses(Geometry) method.
093: *
094: * </p>
095: *
096: * <b>Description:</b><br>
097: * <p>
098: * The function filters the FeatureSources using the given bounding box.
099: * It creates iterators over both filtered FeatureSources. It calls intersects() using the
100: * geometries in the FeatureSource layers. Tests the results of the method call against
101: * the given expected results. Returns true if the returned results and the expected results
102: * are true, false otherwise.
103: *
104: * </p>
105: *
106: * Author: bowens<br>
107: * Created on: Apr 27, 2004<br>
108: * @param featureSourceA - the FeatureSource to pull the original geometries from. This geometry is the one that is tested for intersecting with the other
109: * @param featureSourceB - the FeatureSource to pull the other geometries from - these geometries will be those that may intersect the first geometry
110: * @param expected - boolean value representing the user's expected outcome of the test
111: * @param results - ValidationResults
112: * @param bBox - Envelope - the bounding box within which to perform the intersects()
113: * @return boolean result of the test
114: * @throws Exception - IOException if iterators improperly closed
115: */
116: private boolean validateMultipleLayers(
117: FeatureSource featureSourceA, FeatureSource featureSourceB,
118: boolean expected, ValidationResults results, Envelope bBox)
119: throws Exception {
120: boolean success = true;
121:
122: FilterFactory ff = FilterFactoryFinder.createFilterFactory();
123: Filter filter = null;
124:
125: //JD: fix this!!
126: //filter = (Filter) ff.createBBoxExpression(bBox);
127:
128: FeatureCollection FeatureCollectionA = featureSourceA
129: .getFeatures(filter);
130: FeatureCollection FeatureCollectionB = featureSourceB
131: .getFeatures(filter);
132:
133: FeatureIterator fr1 = null;
134: FeatureIterator fr2 = null;
135: try {
136: fr1 = FeatureCollectionA.features();
137:
138: if (fr1 == null)
139: return false;
140:
141: while (fr1.hasNext()) {
142: Feature f1 = fr1.next();
143: Geometry g1 = f1.getDefaultGeometry();
144: fr2 = FeatureCollectionB.features();
145:
146: while (fr2 != null && fr2.hasNext()) {
147: Feature f2 = fr2.next();
148: Geometry g2 = f2.getDefaultGeometry();
149: if (g1.intersects(g2) != expected) {
150: results.error(f1, f1.getDefaultGeometry()
151: .getGeometryType()
152: + " "
153: + getGeomTypeRefA()
154: + " intersects "
155: + getGeomTypeRefB()
156: + "("
157: + f2.getID()
158: + "), Result was not " + expected);
159: success = false;
160: }
161: }
162: }
163: } finally {
164: fr1.close();
165: if (fr2 != null)
166: fr2.close();
167: }
168:
169: return success;
170: }
171:
172: /**
173: * <b>validateSingleLayer Purpose:</b> <br>
174: * <p>
175: * This validation tests for a geometry that intersects with itself.
176: * Uses JTS' Geometry.intersects(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 intersects() 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.
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 intersects()
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 FeatureCollection = featureSourceA
209: .getFeatures(filter);
210:
211: FeatureIterator fr1 = null;
212: FeatureIterator fr2 = null;
213: try {
214: fr1 = FeatureCollection.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 = FeatureCollection.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.intersects(g2) != expected) {
230: results.error(f1, f1.getDefaultGeometry()
231: .getGeometryType()
232: + " "
233: + getGeomTypeRefA()
234: + " intersects "
235: + getGeomTypeRefA()
236: + "("
237: + f2.getID()
238: + "), Result was not "
239: + expected);
240: success = false;
241: }
242: }
243: }
244: }
245: } finally {
246: fr1.close();
247: if (fr2 != null)
248: fr2.close();
249: }
250:
251: return success;
252: }
253: }
|