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.logging.Level;
020: import java.util.logging.Logger;
021:
022: import org.geotools.feature.Feature;
023: import org.geotools.feature.FeatureType;
024: import org.geotools.validation.DefaultFeatureValidation;
025: import org.geotools.validation.ValidationResults;
026:
027: import com.vividsolutions.jts.geom.Geometry;
028:
029: /**
030: * Tests to see if a geometry is valid by calling Geometry.isValid().
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 bowens, Refractions Research, Inc.
038: * @author $Author: dmzwiers $ (last modification)
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/main/java/org/geotools/validation/spatial/IsValidGeometryValidation.java $
040: * @version $Id: IsValidGeometryValidation.java 27862 2007-11-12 19:51:19Z desruisseaux $
041: */
042: public class IsValidGeometryValidation extends DefaultFeatureValidation {
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: /**
048: * IsValidGeometryFeatureValidation constructor.
049: *
050: * <p>
051: * Description
052: * </p>
053: */
054: public IsValidGeometryValidation() {
055: }
056:
057: /**
058: * Override getPriority.
059: *
060: * <p>
061: * Sets the priority level of this validation.
062: * </p>
063: *
064: * @return A made up priority for this validation.
065: *
066: * @see org.geotools.validation.Validation#getPriority()
067: */
068: public int getPriority() {
069: return PRIORITY_TRIVIAL;
070: }
071:
072: /**
073: * Override getTypeNames.
074: *
075: * <p>
076: * Returns the TypeNames of the FeatureTypes used in this particular
077: * validation.
078: * </p>
079: *
080: * @return An array of TypeNames
081: *
082: * @see org.geotools.validation.Validation#getTypeRefs()
083: */
084: public String[] getTypeNames() {
085: if (getTypeRef() == null) {
086: return null; // disabled
087: } else if (getTypeRef().equals("*")) {
088: return new String[0]; // apply to all
089: } else {
090: return new String[] { getTypeRef(), };
091: }
092: }
093:
094: /**
095: * Tests to see if a geometry is valid by calling Geometry.isValid().
096: *
097: * <p>
098: * The geometry is first tested to see if it is null, and if it is null,
099: * then it is tested to see if it is allowed to be null by calling
100: * isNillable().
101: * </p>
102: *
103: * @param feature The Feature to be validated
104: * @param type The FeatureTypeInfo of the feature
105: * @param results The storage for error messages.
106: *
107: * @return True if the feature is a valid geometry.
108: *
109: * @see org.geotools.validation.FeatureValidation#validate(
110: * org.geotools.feature.Feature,
111: * org.geotools.feature.FeatureType,
112: * org.geotools.validation.ValidationResults)
113: */
114: public boolean validate(Feature feature, FeatureType type,
115: ValidationResults results) {
116: Geometry geom = feature.getDefaultGeometry();
117:
118: if (geom == null) {
119: if (type.getDefaultGeometry().isNillable()) {
120: LOGGER.log(Level.FINEST, getName() + "("
121: + feature.getID() + ") passed");
122:
123: return true;
124: } else {
125: String message = "Geometry was null but is not nillable.";
126: results.error(feature, message);
127: LOGGER.log(Level.FINEST, getName() + "("
128: + feature.getID() + "):" + message);
129:
130: return false;
131: }
132: }
133:
134: if (!geom.isValid()) {
135: String message = "Not a valid geometry. isValid() failed";
136: LOGGER.log(Level.FINEST, getName() + "(" + feature.getID()
137: + "):" + message);
138: results.error(feature, message);
139:
140: return false;
141: }
142:
143: LOGGER.log(Level.FINEST, getName() + "(" + feature.getID()
144: + ") passed");
145:
146: return true;
147: }
148: }
|