Statistics
| Revision:

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

History | View | Annotate | Download (3.31 KB)

1
package org.gvsig.app.project.documents.view.toolListeners.snapping.snappers;
2

    
3
import java.awt.geom.Point2D;
4
import java.util.List;
5

    
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

    
9
import org.gvsig.fmap.geom.Geometry;
10
import org.gvsig.fmap.geom.operation.GeometryOperationException;
11
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
12
import org.gvsig.fmap.geom.primitive.Curve;
13
import org.gvsig.fmap.mapcontrol.PrimitivesDrawer;
14
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapperGeometriesVectorial;
15
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.impl.AbstractSnapper;
16
import org.gvsig.i18n.Messages;
17

    
18

    
19
/**
20
 * Intersection point snapper.
21
 *
22
 * @author Vicente Caballero Navarro
23
 */
24
public class IntersectionPointSnapper extends AbstractSnapper
25
    implements ISnapperGeometriesVectorial {
26
    private static final Logger LOG = LoggerFactory.getLogger(IntersectionPointSnapper.class);
27
    
28
        private static int maxPointsGeom = 1000;
29
        private List<Geometry> geometries;
30

    
31
    public Point2D getSnapPoint(Point2D point, Geometry geom,
32
        double tolerance, Point2D lastPointEntered) {
33
            if (!(geom instanceof Curve)){
34
                    return null;
35
            }
36
            Point2D result = null;
37

    
38
        if (geometries == null) {
39
            return null;
40
        }
41

    
42
        for (int i = 0; i < geometries.size(); i++) {
43
                Point2D r = intersects(geom, geometries.get(i), point, tolerance);
44

    
45
            if (r != null) {
46
                result = r;
47
            }
48
        }
49

    
50
        return result;
51
    }
52

    
53
    private Point2D intersects(Geometry geometry1, Geometry geometry2, Point2D point,
54
            double tolerance) {
55
            
56
            //If there is a topology error don't intersects
57
            if ((geometry1 == null) || (geometry2 == null)){
58
                return null;
59
            }
60
            
61
            //The getNumCoords is not a getNumPoints but it is a good approximation...            
62
            if (geometry1.getGeneralPath().getNumCoords() > maxPointsGeom || geometry2.getGeneralPath().getNumCoords() > maxPointsGeom){
63
                    return null;
64
            }
65
            
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
            return null;
79
    }
80

    
81
    public void draw(PrimitivesDrawer primitivesDrawer, Point2D pPixels) {
82
            primitivesDrawer.setColor(getColor());
83

    
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
        primitivesDrawer.drawLine(x1, y1, x2, y2);
91
        primitivesDrawer.drawLine(x1, y2, x2, y1);
92
    }
93

    
94
    public String getToolTipText() {
95
        return Messages.getText("Intersection_point");
96
    }
97

    
98
        public void setGeometries(List geoms) {
99
            this.geometries = geoms;                
100
        }
101
}