Statistics
| Revision:

root / tags / v2_0_0_Build_2047 / applications / appgvSIG / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / snapping / snappers / IntersectionPointSnapper.java @ 38317

History | View | Annotate | Download (3.31 KB)

1 29598 jpiera
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers;
2 23642 vcaballero
3
import java.awt.geom.Point2D;
4 29683 jpiera
import java.util.List;
5 23642 vcaballero
6 36371 jpiera
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
9 23642 vcaballero
import org.gvsig.fmap.geom.Geometry;
10 36371 jpiera
import org.gvsig.fmap.geom.operation.GeometryOperationException;
11
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
12 30212 vcaballero
import org.gvsig.fmap.geom.primitive.Curve;
13 30349 jpiera
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer;
14 23642 vcaballero
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperGeometriesVectorial;
15 30349 jpiera
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper;
16 38226 jldominguez
import org.gvsig.i18n.Messages;
17 23642 vcaballero
18
19
/**
20
 * Intersection point snapper.
21
 *
22
 * @author Vicente Caballero Navarro
23
 */
24
public class IntersectionPointSnapper extends AbstractSnapper
25
    implements ISnapperGeometriesVectorial {
26 36371 jpiera
    private static final Logger LOG = LoggerFactory.getLogger(IntersectionPointSnapper.class);
27
28
        private static int maxPointsGeom = 1000;
29 35614 jpiera
        private List<Geometry> geometries;
30 23642 vcaballero
31
    public Point2D getSnapPoint(Point2D point, Geometry geom,
32
        double tolerance, Point2D lastPointEntered) {
33 30212 vcaballero
            if (!(geom instanceof Curve)){
34
                    return null;
35 23642 vcaballero
            }
36
            Point2D result = null;
37
38
        if (geometries == null) {
39
            return null;
40
        }
41
42 35614 jpiera
        for (int i = 0; i < geometries.size(); i++) {
43
                Point2D r = intersects(geom, geometries.get(i), point, tolerance);
44 23642 vcaballero
45
            if (r != null) {
46
                result = r;
47
            }
48
        }
49
50
        return result;
51
    }
52
53 36371 jpiera
    private Point2D intersects(Geometry geometry1, Geometry geometry2, Point2D point,
54 30212 vcaballero
            double tolerance) {
55 36371 jpiera
56 35619 jpiera
            //If there is a topology error don't intersects
57 36371 jpiera
            if ((geometry1 == null) || (geometry2 == null)){
58 35619 jpiera
                return null;
59
            }
60 36371 jpiera
61
            //The getNumCoords is not a getNumPoints but it is a good approximation...
62
            if (geometry1.getGeneralPath().getNumCoords() > maxPointsGeom || geometry2.getGeneralPath().getNumCoords() > maxPointsGeom){
63 30313 vcaballero
                    return null;
64
            }
65 36371 jpiera
66
            Geometry geometry;
67
        try {
68
            geometry = geometry1.intersection(geometry2);
69
            if ((geometry != null) && (geometry.getType() == Geometry.TYPES.POINT)){
70
                return geometry.getHandlers(Geometry.SELECTHANDLER)[0].getPoint();
71
            }
72
        } catch (GeometryOperationNotSupportedException e) {
73
            LOG.error("Is not possible to intersect these geometries", e);
74
        } catch (GeometryOperationException e) {
75
            LOG.error("Is not possible to intersect these geometries", e);
76
        }
77
78 30212 vcaballero
            return null;
79
    }
80 36371 jpiera
81 30349 jpiera
    public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
82
            primitivesDrawer.setColor(getColor());
83 23642 vcaballero
84
        int half = getSizePixels() / 2;
85
        int x1 = (int) (pPixels.getX() - half);
86
        int x2 = (int) (pPixels.getX() + half);
87
        int y1 = (int) (pPixels.getY() - half);
88
        int y2 = (int) (pPixels.getY() + half);
89
90 30349 jpiera
        primitivesDrawer.drawLine(x1, y1, x2, y2);
91
        primitivesDrawer.drawLine(x1, y2, x2, y1);
92 23642 vcaballero
    }
93 36371 jpiera
94 23642 vcaballero
    public String getToolTipText() {
95 38226 jldominguez
        return Messages.getText("Intersection_point");
96 23642 vcaballero
    }
97 29683 jpiera
98
        public void setGeometries(List geoms) {
99 35614 jpiera
            this.geometries = geoms;
100 29683 jpiera
        }
101 23642 vcaballero
}