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.network;
019:
020: import java.util.Map;
021:
022: import org.geotools.data.FeatureSource;
023: import org.geotools.feature.Feature;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureIterator;
026: import org.geotools.graph.build.line.LineStringGraphGenerator;
027: import org.geotools.graph.structure.Graph;
028: import org.geotools.validation.DefaultIntegrityValidation;
029: import org.geotools.validation.ValidationResults;
030:
031: import com.vividsolutions.jts.geom.Envelope;
032:
033: /**
034: * OrphanNodeValidation purpose.
035: *
036: * <p>
037: * Builds a network, and looks for orphaned nodes.
038: * </p>
039: *
040: * @author dzwiers, Refractions Research, Inc.
041: * @author $Author: dmzwiers $ (last modification)
042: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/network/OrphanNodeValidation.java $
043: * @version $Id: OrphanNodeValidation.java 22666 2006-11-09 03:50:28Z jgarnett $
044: */
045: public class OrphanNodeValidation extends DefaultIntegrityValidation {
046: /** the FeatureSource name datastoreId:typename */
047: private String typeName;
048:
049: /**
050: * StarNodeValidation constructor.
051: *
052: * <p>
053: * Description
054: * </p>
055: */
056: public OrphanNodeValidation() {
057: super ();
058: }
059:
060: /**
061: * Implementation of getTypeRefs.
062: *
063: * @see org.geotools.validation.Validation#getTypeRefs()
064: *
065: */
066: public String[] getTypeRefs() {
067: return new String[] { typeName };
068: }
069:
070: /**
071: * Check FeatureType for ...
072: *
073: * <p>
074: * Detailed description...
075: * </p>
076: *
077: * @param layers Map of FeatureSource by "dataStoreID:typeName"
078: * @param envelope The bounding box that encloses the unvalidated data
079: * @param results Used to coallate results information
080: *
081: * @return <code>true</code> if all the features pass this test.
082: *
083: * @throws Exception DOCUMENT ME!
084: */
085: public boolean validate(Map layers, Envelope envelope,
086: final ValidationResults results) throws Exception {
087:
088: LineStringGraphGenerator lgb = new LineStringGraphGenerator();
089: FeatureSource fs = (FeatureSource) layers.get(typeName);
090: FeatureCollection fr = fs.getFeatures();
091: FeatureCollection fc = fr;
092: FeatureIterator f = fc.features();
093:
094: while (f.hasNext()) {
095: Feature ft = f.next();
096:
097: if (envelope.contains(ft.getBounds())) {
098: //lgb.add(ft);
099: lgb.add(ft.getDefaultGeometry());
100: }
101: }
102:
103: // lgb is loaded
104: Graph g = lgb.getGraph();
105:
106: return (g.getNodesOfDegree(0).size() == 0);
107:
108: }
109:
110: // public boolean validate_old(Map layers, Envelope envelope,
111: // final ValidationResults results) throws Exception {
112: // LineGraphBuilder lgb = new LineGraphBuilder();
113: // FeatureSource fs = (FeatureSource) layers.get(typeName);
114: // FeatureResults fr = fs.getFeatures();
115: // FeatureCollection fc = fr.collection();
116: // FeatureIterator f = fc.features();
117: //
118: // while (f.hasNext()) {
119: // Feature ft = f.next();
120: //
121: // if (envelope.contains(ft.getBounds())) {
122: // lgb.add(ft);
123: // }
124: // }
125: //
126: // // lgb is loaded
127: // org.geotools.graph.structure.Graph g = lgb.build();
128: //
129: // class OrphanVisitor implements GraphVisitor {
130: // private int count = 0;
131: //
132: // public int getCount() {
133: // return count;
134: // }
135: //
136: // public int visit(GraphComponent element) {
137: // if (element.getAdjacentElements().size() == 0) {
138: // count++;
139: // }
140: //
141: // results.error(element.getFeature(), "Orphaned");
142: //
143: // return GraphTraversal.CONTINUE;
144: // }
145: // }
146: //
147: // OrphanVisitor ov = new OrphanVisitor();
148: // SimpleGraphWalker sgv = new SimpleGraphWalker(ov);
149: // BasicGraphTraversal bgt = new BasicGraphTraversal(g, sgv);
150: // bgt.walkNodes();
151: //
152: // if (ov.getCount() == 0) {
153: // return true;
154: // } else {
155: // return false;
156: // }
157: // }
158:
159: /**
160: * Access typeName property.
161: *
162: * @return Returns the typeName.
163: */
164: public String getTypeName() {
165: return typeName;
166: }
167:
168: /**
169: * Set typeName to typeName.
170: *
171: * @param typeName The typeName to set.
172: */
173: public void setTypeName(String typeName) {
174: this.typeName = typeName;
175: }
176: }
|