001: /*
002: * The JTS Topology Suite is a collection of Java classes that
003: * implement the fundamental operations required to validate a given
004: * geo-spatial data set to a known topological specification.
005: *
006: * Copyright (C) 2001 Vivid Solutions
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: *
022: * For more information, contact:
023: *
024: * Vivid Solutions
025: * Suite #1A
026: * 2328 Government Street
027: * Victoria BC V8T 5G5
028: * Canada
029: *
030: * (250)385-6040
031: * www.vividsolutions.com
032: */
033: package com.vividsolutions.jtsexample.operation.distance;
034:
035: import com.vividsolutions.jts.geom.*;
036: import com.vividsolutions.jts.io.*;
037: import com.vividsolutions.jts.operation.distance.DistanceOp;
038:
039: /**
040: * Example of computing distance and closest points between geometries
041: * using the DistanceOp class.
042: *
043: * @version 1.7
044: */
045: public class ClosestPointExample {
046: static GeometryFactory fact = new GeometryFactory();
047: static WKTReader wktRdr = new WKTReader(fact);
048:
049: public static void main(String[] args) {
050: ClosestPointExample example = new ClosestPointExample();
051: example.run();
052: }
053:
054: public ClosestPointExample() {
055: }
056:
057: public void run() {
058: findClosestPoint(
059: "POLYGON ((200 180, 60 140, 60 260, 200 180))",
060: "POINT (140 280)");
061: findClosestPoint(
062: "POLYGON ((200 180, 60 140, 60 260, 200 180))",
063: "MULTIPOINT (140 280, 140 320)");
064: findClosestPoint(
065: "LINESTRING (100 100, 200 100, 200 200, 100 200, 100 100)",
066: "POINT (10 10)");
067: findClosestPoint("LINESTRING (100 100, 200 200)",
068: "LINESTRING (100 200, 200 100)");
069: findClosestPoint("LINESTRING (100 100, 200 200)",
070: "LINESTRING (150 121, 200 0)");
071: findClosestPoint(
072: "POLYGON (( 76 185, 125 283, 331 276, 324 122, 177 70, 184 155, 69 123, 76 185 ), ( 267 237, 148 248, 135 185, 223 189, 251 151, 286 183, 267 237 ))",
073: "LINESTRING ( 153 204, 185 224, 209 207, 238 222, 254 186 )");
074: findClosestPoint(
075: "POLYGON (( 76 185, 125 283, 331 276, 324 122, 177 70, 184 155, 69 123, 76 185 ), ( 267 237, 148 248, 135 185, 223 189, 251 151, 286 183, 267 237 ))",
076: "LINESTRING ( 120 215, 185 224, 209 207, 238 222, 254 186 )");
077: }
078:
079: public void findClosestPoint(String wktA, String wktB) {
080: System.out.println("-------------------------------------");
081: try {
082: Geometry A = wktRdr.read(wktA);
083: Geometry B = wktRdr.read(wktB);
084: System.out.println("Geometry A: " + A);
085: System.out.println("Geometry B: " + B);
086: DistanceOp distOp = new DistanceOp(A, B);
087:
088: double distance = distOp.distance();
089: System.out.println("Distance = " + distance);
090:
091: Coordinate[] closestPt = distOp.closestPoints();
092: LineString closestPtLine = fact.createLineString(closestPt);
093: System.out
094: .println("Closest points: " + closestPtLine
095: + " (distance = "
096: + closestPtLine.getLength() + ")");
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: }
100: }
101:
102: }
|