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.Map;
021:
022: import org.geotools.data.FeatureSource;
023: import org.geotools.feature.Feature;
024: import org.geotools.validation.ValidationResults;
025:
026: import com.vividsolutions.jts.geom.Envelope;
027: import com.vividsolutions.jts.geom.Geometry;
028: import com.vividsolutions.jts.geom.Point;
029: import com.vividsolutions.jts.geom.Polygon;
030:
031: /**
032: * PointCoveredByLineValidation purpose.
033: *
034: * <p>
035: * Checks to ensure the Point is covered by the Polygon.
036: * </p>
037: *
038: * @author dzwiers, 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/PointCoveredByPolygonValidation.java $
041: * @version $Id: PointCoveredByPolygonValidation.java 22666 2006-11-09 03:50:28Z jgarnett $
042: */
043: public class PointCoveredByPolygonValidation extends
044: PointPolygonAbstractValidation {
045: /**
046: * PointCoveredByLineValidation constructor.
047: *
048: * <p>
049: * Super
050: * </p>
051: */
052: public PointCoveredByPolygonValidation() {
053: super ();
054: }
055:
056: /**
057: * Ensure Point is covered by the Polygon.
058: *
059: * <p></p>
060: *
061: * @param layers a HashMap of key="TypeName" value="FeatureSource"
062: * @param envelope The bounding box of modified features
063: * @param results Storage for the error and warning messages
064: *
065: * @return True if no features intersect. If they do then the validation
066: * failed.
067: *
068: * @throws Exception DOCUMENT ME!
069: *
070: * @see org.geotools.validation.IntegrityValidation#validate(java.util.Map,
071: * com.vividsolutions.jts.geom.Envelope,
072: * org.geotools.validation.ValidationResults)
073: */
074: public boolean validate(Map layers, Envelope envelope,
075: ValidationResults results) throws Exception {
076: FeatureSource pointSource = (FeatureSource) layers
077: .get(getPointTypeRef());
078: FeatureSource polySource = (FeatureSource) layers
079: .get(getRestrictedPolygonTypeRef());
080:
081: Object[] polys = polySource.getFeatures().toArray();
082: Object[] points = pointSource.getFeatures().toArray();
083:
084: if (!envelope.contains(polySource.getBounds())) {
085: results
086: .error((Feature) polys[0],
087: "Point Feature Source is not contained within the Envelope provided.");
088:
089: return false;
090: }
091:
092: if (!envelope.contains(pointSource.getBounds())) {
093: results
094: .error((Feature) points[0],
095: "Line Feature Source is not contained within the Envelope provided.");
096:
097: return false;
098: }
099:
100: for (int i = 0; i < points.length; i++) {
101: Feature tmp = (Feature) points[i];
102: Geometry gt = tmp.getDefaultGeometry();
103:
104: if (gt instanceof Polygon) {
105: Polygon ls = (Polygon) gt;
106:
107: boolean r = false;
108: for (int j = 0; j < polys.length && !r; j++) {
109: Feature tmp2 = (Feature) polys[j];
110: Geometry gt2 = tmp2.getDefaultGeometry();
111:
112: if (gt2 instanceof Point) {
113: Point pt = (Point) gt2;
114: if (!ls.contains(pt)) {
115: r = true;
116: }
117: }
118: }
119: if (!r) {
120: results
121: .error(tmp,
122: "Polygon does not contained one of the specified points.");
123: return false;
124: }
125: }
126: }
127:
128: return true;
129: }
130: }
|