Class GeometryLocation

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.operation.distance;
14  
15 import org.locationtech.jts.geom.Coordinate;
16 import org.locationtech.jts.geom.Geometry;
17 import org.locationtech.jts.io.WKTWriter;
18  
19 /**
20  * Represents the location of a point on a Geometry.
21  * Maintains both the actual point location 
22  * (which may not be exact, if the point is not a vertex) 
23  * as well as information about the component
24  * and segment index where the point occurs.
25  * Locations inside area Geometrys will not have an associated segment index,
26  * so in this case the segment index will have the sentinel value of 
27  * {@link #INSIDE_AREA}.
28  *
29  * @version 1.7
30  */
31 public class GeometryLocation
32 {
33   /**
34    * A special value of segmentIndex used for locations inside area geometries. 
35    * These locations are not located on a segment, 
36    * and thus do not have an associated segment index.
37    */
38   public static final int INSIDE_AREA = -1;
39  
40   private Geometry component = null;
41   private int segIndex;
42   private Coordinate pt = null;
43  
44   /**
45    * Constructs a GeometryLocation specifying a point on a geometry, as well as the 
46    * segment that the point is on 
47    * (or {@link #INSIDE_AREA} if the point is not on a segment).
48    * 
49    * @param component the component of the geometry containing the point
50    * @param segIndex the segment index of the location, or INSIDE_AREA
51    * @param pt the coordinate of the location
52    */
53   public GeometryLocation(Geometry component, int segIndex, Coordinate pt)
54   {
55     this.component = component;
56     this.segIndex = segIndex;
57     this.pt = pt;
58   }
59  
60   /**
61    * Constructs a GeometryLocation specifying a point inside an area geometry.
62    * 
63    * @param component the component of the geometry containing the point
64    * @param pt the coordinate of the location
65    */  
66   public GeometryLocation(Geometry component,Coordinate pt)
67   {
68     this(component, INSIDE_AREA, pt);
69   }
70  
71   /**
72    * Returns the geometry component on (or in) which this location occurs.
73    */
74   public Geometry getGeometryComponent() { return component; }
75   
76   /**
77    * Returns the segment index for this location. If the location is inside an
78    * area, the index will have the value {@link #INSIDE_AREA};
79    *
80    * @return the segment index for the location, or INSIDE_AREA
81    */
82   public int getSegmentIndex() { return segIndex; }
83   
84   /**
85    * Returns the {@link Coordinate} of this location.
86    */
87   public Coordinate getCoordinate() { return pt; }
88   
89   /**
90    * Tests whether this location represents a point inside an area geometry.
91    */
92   public boolean isInsideArea() { return segIndex == INSIDE_AREA; }
93   
94   public String toString() {
95     return component.getGeometryType() 
96         + "[" + segIndex + "]" 
97         + "-" + WKTWriter.toPoint(pt);
98   }
99 }
100