3
* The JTS Topology Suite is a collection of Java classes that
4
* implement the fundamental operations required to validate a given
5
* geo-spatial data set to a known topological specification.
7
* Copyright (C) 2001 Vivid Solutions
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
* For more information, contact:
27
* 2328 Government Street
32
* www.vividsolutions.com
34
package com.vividsolutions.jts.algorithm;
36
import com.vividsolutions.jts.geom.*;
39
* Computes a point in the interior of an linear geometry.
42
* <li>Find an interior vertex which is closest to
43
* the centroid of the linestring.
44
* <li>If there is no interior vertex, find the endpoint which is
45
* closest to the centroid.
50
public class InteriorPointLine {
52
private Coordinate centroid;
53
private double minDistance = Double.MAX_VALUE;
55
private Coordinate interiorPoint = null;
57
public InteriorPointLine(Geometry g)
59
centroid = g.getCentroid().getCoordinate();
61
if (interiorPoint == null)
65
public Coordinate getInteriorPoint()
71
* Tests the interior vertices (if any)
72
* defined by a linear Geometry for the best inside point.
73
* If a Geometry is not of dimension 1 it is not tested.
74
* @param geom the geometry to add
76
private void addInterior(Geometry geom)
78
if (geom instanceof LineString) {
79
addInterior(geom.getCoordinates());
81
else if (geom instanceof GeometryCollection) {
82
GeometryCollection gc = (GeometryCollection) geom;
83
for (int i = 0; i < gc.getNumGeometries(); i++) {
84
addInterior(gc.getGeometryN(i));
88
private void addInterior(Coordinate[] pts)
90
for (int i = 1; i < pts.length - 1; i++) {
95
* Tests the endpoint vertices
96
* defined by a linear Geometry for the best inside point.
97
* If a Geometry is not of dimension 1 it is not tested.
98
* @param geom the geometry to add
100
private void addEndpoints(Geometry geom)
102
if (geom instanceof LineString) {
103
addEndpoints(geom.getCoordinates());
105
else if (geom instanceof GeometryCollection) {
106
GeometryCollection gc = (GeometryCollection) geom;
107
for (int i = 0; i < gc.getNumGeometries(); i++) {
108
addEndpoints(gc.getGeometryN(i));
112
private void addEndpoints(Coordinate[] pts)
115
add(pts[pts.length - 1]);
118
private void add(Coordinate point)
120
double dist = point.distance(centroid);
121
if (dist < minDistance) {
122
interiorPoint = new Coordinate(point);