root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / snapping / GeometriesSnappingVisitor.java @ 8943
History | View | Annotate | Download (1.82 KB)
1 |
package com.iver.cit.gvsig.project.documents.view.snapping; |
---|---|
2 |
|
3 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
4 |
|
5 |
import com.vividsolutions.jts.geom.Coordinate; |
6 |
import com.vividsolutions.jts.geom.Geometry; |
7 |
import com.vividsolutions.jts.geom.GeometryFactory; |
8 |
import com.vividsolutions.jts.index.ItemVisitor; |
9 |
|
10 |
import java.awt.geom.Point2D; |
11 |
|
12 |
import java.util.ArrayList; |
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 |
IGeometry geom = (IGeometry) item; |
33 |
Geometry geometry = geom.toJTSGeometry(); |
34 |
double distance = geometry.distance(geometryFactory.createPoint(
|
35 |
new Coordinate(queryPoint.getX(), queryPoint.getY())));
|
36 |
|
37 |
if (distance < tolerance) {
|
38 |
geometries.add(geom); |
39 |
} |
40 |
}catch (Exception e) { |
41 |
} |
42 |
} |
43 |
|
44 |
public Point2D getSnapPoint() { |
45 |
if (geometries.isEmpty()) {
|
46 |
return null; |
47 |
} |
48 |
|
49 |
IGeometry[] geoms = (IGeometry[]) geometries.toArray(new IGeometry[0]); |
50 |
((ISnapperGeometriesVectorial) snapper).setGeometries(geoms); |
51 |
|
52 |
Point2D result = null; |
53 |
|
54 |
for (int i = 0; i < geoms.length; i++) { |
55 |
result = snapper.getSnapPoint(queryPoint, geoms[i], tolerance, |
56 |
lastPointEntered); |
57 |
|
58 |
if (result != null) { |
59 |
return result;
|
60 |
} |
61 |
} |
62 |
|
63 |
return result;
|
64 |
} |
65 |
} |