1 |
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * The JCS Conflation Suite (JCS) is a library of Java classes that
|
---|
5 | * can be used to build automated or semi-automated conflation solutions.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2003 Vivid Solutions
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU General Public License
|
---|
11 | * as published by the Free Software Foundation; either version 2
|
---|
12 | * of the License, or (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program 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
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with this program; 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 |
|
---|
35 | package com.vividsolutions.jcs.algorithm;
|
---|
36 |
|
---|
37 | import com.vividsolutions.jts.geom.*;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Computes the Euclidean distance (L2 metric) from a Point to a Geometry.
|
---|
41 | * Also computes two points which are separated by the distance.
|
---|
42 | */
|
---|
43 | public class EuclideanDistanceToPoint {
|
---|
44 |
|
---|
45 | // used for point-line distance calculation
|
---|
46 | private static LineSegment tempSegment = new LineSegment();
|
---|
47 |
|
---|
48 | public EuclideanDistanceToPoint() {
|
---|
49 | }
|
---|
50 |
|
---|
51 | public static void computeDistance(Geometry geom, Coordinate pt, PointPairDistance ptDist)
|
---|
52 | {
|
---|
53 | if (geom instanceof LineString) {
|
---|
54 | computeDistance((LineString) geom, pt, ptDist);
|
---|
55 | }
|
---|
56 | else if (geom instanceof Polygon) {
|
---|
57 | computeDistance((Polygon) geom, pt, ptDist);
|
---|
58 | }
|
---|
59 | else if (geom instanceof GeometryCollection) {
|
---|
60 | GeometryCollection gc = (GeometryCollection) geom;
|
---|
61 | for (int i = 0; i < gc.getNumGeometries(); i++) {
|
---|
62 | Geometry g = gc.getGeometryN(i);
|
---|
63 | computeDistance(g, pt, ptDist);
|
---|
64 | }
|
---|
65 | }
|
---|
66 | else { // assume geom is Point
|
---|
67 | ptDist.setMinimum(geom.getCoordinate(), pt);
|
---|
68 | }
|
---|
69 | }
|
---|
70 | public static void computeDistance(LineString line, Coordinate pt, PointPairDistance ptDist)
|
---|
71 | {
|
---|
72 | Coordinate[] coords = line.getCoordinates();
|
---|
73 | for (int i = 0; i < coords.length - 1; i++) {
|
---|
74 | tempSegment.setCoordinates(coords[i], coords[i + 1]);
|
---|
75 | // this is somewhat inefficient - could do better
|
---|
76 | Coordinate closestPt = tempSegment.closestPoint(pt);
|
---|
77 | ptDist.setMinimum(closestPt, pt);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static void computeDistance(LineSegment segment, Coordinate pt, PointPairDistance ptDist)
|
---|
82 | {
|
---|
83 | Coordinate closestPt = segment.closestPoint(pt);
|
---|
84 | ptDist.setMinimum(closestPt, pt);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static void computeDistance(Polygon poly, Coordinate pt, PointPairDistance ptDist)
|
---|
88 | {
|
---|
89 | computeDistance(poly.getExteriorRing(), pt, ptDist);
|
---|
90 | for (int i = 0; i < poly.getNumInteriorRing(); i++) {
|
---|
91 | computeDistance(poly.getInteriorRingN(i), pt, ptDist);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|