001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on Jan 24, 2004
017: */
018: package org.geotools.validation.spatial;
019:
020: import java.util.HashSet;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.geotools.data.FeatureSource;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.FeatureIterator;
028: import org.geotools.validation.ValidationResults;
029:
030: import com.vividsolutions.jts.geom.CoordinateSequence;
031: import com.vividsolutions.jts.geom.Envelope;
032: import com.vividsolutions.jts.geom.Geometry;
033: import com.vividsolutions.jts.geom.LineString;
034:
035: /**
036: * LineIntersectsLineWithNodeValidation purpose.
037: *
038: * <p>
039: * Ensures Line crosses the other Line at a node.
040: * </p>
041: *
042: * @author dzwiers, Refractions Research, Inc.
043: * @author $Author: jive $ (last modification)
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/spatial/LineIntersectsLineWithNodeValidation.java $
045: * @version $Id: LineIntersectsLineWithNodeValidation.java 22666 2006-11-09 03:50:28Z jgarnett $
046: */
047: public class LineIntersectsLineWithNodeValidation extends
048: LineLineAbstractValidation {
049: /**
050: * LineIntersectsLineWithNodeValidation constructor.
051: *
052: * <p>
053: * Description
054: * </p>
055: */
056: public LineIntersectsLineWithNodeValidation() {
057: super ();
058: }
059:
060: /**
061: * Ensure Line crosses the other Line at a node.
062: *
063: * <p></p>
064: *
065: * @param layers a HashMap of key="TypeName" value="FeatureSource"
066: * @param envelope The bounding box of modified features
067: * @param results Storage for the error and warning messages
068: *
069: * @return True if no features intersect. If they do then the validation
070: * failed.
071: *
072: * @throws Exception DOCUMENT ME!
073: *
074: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map,
075: * com.vividsolutions.jts.geom.Envelope,
076: * org.geotools.validation.ValidationResults)
077: */
078: public boolean validate(Map layers, Envelope envelope,
079: ValidationResults results) throws Exception {
080: boolean r = true;
081:
082: FeatureSource fsLine = (FeatureSource) layers
083: .get(getLineTypeRef());
084:
085: FeatureCollection fcLine = fsLine.getFeatures();
086: FeatureIterator fLine = fcLine.features();
087:
088: FeatureSource fsRLine = (FeatureSource) layers
089: .get(getRestrictedLineTypeRef());
090:
091: FeatureCollection fcRLine = fsRLine.getFeatures();
092:
093: while (fLine.hasNext()) {
094: Feature line = fLine.next();
095: FeatureIterator fRLine = fcRLine.features();
096: Geometry lineGeom = line.getDefaultGeometry();
097: if (envelope.contains(lineGeom.getEnvelopeInternal())) {
098: // check for valid comparison
099: if (LineString.class.isAssignableFrom(lineGeom
100: .getClass())) {
101: while (fRLine.hasNext()) {
102: Feature rLine = fRLine.next();
103: Geometry rLineGeom = rLine.getDefaultGeometry();
104: if (envelope.contains(rLineGeom
105: .getEnvelopeInternal())) {
106: if (LineString.class
107: .isAssignableFrom(rLineGeom
108: .getClass())) {
109: if (lineGeom.intersects(rLineGeom)) {
110: if (!hasPair(
111: ((LineString) lineGeom)
112: .getCoordinateSequence(),
113: ((LineString) rLineGeom)
114: .getCoordinateSequence())) {
115: results
116: .error(rLine,
117: "Line does not intersect line at node covered by the specified Line.");
118: r = false;
119: }
120: } else {
121: results
122: .warning(rLine,
123: "Does not intersect the LineString");
124: }
125: // do next.
126: } else {
127: fcRLine.remove(rLine);
128: results
129: .warning(rLine,
130: "Invalid type: this feature is not a derivative of a LineString");
131: }
132: } else {
133: fcRLine.remove(rLine);
134: }
135: }
136: } else {
137: results
138: .warning(line,
139: "Invalid type: this feature is not a derivative of a LineString");
140: }
141: }
142: }
143: return r;
144: }
145:
146: /**
147: * hasPair purpose.
148: * <p>
149: * finds a pair of points, assumes the sequence is sorted smallest to largest.
150: * </p>
151: * @param a1
152: * @param a2
153: */
154: private boolean hasPair(CoordinateSequence a1, CoordinateSequence a2) {
155: int i = 0;
156: CoordinateSequence c;
157: c = a1;
158: Set m = new HashSet();
159: while (i < c.size()) {
160: m.add(c.getCoordinate(i));
161: i++;
162: }
163: i = 0;
164: c = a2;
165: while (i < c.size()) {
166: if (!m.add(c.getCoordinate(i)))
167: return true;
168: i++;
169: }
170: return false;
171: }
172: }
|