root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / snapping / GeometriesSnappingVisitor.java @ 38564
History | View | Annotate | Download (2.07 KB)
1 |
package org.gvsig.fmap.mapcontrol.tools.snapping; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.util.ArrayList; |
5 |
|
6 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
7 |
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperVectorial; |
8 |
|
9 |
import com.vividsolutions.jts.geom.Coordinate; |
10 |
import com.vividsolutions.jts.geom.Geometry; |
11 |
import com.vividsolutions.jts.geom.GeometryFactory; |
12 |
import com.vividsolutions.jts.index.ItemVisitor; |
13 |
|
14 |
|
15 |
/**
|
16 |
* SnappingVisitor with geometries.
|
17 |
*
|
18 |
* @author Vicente Caballero Navarro
|
19 |
*/
|
20 |
public class GeometriesSnappingVisitor extends SnappingVisitor |
21 |
implements ItemVisitor {
|
22 |
private ArrayList geometries = new ArrayList(); |
23 |
private GeometryFactory geometryFactory = new GeometryFactory(); |
24 |
|
25 |
public GeometriesSnappingVisitor(ISnapperVectorial snapper, Point2D point, |
26 |
double mapTolerance, Point2D lastPointEntered) { |
27 |
super(snapper, point, mapTolerance, lastPointEntered);
|
28 |
} |
29 |
|
30 |
public void visitItem(Object item) { |
31 |
try {
|
32 |
org.gvsig.fmap.geom.Geometry geom = (org.gvsig.fmap.geom.Geometry) item; |
33 |
|
34 |
Geometry geometry = (Geometry)geom.invokeOperation(ToJTS.CODE,null);//toJTSGeometry(); |
35 |
double distance = geometry.distance(geometryFactory.createPoint(
|
36 |
new Coordinate(queryPoint.getX(), queryPoint.getY())));
|
37 |
|
38 |
if (distance < tolerance) {
|
39 |
geometries.add(geom); |
40 |
} |
41 |
}catch (Exception e) { |
42 |
} |
43 |
} |
44 |
|
45 |
public Point2D getSnapPoint() { |
46 |
if (geometries.isEmpty()) {
|
47 |
return null; |
48 |
} |
49 |
|
50 |
org.gvsig.fmap.geom.Geometry[] geoms = (org.gvsig.fmap.geom.Geometry[]) geometries.toArray(new Geometry[0]); |
51 |
// ((ISnapperGeometriesVectorial) snapper).setGeometries(geoms);
|
52 |
|
53 |
Point2D result = null; |
54 |
|
55 |
for (int i = 0; i < geoms.length; i++) { |
56 |
result = snapper.getSnapPoint(queryPoint, geoms[i], tolerance, |
57 |
lastPointEntered); |
58 |
|
59 |
if (result != null) { |
60 |
return result;
|
61 |
} |
62 |
} |
63 |
|
64 |
return result;
|
65 |
} |
66 |
} |