Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / vividsolutions / jts / algorithms / SnapPointLocator.java @ 10627

History | View | Annotate | Download (6.98 KB)

1
/*
2
 * Created on 06-oct-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: SnapPointLocator.java 10627 2007-03-06 17:10:21Z caballero $
47
* $Log$
48
* Revision 1.2  2007-03-06 17:08:55  caballero
49
* Exceptions
50
*
51
* Revision 1.1  2006/12/04 19:29:31  azabala
52
* *** empty log message ***
53
*
54
* Revision 1.1  2006/10/17 18:25:53  azabala
55
* *** empty log message ***
56
*
57
* Revision 1.1  2006/10/09 19:10:56  azabala
58
* First version in CVS
59
*
60
*
61
*/
62
package com.vividsolutions.jts.algorithms;
63

    
64
import java.util.Iterator;
65

    
66
import com.vividsolutions.jts.geom.Coordinate;
67
import com.vividsolutions.jts.geom.Geometry;
68
import com.vividsolutions.jts.geom.GeometryCollection;
69
import com.vividsolutions.jts.geom.GeometryCollectionIterator;
70
import com.vividsolutions.jts.geom.LineString;
71
import com.vividsolutions.jts.geom.LinearRing;
72
import com.vividsolutions.jts.geom.Location;
73
import com.vividsolutions.jts.geom.MultiLineString;
74
import com.vividsolutions.jts.geom.MultiPolygon;
75
import com.vividsolutions.jts.geom.Polygon;
76
import com.vividsolutions.jts.geomgraph.SnappingGeometryGraph;
77

    
78
public class SnapPointLocator extends com.vividsolutions.jts.algorithm.PointLocator {
79
         private boolean isIn;         // true if the point lies in or on any Geometry element
80
          private int numBoundaries;    // the number of sub-elements whose boundaries the point lies in
81

    
82
          public SnapPointLocator() {
83
          }
84

    
85
          /**
86
           * Convenience method to test a point for intersection with
87
           * a Geometry
88
           * @param p the coordinate to test
89
           * @param geom the Geometry to test
90
           * @return <code>true</code> if the point is in the interior or boundary of the Geometry
91
           */
92
          public boolean intersects(Coordinate p, Geometry geom, double snapTolerance)
93
          {
94
            return locate(p, geom, snapTolerance) != Location.EXTERIOR;
95
          }
96

    
97
          /**
98
           * Computes the topological relationship ({@link Location}) of a single point
99
           * to a Geometry.
100
           * It handles both single-element
101
           * and multi-element Geometries.
102
           * The algorithm for multi-part Geometries
103
           * takes into account the SFS Boundary Determination Rule.
104
           *
105
           * @return the {@link Location} of the point relative to the input Geometry
106
           */
107
          public int locate(Coordinate p, Geometry geom, double snapTolerance)
108
          {
109
            if (geom.isEmpty()) return Location.EXTERIOR;
110

    
111
            if (geom instanceof LinearRing) {
112
              return locate(p, (LinearRing) geom, snapTolerance);
113
            }
114
            if (geom instanceof LineString) {
115
              return locate(p, (LineString) geom, snapTolerance);
116
            }
117
            else if (geom instanceof Polygon) {
118
              return locate(p, (Polygon) geom, snapTolerance);
119
            }
120

    
121
            isIn = false;
122
            numBoundaries = 0;
123
            computeLocation(p, geom, snapTolerance);
124
            if (SnappingGeometryGraph.isInBoundary(numBoundaries)) 
125
                    return Location.BOUNDARY;
126
            if (numBoundaries > 0 || isIn) 
127
                    return Location.INTERIOR;
128
            return Location.EXTERIOR;
129
          }
130

    
131
          private void computeLocation(Coordinate p, Geometry geom, double snapTolerance)
132
          {
133
            if (geom instanceof LinearRing) {
134
              updateLocationInfo(locate(p, (LinearRing) geom, snapTolerance));
135
            }
136
            if (geom instanceof LineString) {
137
              updateLocationInfo(locate(p, (LineString) geom, snapTolerance));
138
            }
139
            else if (geom instanceof Polygon) {
140
              updateLocationInfo(locate(p, (Polygon) geom, snapTolerance));
141
            }
142
            else if (geom instanceof MultiLineString) {
143
              MultiLineString ml = (MultiLineString) geom;
144
              for (int i = 0; i < ml.getNumGeometries(); i++) {
145
                LineString l = (LineString) ml.getGeometryN(i);
146
                updateLocationInfo(locate(p, l, snapTolerance));
147
              }
148
            }
149
            else if (geom instanceof MultiPolygon) {
150
              MultiPolygon mpoly = (MultiPolygon) geom;
151
              for (int i = 0; i < mpoly.getNumGeometries(); i++) {
152
                Polygon poly = (Polygon) mpoly.getGeometryN(i);
153
                updateLocationInfo(locate(p, poly, snapTolerance));
154
              }
155
            }
156
            else if (geom instanceof GeometryCollection) {
157
              Iterator geomi = new GeometryCollectionIterator((GeometryCollection) geom);
158
              while (geomi.hasNext()) {
159
                Geometry g2 = (Geometry) geomi.next();
160
                if (g2 != geom)
161
                  computeLocation(p, g2, snapTolerance);
162
              }
163
            }
164
          }
165

    
166
          private void updateLocationInfo(int loc)
167
          {
168
            if (loc == Location.INTERIOR) isIn = true;
169
            if (loc == Location.BOUNDARY) numBoundaries++;
170
          }
171

    
172
          private int locate(Coordinate p, LineString l, double snapTolerance)
173
          {
174
            Coordinate[] pt = l.getCoordinates();
175
            if (! l.isClosed()) {
176
              if (p.distance(pt[0]) <= snapTolerance
177
                  || p.distance(pt[pt.length - 1]) <= snapTolerance ) {
178
                return Location.BOUNDARY;
179
              }
180
            }
181
            if (SnapCGAlgorithms.isOnLine(p, pt, snapTolerance))
182
              return Location.INTERIOR;
183
            return Location.EXTERIOR;
184
          }
185

    
186
          private int locate(Coordinate p, LinearRing ring, double snapTolerance)
187
          {
188
            // can this test be folded into isPointInRing ?
189
            if (SnapCGAlgorithms.isOnLine(p, ring.getCoordinates())) {
190
              return Location.BOUNDARY;
191
            }
192
            if (SnapCGAlgorithms.isPointInRing(p, ring.getCoordinates(), snapTolerance))
193
              return Location.INTERIOR;
194
            return Location.EXTERIOR;
195
          }
196

    
197
          private int locate(Coordinate p, Polygon poly, double snapTolerance)
198
          {
199
            if (poly.isEmpty()) return Location.EXTERIOR;
200
            LinearRing shell = (LinearRing) poly.getExteriorRing();
201

    
202
            int shellLoc = locate(p, shell, snapTolerance);
203
            if (shellLoc == Location.EXTERIOR) return Location.EXTERIOR;
204
            if (shellLoc == Location.BOUNDARY) return Location.BOUNDARY;
205
            // now test if the point lies in or on the holes
206
            for (int i = 0; i < poly.getNumInteriorRing(); i++) {
207
              LinearRing hole = (LinearRing) poly.getInteriorRingN(i);
208
              int holeLoc = locate(p, hole, snapTolerance);
209
              if (holeLoc == Location.INTERIOR) return Location.EXTERIOR;
210
              if (holeLoc == Location.BOUNDARY) return Location.BOUNDARY;
211
            }
212
            return Location.INTERIOR;
213
          }
214
}
215