001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program 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
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.qa.diff;
034:
035: import java.util.*;
036: import com.vividsolutions.jts.geom.*;
037: import com.vividsolutions.jump.feature.*;
038: import com.vividsolutions.jump.util.CoordinateArrays;
039: import com.vividsolutions.jump.geom.*;
040: import com.vividsolutions.jump.algorithm.VertexHausdorffDistance;
041:
042: public class UniqueSegmentsWithToleranceFinder {
043:
044: public static double maximumDistance(LineSegment seg1,
045: LineSegment seg2) {
046: double maxDist = 0.0;
047: double dist;
048: dist = seg1.p0.distance(seg2.p0);
049: maxDist = dist;
050:
051: dist = seg1.p0.distance(seg2.p1);
052: if (dist > maxDist)
053: maxDist = dist;
054:
055: dist = seg1.p1.distance(seg2.p0);
056: if (dist > maxDist)
057: maxDist = dist;
058:
059: dist = seg1.p1.distance(seg2.p1);
060: if (dist > maxDist)
061: maxDist = dist;
062:
063: return maxDist;
064: }
065:
066: private FeatureCollection queryFC;
067: private SegmentIndex segIndex;
068: private List resultSegs = new ArrayList();
069: private Envelope queryEnv = new Envelope();
070:
071: public UniqueSegmentsWithToleranceFinder(FeatureCollection fc0,
072: FeatureCollection fc1) {
073: queryFC = fc0;
074: segIndex = new SegmentIndex(fc1);
075: }
076:
077: public List findUniqueSegments(double tolerance) {
078: for (Iterator it = queryFC.iterator(); it.hasNext();) {
079: Feature f = (Feature) it.next();
080: Geometry geom = f.getGeometry();
081: findUniqueSegments(geom, tolerance);
082: }
083: return resultSegs;
084: }
085:
086: public void findUniqueSegments(Geometry geom, double tolerance) {
087: List coordArrays = CoordinateArrays.toCoordinateArrays(geom,
088: false);
089: for (Iterator i = coordArrays.iterator(); i.hasNext();) {
090: findUniqueSegments((Coordinate[]) i.next(), tolerance);
091: }
092: }
093:
094: public void findUniqueSegments(Coordinate[] coord, double tolerance) {
095: for (int i = 0; i < coord.length - 1; i++) {
096: LineSegment querySeg = new LineSegment(coord[i],
097: coord[i + 1]);
098: querySeg.normalize();
099: queryEnv.init(querySeg.p0, querySeg.p1);
100: Envelope queryEnvExp = EnvelopeUtil.expand(queryEnv,
101: tolerance);
102: List testSegs = segIndex.query(queryEnvExp);
103: if (!hasSegmentWithinTolerance(querySeg, testSegs,
104: tolerance))
105: resultSegs.add(querySeg);
106: }
107: }
108:
109: private boolean hasSegmentWithinTolerance(LineSegment querySeg,
110: List testSegs, double tolerance) {
111: for (Iterator i = testSegs.iterator(); i.hasNext();) {
112: LineSegment testSeg = (LineSegment) i.next();
113: VertexHausdorffDistance vhd = new VertexHausdorffDistance(
114: querySeg, testSeg);
115: if (vhd.distance() < tolerance)
116: return true;
117: //if (maximumDistance(querySeg, testSeg) < tolerance)
118: // return true;
119: }
120: return false;
121: }
122:
123: }
|