Revision 15
trunk/org.gvsig.toolbox/org.gvsig.toolbox.algorithm/src/es/unex/sextante/vectorTools/snapPoints/SnapPointsAlgorithm.java | ||
---|---|---|
111 | 111 |
|
112 | 112 |
|
113 | 113 |
private Point getSnappedPoint(final double x, |
114 |
final double y) {
|
|
114 |
final double y) {
|
|
115 | 115 |
|
116 |
final GeometryFactory gf = new GeometryFactory();
|
|
117 |
final Coordinate c = new Coordinate(x, y);
|
|
118 |
Coordinate closestPoint = new Coordinate(x, y);
|
|
119 |
Coordinate closestPointToThisLine;
|
|
120 |
double minDist = m_dTolerance;
|
|
116 |
final GeometryFactory gf = new GeometryFactory();
|
|
117 |
final Coordinate c = new Coordinate(x, y);
|
|
118 |
Coordinate closestPoint = new Coordinate(x, y);
|
|
119 |
Coordinate closestPointToThisLine;
|
|
120 |
double minDist = m_dTolerance;
|
|
121 | 121 |
|
122 |
try { |
|
123 |
final List<Geometry> list = m_NNF.getClosestGeometries(c, m_dTolerance); |
|
124 |
for (int iGeom = 0; iGeom < list.size(); iGeom++) { |
|
125 |
final Geometry geom = list.get(iGeom); |
|
126 |
final int iNumGeom = geom.getNumGeometries(); |
|
127 |
for (int i = 0; i < iNumGeom; i++) { |
|
128 |
final Geometry subGeom = geom.getGeometryN(i); |
|
129 |
final Coordinate[] coords = subGeom.getCoordinates(); |
|
130 |
if (coords.length == 1){ |
|
131 |
//When snapping to point layer |
|
132 |
final double dist = c.distance(coords[0]); |
|
133 |
if ((dist < minDist)) { |
|
134 |
minDist = dist; |
|
135 |
closestPoint = coords[0]; |
|
122 |
try { |
|
123 |
final List<Geometry> list = m_NNF.getClosestGeometries(c, m_dTolerance); |
|
124 |
for (int iGeom = 0; iGeom < list.size(); iGeom++) { |
|
125 |
final Geometry geom = list.get(iGeom); |
|
126 |
final int iNumGeom = geom.getNumGeometries(); |
|
127 |
for (int i = 0; i < iNumGeom; i++) { |
|
128 |
final Geometry subGeom = geom.getGeometryN(i); |
|
129 |
final Coordinate[] coords = subGeom.getCoordinates(); |
|
130 |
if (coords.length == 1) { |
|
131 |
//When snapping to point layer |
|
132 |
final double dist = c.distance(coords[0]); |
|
133 |
if ((dist < minDist)) { |
|
134 |
minDist = dist; |
|
135 |
closestPoint = coords[0]; |
|
136 |
} |
|
137 |
} else { |
|
138 |
// When snapping to linestring or polygon layer |
|
139 |
for (int j = 0; j < coords.length - 1; j++) { |
|
140 |
final LineSegment line = new LineSegment(coords[j], coords[j + 1]); |
|
141 |
closestPointToThisLine = line.closestPoint(c); |
|
142 |
final double dist = c.distance(closestPointToThisLine); |
|
143 |
if ((dist < minDist)) { |
|
144 |
minDist = dist; |
|
145 |
closestPoint = closestPointToThisLine; |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
136 | 150 |
} |
137 |
} else { |
|
138 |
// When snapping to linestring or polygon layer |
|
139 |
for (int j = 0; j < coords.length - 1; j++) { |
|
140 |
final LineSegment line = new LineSegment(coords[j], coords[j + 1]); |
|
141 |
closestPointToThisLine = line.closestPoint(c); |
|
142 |
final double dist = c.distance(closestPointToThisLine); |
|
143 |
if ((dist < minDist)) { |
|
144 |
minDist = dist; |
|
145 |
closestPoint = closestPointToThisLine; |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
} |
|
151 |
} catch (final Exception e) { |
|
152 |
// will return closest point so far |
|
153 |
} |
|
151 |
} catch (final Exception e) { |
|
152 |
// will return closest point so far |
|
153 |
} |
|
154 | 154 |
|
155 |
return gf.createPoint(closestPoint);
|
|
155 |
return gf.createPoint(closestPoint);
|
|
156 | 156 |
|
157 | 157 |
} |
158 | 158 |
|
Also available in: Unified diff