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: package org.geotools.validation.spatial;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.geotools.feature.FeatureIterator;
024: import org.geotools.data.FeatureSource;
025: import org.geotools.feature.Feature;
026: import org.geotools.validation.ValidationResults;
027:
028: import com.vividsolutions.jts.geom.Envelope;
029: import com.vividsolutions.jts.geom.Geometry;
030:
031: /**
032: * This validation plugIn checks to see if any features intersect.
033: *
034: * <p>
035: * If they do then the validation failed.
036: * </p>
037: *
038: * @author Brent Owens, Refractions Research, Inc.
039: * @author $Author: dmzwiers $ (last modification)
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/spatial/LinesNotIntersectValidation.java $
041: * @version $Id: LinesNotIntersectValidation.java 22754 2006-11-16 03:03:20Z jgarnett $
042: */
043: public class LinesNotIntersectValidation extends
044: LineLineAbstractValidation {
045: /**
046: * An no argument constructor (for the Java Beans Specification)
047: */
048: public LinesNotIntersectValidation() {
049: }
050:
051: /**
052: * Ensure Lines do not intersect.
053: *
054: * <p>
055: * This is supposed to go off and grab the necesary features from the
056: * database using the envelope with the typeNames. But it doesn't yet. It
057: * just uses the ones passed in through parameter layers.
058: * </p>
059: *
060: * @param layers a HashMap of key="TypeName" value="FeatureSource"
061: * @param envelope The bounding box of modified features
062: * @param results Storage for the error and warning messages
063: *
064: * @return True if no features intersect. If they do then the validation
065: * failed.
066: *
067: * @throws Exception DOCUMENT ME!
068: *
069: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map,
070: * com.vividsolutions.jts.geom.Envelope,
071: * org.geotools.validation.ValidationResults)
072: */
073: public boolean validate(Map layers, Envelope envelope,
074: ValidationResults results) throws Exception {
075: ArrayList geoms = new ArrayList(); // FIDs used for lookup to see if any match
076: boolean result = true;
077: Iterator it = layers.values().iterator();
078:
079: while (it.hasNext()) // for each layer
080: {
081: FeatureSource featureSource = (FeatureSource) it.next();
082: FeatureIterator features = featureSource.getFeatures()
083: .features();
084:
085: try {
086: while (features.hasNext()) // for each feature
087: {
088: // check if it intersects any of the previous features
089: Feature feature = features.next();
090: Geometry geom = feature.getDefaultGeometry();
091:
092: for (int i = 0; i < geoms.size(); i++) // for each existing geometry
093: {
094: // I don't trust this thing to work correctly
095: if (geom.crosses((Geometry) geoms.get(i))) {
096: results.error(feature,
097: "Lines cross when they shouldn't.");
098: result = false;
099: }
100: }
101:
102: geoms.add(geom);
103: }
104: } finally {
105: features.close(); // this is an important line
106: }
107: }
108:
109: return result;
110: }
111:
112: /**
113: * Override getPriority.
114: *
115: * <p>
116: * Sets the priority level of this validation.
117: * </p>
118: *
119: * @return A made up priority for this validation.
120: *
121: * @see org.geotools.validation.Validation#getPriority()
122: */
123: public int getPriority() {
124: return PRIORITY_INVOLVED;
125: }
126: }
|