Statistics
| Revision:

root / trunk / libraries / libTopology / src / com / vividsolutions / jcs / conflate / coverage / snap / CoordinateSnapper.java @ 22873

History | View | Annotate | Download (2.4 KB)

1
/*
2
 * The JCS Conflation Suite (JCS) is a library of Java classes that
3
 * can be used to build automated or semi-automated conflation solutions.
4
 *
5
 * Copyright (C) 2003 Vivid Solutions
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * Vivid Solutions
24
 * Suite #1A
25
 * 2328 Government Street
26
 * Victoria BC  V8T 5G5
27
 * Canada
28
 *
29
 * (250)385-6040
30
 * www.vividsolutions.com
31
 */
32
package com.vividsolutions.jcs.conflate.coverage.snap;
33

    
34
import com.vividsolutions.jts.geom.*;
35
import java.util.*;
36

    
37
/**
38
 * Note: this class is not thread-safe.
39
 */
40
public class CoordinateSnapper {
41

    
42
  public Coordinate closestPt(List closePts, Coordinate pt)
43
  {
44
    Coordinate closestPt = null;
45
    double minDistance = 0.0;
46

    
47
    for (Iterator i = closePts.iterator(); i.hasNext(); ) {
48
      Coordinate closePt = (Coordinate) i.next();
49
      double distance = closePt.distance(pt);
50
      if (closestPt == null || distance < minDistance) {
51
        closestPt = closePt;
52
        minDistance = distance;
53
      }
54
    }
55
    return closestPt;
56
  }
57

    
58
  private SlowPointIndex ptIndex;
59
  private Envelope queryEnv = new Envelope();
60

    
61
  public CoordinateSnapper(SlowPointIndex ptIndex)
62
  {
63
    this.ptIndex = ptIndex;
64
  }
65

    
66
  /**
67
   * Compute the closest point within the given distance, if any.
68
   * @param pt
69
   * @param distance
70
   * @return the reference point to snap to, if any
71
   *   the original point if there were no reference points within the given distance
72
   */
73
  public Coordinate snap(Coordinate pt, double distance)
74
  {
75
    queryEnv.init(pt.x - distance, pt.x + distance, pt.y - distance, pt.y + distance);
76
    List closePts = ptIndex.query(queryEnv);
77
    Coordinate closestPt = closestPt(closePts, pt);
78
    if (closestPt == null) return pt;
79
    return closestPt;
80
  }
81

    
82

    
83
}