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