root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / util / SnappingCoordinateMap.java @ 8712
History | View | Annotate | Download (4.06 KB)
1 |
/*
|
---|---|
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 8712 2006-11-13 20:41:08Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.2 2006-11-13 20:41:08 azabala
|
49 |
* *** empty log message ***
|
50 |
*
|
51 |
* Revision 1.1 2006/11/09 21:08:32 azabala
|
52 |
* *** empty log message ***
|
53 |
*
|
54 |
*
|
55 |
*/
|
56 |
package com.iver.cit.gvsig.util; |
57 |
|
58 |
import java.util.Comparator; |
59 |
import java.util.Hashtable; |
60 |
|
61 |
import com.vividsolutions.jts.geom.Coordinate; |
62 |
import com.vividsolutions.jts.geomgraph.Node; |
63 |
|
64 |
public class SnappingCoordinateMap extends Hashtable { |
65 |
class SnapCoordinate extends Coordinate { |
66 |
public SnapCoordinate(Coordinate arg0) {
|
67 |
super(arg0);
|
68 |
} |
69 |
|
70 |
public boolean equals(Object obj) { |
71 |
if(! (obj instanceof SnapCoordinate)) |
72 |
return false; |
73 |
SnapCoordinate other = (SnapCoordinate) obj; |
74 |
return other.distance(this) <= snapTolerance; |
75 |
} |
76 |
|
77 |
public int hashCode() { |
78 |
int result = 17; |
79 |
double xs = simplify(x);
|
80 |
double ys = simplify(y);
|
81 |
result = 37 * result + hashCode(xs);
|
82 |
result = 37 * result + hashCode(ys);
|
83 |
return result;
|
84 |
|
85 |
// return 1; // esto no es eficiente
|
86 |
} |
87 |
|
88 |
public double simplify(double coordinate){ |
89 |
if(scaleFactor == 0d) |
90 |
return coordinate;
|
91 |
return Math.round(coordinate * scaleFactor) / scaleFactor; |
92 |
} |
93 |
} |
94 |
|
95 |
|
96 |
|
97 |
private double snapTolerance; |
98 |
private double scaleFactor; |
99 |
|
100 |
public SnappingCoordinateMap(double snapTolerance) { |
101 |
super();
|
102 |
this.snapTolerance = snapTolerance;
|
103 |
if(snapTolerance != 0d) |
104 |
this.scaleFactor = 1d / snapTolerance; |
105 |
} |
106 |
|
107 |
class MinDistCoordComparator implements Comparator { |
108 |
Coordinate coord; |
109 |
|
110 |
MinDistCoordComparator(Coordinate coord) { |
111 |
this.coord = coord;
|
112 |
} |
113 |
|
114 |
public int compare(Object arg0, Object arg1) { |
115 |
Coordinate c1 = ((Node) arg0).getCoordinate(); |
116 |
Coordinate c2 = ((Node) arg1).getCoordinate(); |
117 |
|
118 |
double d1 = c1.distance(coord);
|
119 |
double d2 = c2.distance(coord);
|
120 |
|
121 |
if (d1 < d2)
|
122 |
return 1; |
123 |
if (d1 > d2)
|
124 |
return -1; |
125 |
else
|
126 |
return 0; |
127 |
} |
128 |
} |
129 |
|
130 |
|
131 |
public Object put(Object key, Object obj){ |
132 |
if(! (key instanceof Coordinate) ) |
133 |
return null; |
134 |
return super.put(new SnapCoordinate((Coordinate)key), |
135 |
obj); |
136 |
} |
137 |
|
138 |
public Object get(Object key){ |
139 |
if(! (key instanceof Coordinate) ) |
140 |
return null; |
141 |
return super.get(new SnapCoordinate((Coordinate)key)); |
142 |
} |
143 |
|
144 |
public boolean containsKey(Object key){ |
145 |
if(! (key instanceof Coordinate) ) |
146 |
return false; |
147 |
return super.containsKey(new SnapCoordinate((Coordinate)key)); |
148 |
} |
149 |
|
150 |
public static void main(String[] args){ |
151 |
SnappingCoordinateMap map = |
152 |
new SnappingCoordinateMap(0.1); |
153 |
Coordinate c0 = new Coordinate(0, 0); |
154 |
Coordinate c1 = new Coordinate(0.01, 0.01); |
155 |
Coordinate c2 = new Coordinate(0.31, 0.41); |
156 |
Coordinate c3 = new Coordinate(0.29, 0.39); |
157 |
Coordinate c4 = new Coordinate(0.299, 0.411); |
158 |
map.put(c0, c0); |
159 |
map.put(c1, c1); |
160 |
map.put(c2, c2); |
161 |
map.put(c3, c3); |
162 |
map.put(c4, c4); |
163 |
System.out.println(map.size());
|
164 |
java.util.Set values = map.entrySet(); |
165 |
System.out.println(values.size());
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
} |
171 |
|
172 |
} |