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;
018:
019: import java.util.logging.Logger;
020:
021: import org.geotools.feature.Feature;
022: import org.geotools.feature.FeatureType;
023:
024: import com.vividsolutions.jts.geom.Geometry;
025: import com.vividsolutions.jts.geom.GeometryCollection;
026: import com.vividsolutions.jts.geom.LineString;
027: import com.vividsolutions.jts.geom.MultiLineString;
028:
029: /**
030: * Tests to see if a Feature ...
031: *
032: * <p>
033: * The geometry is first tested to see if it is null, and if it is null, then
034: * it is tested to see if it is allowed to be null by calling isNillable().
035: * </p>
036: *
037: * @author Jody Garnett, Refractions Research, Inc.
038: * @author $Author: jive $ (last modification)
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/DefaultFeatureValidation.java $
040: * @version $Id: DefaultFeatureValidation.java 27862 2007-11-12 19:51:19Z desruisseaux $
041: */
042: public class DefaultFeatureValidation implements FeatureValidation {
043: /** The logger for the validation module. */
044: private static final Logger LOGGER = org.geotools.util.logging.Logging
045: .getLogger("org.geotools.validation");
046:
047: /** User's Name of this validation test. */
048: private String name; // name of the validation
049:
050: /** User's description of this validation test. */
051: private String description;
052:
053: /**
054: * Identification of required FeatureType as dataStoreId:typeName.
055: *
056: * <p>
057: * The provided ValidationProcessor assumes that FeatureTypes will be
058: * references will be of the form dataStoreId:typeName.
059: * </p>
060: *
061: * <p>
062: * If "" or null is used All FetureTypes will be checked.
063: * </p>
064: */
065: private String typeRef;
066:
067: /**
068: * No argument constructor, required by the Java Bean Specification.
069: */
070: public DefaultFeatureValidation() {
071: }
072:
073: /**
074: * Sets the name of this validation.
075: *
076: * @param name The name of this validation.
077: *
078: * @see org.geotools.validation.Validation#setName(java.lang.String)
079: */
080: public final void setName(String name) {
081: this .name = name;
082: }
083:
084: /**
085: * Access the user's name for this test.
086: *
087: * @return The name of this validation.
088: *
089: * @see org.geotools.validation.Validation#getName()
090: */
091: public final String getName() {
092: return name;
093: }
094:
095: /**
096: * Sets the description of this validation.
097: *
098: * @param description The description of the validation.
099: *
100: * @see org.geotools.validation.Validation#setDescription(java.lang.String)
101: */
102: public final void setDescription(String description) {
103: this .description = description;
104: }
105:
106: /**
107: * Override getDescription.
108: *
109: * <p>
110: * Returns the description of this validation as a string.
111: * </p>
112: *
113: * @return The description of this validation.
114: *
115: * @see org.geotools.validation.Validation#getDescription()
116: */
117: public final String getDescription() {
118: return description;
119: }
120:
121: /**
122: * The priority level used to schedule this Validation.
123: *
124: * @return PRORITY_SIMPLE
125: *
126: * @see org.geotools.validation.Validation#getPriority()
127: */
128: public int getPriority() {
129: return PRIORITY_SIMPLE;
130: }
131:
132: /**
133: * Implementation of getTypeNames.
134: *
135: * @return Array of typeNames, or empty array for all, null for disabled
136: *
137: * @see org.geotools.validation.Validation#getTypeRefs()
138: */
139: public String[] getTypeRefs() {
140: if (typeRef == null) {
141: return null;
142: }
143:
144: if (typeRef.equals("*")) {
145: return ALL;
146: }
147:
148: return new String[] { typeRef, };
149: }
150:
151: /**
152: * Access typeRef property.
153: *
154: * @return Returns the typeRef in the format dataStoreId:typeName.
155: */
156: public String getTypeRef() {
157: return typeRef;
158: }
159:
160: /**
161: * Set typeRef to typeRef.
162: *
163: * @param typeRef The typeRef in the format dataStoreId:typeName
164: */
165: public void setTypeRef(String typeRef) {
166: this .typeRef = typeRef;
167: }
168:
169: /**
170: * Validation test for feature.
171: *
172: * <p>
173: * Description of test ...
174: * </p>
175: *
176: * @param feature The Feature to be validated
177: * @param type The FeatureType of the feature
178: * @param results The storage for error messages.
179: *
180: * @return <code>true</code> if the feature is a valid geometry.
181: *
182: * @see org.geotools.validation.FeatureValidation#validate
183: */
184: public boolean validate(Feature feature, FeatureType type,
185: ValidationResults results) {
186: LOGGER.warning(getName() + " not implemented");
187: results.warning(feature, " test not implemented");
188:
189: return false;
190: }
191:
192: //
193: // Convience Methods
194: //
195: /**
196: * Retrives a single LineString from feature.getDefaultGeometry.
197: * <p>
198: * If feature contains MultiLineString (or GeometryCollection ) of length
199: * 1 it will be deemed sufficient. Shapefiles are determined to work with
200: * MultiLineStrings of length 1 forcing the creation of this method.
201: * </p>
202: *
203: * <p>
204: * If feature.getDefaultGeometry returns <code>null</code> this method
205: * will return null. For most cases the validation should just be abandoned
206: * with a warning; the user can separately specify a NullZero check. This
207: * will prevent the same error (a null value) being reproted by
208: * each and every SpatialValidation test.
209: * </p>
210: *
211: * @param feature Feature
212: * @return feature.getDefaultGeomertry as a LineString, or <code>null</code>
213: * @throws ClassCastException If feature.getDefaultGeometry is the wrong type
214: */
215: protected LineString getDefaultLineString(Feature feature)
216: throws ClassCastException {
217: Geometry geom = feature.getDefaultGeometry();
218: if (geom == null) {
219: // Ignore null value, user can use NullZero check
220: return null;
221: }
222: if (geom instanceof LineString) {
223: return (LineString) geom;
224: } else if (geom instanceof MultiLineString) {
225: // Shapefiles are determined to give us their contents as
226: // a MultiLineString - forcing our hand in this case
227: //
228: MultiLineString lines = (MultiLineString) geom;
229: if (lines.getNumGeometries() == 1) {
230: return (LineString) lines.getGeometryN(0);
231: }
232: throw new ClassCastException(
233: "MultiLineString does not contain a single LineString");
234: } else if (geom instanceof GeometryCollection) {
235: GeometryCollection geoms = (GeometryCollection) geom;
236: if (geoms.getNumGeometries() == 1
237: && geoms.getGeometryN(0) instanceof LineString) {
238: return (LineString) geoms.getGeometryN(0);
239: }
240: throw new ClassCastException(
241: "GeometryCollection does not contain a single LineString");
242: } else {
243: throw new ClassCastException("Cannot convert to LineString");
244: }
245: }
246: }
|