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