~ubuntu-branches/ubuntu/natty/jts/natty

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/operation/valid/ConnectedInteriorTester.java

  • Committer: Bazaar Package Importer
  • Author(s): Wolfgang Baer
  • Date: 2005-08-07 14:12:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050807141235-7hy3ll3xpq79djcb
Tags: upstream-1.6
ImportĀ upstreamĀ versionĀ 1.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
/*
 
4
 * The JTS Topology Suite is a collection of Java classes that
 
5
 * implement the fundamental operations required to validate a given
 
6
 * geo-spatial data set to a known topological specification.
 
7
 *
 
8
 * Copyright (C) 2001 Vivid Solutions
 
9
 *
 
10
 * This library is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Lesser General Public
 
12
 * License as published by the Free Software Foundation; either
 
13
 * version 2.1 of the License, or (at your option) any later version.
 
14
 *
 
15
 * This library is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * Lesser General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Lesser General Public
 
21
 * License along with this library; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 *
 
24
 * For more information, contact:
 
25
 *
 
26
 *     Vivid Solutions
 
27
 *     Suite #1A
 
28
 *     2328 Government Street
 
29
 *     Victoria BC  V8T 5G5
 
30
 *     Canada
 
31
 *
 
32
 *     (250)385-6040
 
33
 *     www.vividsolutions.com
 
34
 */
 
35
package com.vividsolutions.jts.operation.valid;
 
36
 
 
37
import java.util.*;
 
38
import com.vividsolutions.jts.algorithm.*;
 
39
import com.vividsolutions.jts.geomgraph.*;
 
40
import com.vividsolutions.jts.operation.overlay.*;
 
41
import com.vividsolutions.jts.geom.*;
 
42
import com.vividsolutions.jts.util.*;
 
43
 
 
44
/**
 
45
 * This class tests that the interior of an area {@link Geometry}
 
46
 *  ({@link Polygon}  or {@link MultiPolygon} )
 
47
 * is connected.  An area Geometry is invalid if the interior is disconnected.
 
48
 * This can happen if:
 
49
 * <ul>
 
50
 * <li>one or more holes either form a chain touching the shell at two places
 
51
 * <li>one or more holes form a ring around a portion of the interior
 
52
 * </ul>
 
53
 * If an inconsistency if found the location of the problem
 
54
 * is recorded.
 
55
 *
 
56
 * @version 1.6
 
57
 */
 
58
public class ConnectedInteriorTester {
 
59
 
 
60
  public static Coordinate findDifferentPoint(Coordinate[] coord, Coordinate pt)
 
61
  {
 
62
    for (int i = 0; i < coord.length; i++) {
 
63
      if (! coord[i].equals(pt))
 
64
        return coord[i];
 
65
    }
 
66
    return null;
 
67
  }
 
68
 
 
69
  private GeometryFactory geometryFactory = new GeometryFactory();
 
70
  private CGAlgorithms cga = new CGAlgorithms();
 
71
 
 
72
  private GeometryGraph geomGraph;
 
73
  // save a coordinate for any disconnected interior found
 
74
  // the coordinate will be somewhere on the ring surrounding the disconnected interior
 
75
  private Coordinate disconnectedRingcoord;
 
76
 
 
77
  public ConnectedInteriorTester(GeometryGraph geomGraph)
 
78
  {
 
79
    this.geomGraph = geomGraph;
 
80
  }
 
81
 
 
82
  public Coordinate getCoordinate() { return disconnectedRingcoord; }
 
83
 
 
84
  public boolean isInteriorsConnected()
 
85
  {
 
86
    // node the edges, in case holes touch the shell
 
87
    List splitEdges = new ArrayList();
 
88
    geomGraph.computeSplitEdges(splitEdges);
 
89
 
 
90
    // polygonize the edges
 
91
    PlanarGraph graph = new PlanarGraph(new OverlayNodeFactory());
 
92
    graph.addEdges(splitEdges);
 
93
    setAllEdgesInResult(graph);
 
94
    graph.linkAllDirectedEdges();
 
95
    List edgeRings = buildEdgeRings(graph.getEdgeEnds());
 
96
 
 
97
    /**
 
98
     * Mark all the edges for the edgeRings corresponding to the shells
 
99
     * of the input polygons.  Note only ONE ring gets marked for each shell.
 
100
     */
 
101
    visitShellInteriors(geomGraph.getGeometry(), graph);
 
102
 
 
103
    /**
 
104
     * If there are any unvisited shell edges
 
105
     * (i.e. a ring which is not a hole and which has the interior
 
106
     * of the parent area on the RHS)
 
107
     * this means that one or more holes must have split the interior of the
 
108
     * polygon into at least two pieces.  The polygon is thus invalid.
 
109
     */
 
110
    return ! hasUnvisitedShellEdge(edgeRings);
 
111
  }
 
112
 
 
113
  private void setAllEdgesInResult(PlanarGraph graph)
 
114
  {
 
115
    for (Iterator it = graph.getEdgeEnds().iterator(); it.hasNext(); ) {
 
116
      DirectedEdge de = (DirectedEdge) it.next();
 
117
      de.setInResult(true);
 
118
    }
 
119
  }
 
120
 
 
121
  /**
 
122
   * for all DirectedEdges in result, form them into EdgeRings
 
123
   */
 
124
  private List buildEdgeRings(Collection dirEdges)
 
125
  {
 
126
    List edgeRings = new ArrayList();
 
127
    for (Iterator it = dirEdges.iterator(); it.hasNext(); ) {
 
128
      DirectedEdge de = (DirectedEdge) it.next();
 
129
      // if this edge has not yet been processed
 
130
      if (de.getEdgeRing() == null) {
 
131
        EdgeRing er = new MaximalEdgeRing(de, geometryFactory, cga);
 
132
        edgeRings.add(er);
 
133
      }
 
134
    }
 
135
    return edgeRings;
 
136
  }
 
137
 
 
138
  /**
 
139
   * Mark all the edges for the edgeRings corresponding to the shells
 
140
   * of the input polygons.  Note only ONE ring gets marked for each shell.
 
141
   */
 
142
  private void visitShellInteriors(Geometry g, PlanarGraph graph)
 
143
  {
 
144
    if (g instanceof Polygon) {
 
145
      Polygon p = (Polygon) g;
 
146
      visitInteriorRing(p.getExteriorRing(), graph);
 
147
    }
 
148
    if (g instanceof MultiPolygon) {
 
149
      MultiPolygon mp = (MultiPolygon) g;
 
150
      for (int i = 0; i < mp.getNumGeometries(); i++) {
 
151
        Polygon p = (Polygon) mp.getGeometryN(i);
 
152
        visitInteriorRing(p.getExteriorRing(), graph);
 
153
      }
 
154
    }
 
155
  }
 
156
 
 
157
  private void visitInteriorRing(LineString ring, PlanarGraph graph)
 
158
  {
 
159
    Coordinate[] pts = ring.getCoordinates();
 
160
    Coordinate pt0 = pts[0];
 
161
    /**
 
162
     * Find first point in coord list different to initial point.
 
163
     * Need special check since the first point may be repeated.
 
164
     */
 
165
    Coordinate pt1 = findDifferentPoint(pts, pt0);
 
166
    Edge e = graph.findEdgeInSameDirection(pt0, pt1);
 
167
    DirectedEdge de = (DirectedEdge) graph.findEdgeEnd(e);
 
168
    DirectedEdge intDe = null;
 
169
    if (de.getLabel().getLocation(0, Position.RIGHT) == Location.INTERIOR) {
 
170
      intDe = de;
 
171
    }
 
172
    else if (de.getSym().getLabel().getLocation(0, Position.RIGHT) == Location.INTERIOR) {
 
173
      intDe = de.getSym();
 
174
    }
 
175
    Assert.isTrue(intDe != null, "unable to find dirEdge with Interior on RHS");
 
176
 
 
177
    visitLinkedDirectedEdges(intDe);
 
178
  }
 
179
  protected void visitLinkedDirectedEdges(DirectedEdge start)
 
180
  {
 
181
    DirectedEdge startDe = start;
 
182
    DirectedEdge de = start;
 
183
//Debug.println(de);
 
184
    do {
 
185
      Assert.isTrue(de != null, "found null Directed Edge");
 
186
      de.setVisited(true);
 
187
      de = de.getNext();
 
188
//Debug.println(de);
 
189
    } while (de != startDe);
 
190
  }
 
191
 
 
192
  /**
 
193
   * Check if any shell ring has an unvisited edge.
 
194
   * A shell ring is a ring which is not a hole and which has the interior
 
195
   * of the parent area on the RHS.
 
196
   * (Note that there may be non-hole rings with the interior on the LHS,
 
197
   * since the interior of holes will also be polygonized into CW rings
 
198
   * by the linkAllDirectedEdges() step)
 
199
   *
 
200
   * @return true if there is an unvisited edge in a non-hole ring
 
201
   */
 
202
  private boolean hasUnvisitedShellEdge(List edgeRings)
 
203
  {
 
204
    for (int i = 0; i < edgeRings.size(); i++) {
 
205
      EdgeRing er = (EdgeRing) edgeRings.get(i);
 
206
      if (er.isHole()) continue;
 
207
      List edges = er.getEdges();
 
208
      DirectedEdge de = (DirectedEdge) edges.get(0);
 
209
      // don't check CW rings which are holes
 
210
      if (de.getLabel().getLocation(0, Position.RIGHT) != Location.INTERIOR) continue;
 
211
 
 
212
      // must have a CW ring which surrounds the INT of the area, so check all
 
213
      // edges have been visited
 
214
      for (int j = 0; j < edges.size(); j++) {
 
215
        de = (DirectedEdge) edges.get(j);
 
216
//Debug.print("visted? "); Debug.println(de);
 
217
        if (! de.isVisited()) {
 
218
//Debug.print("not visited "); Debug.println(de);
 
219
          disconnectedRingcoord = de.getCoordinate();
 
220
          return true;
 
221
        }
 
222
      }
 
223
    }
 
224
    return false;
 
225
  }
 
226
}