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

« back to all changes in this revision

Viewing changes to src/com/vividsolutions/jts/algorithm/InteriorPointLine.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
 * 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.
 
6
 *
 
7
 * Copyright (C) 2001 Vivid Solutions
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
22
 *
 
23
 * For more information, contact:
 
24
 *
 
25
 *     Vivid Solutions
 
26
 *     Suite #1A
 
27
 *     2328 Government Street
 
28
 *     Victoria BC  V8T 5G5
 
29
 *     Canada
 
30
 *
 
31
 *     (250)385-6040
 
32
 *     www.vividsolutions.com
 
33
 */
 
34
package com.vividsolutions.jts.algorithm;
 
35
 
 
36
import com.vividsolutions.jts.geom.*;
 
37
 
 
38
/**
 
39
 * Computes a point in the interior of an linear geometry.
 
40
 * <h2>Algorithm</h2>
 
41
 * <ul>
 
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.
 
46
 * </ul>
 
47
 *
 
48
 * @version 1.6
 
49
 */
 
50
public class InteriorPointLine {
 
51
 
 
52
  private Coordinate centroid;
 
53
  private double minDistance = Double.MAX_VALUE;
 
54
 
 
55
  private Coordinate interiorPoint = null;
 
56
 
 
57
  public InteriorPointLine(Geometry g)
 
58
  {
 
59
    centroid = g.getCentroid().getCoordinate();
 
60
    addInterior(g);
 
61
    if (interiorPoint == null)
 
62
      addEndpoints(g);
 
63
  }
 
64
 
 
65
  public Coordinate getInteriorPoint()
 
66
  {
 
67
    return interiorPoint;
 
68
  }
 
69
 
 
70
  /**
 
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
 
75
   */
 
76
  private void addInterior(Geometry geom)
 
77
  {
 
78
    if (geom instanceof LineString) {
 
79
      addInterior(geom.getCoordinates());
 
80
    }
 
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));
 
85
      }
 
86
    }
 
87
  }
 
88
  private void addInterior(Coordinate[] pts)
 
89
  {
 
90
    for (int i = 1; i < pts.length - 1; i++) {
 
91
      add(pts[i]);
 
92
    }
 
93
  }
 
94
  /**
 
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
 
99
   */
 
100
  private void addEndpoints(Geometry geom)
 
101
  {
 
102
    if (geom instanceof LineString) {
 
103
      addEndpoints(geom.getCoordinates());
 
104
    }
 
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));
 
109
      }
 
110
    }
 
111
  }
 
112
  private void addEndpoints(Coordinate[] pts)
 
113
  {
 
114
    add(pts[0]);
 
115
    add(pts[pts.length - 1]);
 
116
  }
 
117
 
 
118
  private void add(Coordinate point)
 
119
  {
 
120
    double dist = point.distance(centroid);
 
121
    if (dist < minDistance) {
 
122
      interiorPoint = new Coordinate(point);
 
123
      minDistance = dist;
 
124
    }
 
125
  }
 
126
 
 
127
}