svn-gvsig-desktop / tags / J2ME_compat_v1_2_Build_1209 / libraries / libFMap / src / com / iver / cit / gvsig / util / SnappingCoordinateMap.java @ 19509
History | View | Annotate | Download (4.67 KB)
1 | 19509 | jcarrasco | /*
|
---|---|---|---|
2 | * Created on 09-nov-2006
|
||
3 | *
|
||
4 | * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
5 | *
|
||
6 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
7 | *
|
||
8 | * This program is free software; you can redistribute it and/or
|
||
9 | * modify it under the terms of the GNU General Public License
|
||
10 | * as published by the Free Software Foundation; either version 2
|
||
11 | * of the License, or (at your option) any later version.
|
||
12 | *
|
||
13 | * This program is distributed in the hope that it will be useful,
|
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
16 | * GNU General Public License for more details.
|
||
17 | *
|
||
18 | * You should have received a copy of the GNU General Public License
|
||
19 | * along with this program; if not, write to the Free Software
|
||
20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
21 | *
|
||
22 | * For more information, contact:
|
||
23 | *
|
||
24 | * Generalitat Valenciana
|
||
25 | * Conselleria d'Infraestructures i Transport
|
||
26 | * Av. Blasco Ib??ez, 50
|
||
27 | * 46010 VALENCIA
|
||
28 | * SPAIN
|
||
29 | *
|
||
30 | * +34 963862235
|
||
31 | * gvsig@gva.es
|
||
32 | * www.gvsig.gva.es
|
||
33 | *
|
||
34 | * or
|
||
35 | *
|
||
36 | * IVER T.I. S.A
|
||
37 | * Salamanca 50
|
||
38 | * 46005 Valencia
|
||
39 | * Spain
|
||
40 | *
|
||
41 | * +34 963163400
|
||
42 | * dac@iver.es
|
||
43 | */
|
||
44 | /* CVS MESSAGES:
|
||
45 | *
|
||
46 | * $Id: SnappingCoordinateMap.java 13853 2007-09-19 15:28:42Z azabala $
|
||
47 | * $Log$
|
||
48 | * Revision 1.2 2007-09-19 15:28:42 azabala
|
||
49 | * added a fixed hashCode (until we fix the requeriment of compute the same hashCode for all points in the same cluster circle)
|
||
50 | *
|
||
51 | * Revision 1.1 2006/12/04 19:50:43 azabala
|
||
52 | * *** empty log message ***
|
||
53 | *
|
||
54 | * Revision 1.2 2006/11/13 20:41:08 azabala
|
||
55 | * *** empty log message ***
|
||
56 | *
|
||
57 | * Revision 1.1 2006/11/09 21:08:32 azabala
|
||
58 | * *** empty log message ***
|
||
59 | *
|
||
60 | *
|
||
61 | */
|
||
62 | package com.iver.cit.gvsig.util; |
||
63 | |||
64 | import java.util.Comparator; |
||
65 | import java.util.Hashtable; |
||
66 | |||
67 | import com.vividsolutions.jts.geom.Coordinate; |
||
68 | import com.vividsolutions.jts.geomgraph.Node; |
||
69 | |||
70 | public class SnappingCoordinateMap extends Hashtable { |
||
71 | class SnapCoordinate extends Coordinate { |
||
72 | public SnapCoordinate(Coordinate arg0) {
|
||
73 | super(arg0);
|
||
74 | } |
||
75 | |||
76 | public boolean equals(Object obj) { |
||
77 | if(! (obj instanceof SnapCoordinate)) |
||
78 | return false; |
||
79 | SnapCoordinate other = (SnapCoordinate) obj; |
||
80 | return other.distance(this) <= snapTolerance; |
||
81 | } |
||
82 | |||
83 | public int hashCode() { |
||
84 | // int result = 17;
|
||
85 | // double xs = simplify(x);
|
||
86 | // double ys = simplify(y);
|
||
87 | // result = 37 * result + hashCode(xs);
|
||
88 | // result = 37 * result + hashCode(ys);
|
||
89 | // return result;
|
||
90 | |||
91 | return 1; // this is not efficient. look for a hash algorithm to ensure |
||
92 | //all points in the same tolerance radius returns the same hash code
|
||
93 | } |
||
94 | |||
95 | public double simplify(double coordinate){ |
||
96 | if(scaleFactor == 0d) |
||
97 | return coordinate;
|
||
98 | return Math.round(coordinate * scaleFactor) / scaleFactor; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | |||
103 | |||
104 | private double snapTolerance; |
||
105 | private double scaleFactor; |
||
106 | |||
107 | public SnappingCoordinateMap(double snapTolerance) { |
||
108 | super();
|
||
109 | this.snapTolerance = snapTolerance;
|
||
110 | if(snapTolerance != 0d) |
||
111 | this.scaleFactor = 1d / snapTolerance; |
||
112 | } |
||
113 | |||
114 | class MinDistCoordComparator implements Comparator { |
||
115 | Coordinate coord; |
||
116 | |||
117 | MinDistCoordComparator(Coordinate coord) { |
||
118 | this.coord = coord;
|
||
119 | } |
||
120 | |||
121 | public int compare(Object arg0, Object arg1) { |
||
122 | Coordinate c1 = ((Node) arg0).getCoordinate(); |
||
123 | Coordinate c2 = ((Node) arg1).getCoordinate(); |
||
124 | |||
125 | double d1 = c1.distance(coord);
|
||
126 | double d2 = c2.distance(coord);
|
||
127 | |||
128 | if (d1 < d2)
|
||
129 | return 1; |
||
130 | if (d1 > d2)
|
||
131 | return -1; |
||
132 | else
|
||
133 | return 0; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | |||
138 | public Object put(Object key, Object obj){ |
||
139 | if(! (key instanceof Coordinate) ) |
||
140 | return null; |
||
141 | return super.put(new SnapCoordinate((Coordinate)key), |
||
142 | obj); |
||
143 | } |
||
144 | |||
145 | public Object get(Object key){ |
||
146 | if(! (key instanceof Coordinate) ) |
||
147 | return null; |
||
148 | return super.get(new SnapCoordinate((Coordinate)key)); |
||
149 | } |
||
150 | |||
151 | public boolean containsKey(Object key){ |
||
152 | if(! (key instanceof Coordinate) ) |
||
153 | return false; |
||
154 | return super.containsKey(new SnapCoordinate((Coordinate)key)); |
||
155 | } |
||
156 | |||
157 | public static void main(String[] args){ |
||
158 | SnappingCoordinateMap map = |
||
159 | new SnappingCoordinateMap(0.1); |
||
160 | Coordinate c0 = new Coordinate(0, 0); |
||
161 | Coordinate c1 = new Coordinate(0.01, 0.01); |
||
162 | Coordinate c2 = new Coordinate(0.31, 0.41); |
||
163 | Coordinate c3 = new Coordinate(0.29, 0.39); |
||
164 | Coordinate c4 = new Coordinate(0.299, 0.411); |
||
165 | map.put(c0, c0); |
||
166 | map.put(c1, c1); |
||
167 | map.put(c2, c2); |
||
168 | map.put(c3, c3); |
||
169 | map.put(c4, c4); |
||
170 | System.out.println(map.size());
|
||
171 | java.util.Set values = map.entrySet(); |
||
172 | System.out.println(values.size());
|
||
173 | |||
174 | |||
175 | |||
176 | |||
177 | } |
||
178 | |||
179 | } |