Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / vectorial / TouchesGeometryEvaluator.java @ 43040

History | View | Annotate | Download (4.35 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.mapcontext.layers.vectorial;
24

    
25
import org.cresques.cts.ICoordTrans;
26
import org.cresques.cts.IProjection;
27
import org.gvsig.fmap.dal.ExpressionBuilder;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
30
import org.gvsig.fmap.dal.feature.FeatureType;
31
import org.gvsig.fmap.geom.Geometry;
32
import org.gvsig.tools.evaluator.AbstractEvaluator;
33
import org.gvsig.tools.evaluator.EvaluatorData;
34
import org.gvsig.tools.evaluator.EvaluatorException;
35

    
36
public class TouchesGeometryEvaluator extends AbstractEvaluator {
37

    
38
    private final String geomName;
39
    private final Geometry geometry;
40
    private final Geometry geometryTrans;
41
    private final boolean isDefault;
42
    private final ExpressionBuilder builder;
43
    private final IProjection projection;
44

    
45
    /**
46
     * @param geometry
47
     * @param projection
48
     * @param featureType
49
     * @param geomName
50
     * @deprecated  use @{link org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory}
51
     */
52
    public TouchesGeometryEvaluator(Geometry geometry,
53
            IProjection projection,
54
            FeatureType featureType,
55
            String geomName
56
    ) {
57
        this(
58
                geometry, 
59
                projection, 
60
                featureType, 
61
                geomName, 
62
                SpatialEvaluatorsFactory.getInstance().createBuilder()
63
        );
64
    }   
65
    
66
    TouchesGeometryEvaluator(Geometry geometry,
67
            IProjection projection,
68
            FeatureType featureType,
69
            String geomName,
70
            ExpressionBuilder builder
71
        ) {
72
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
73
                .get(geomName);
74
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(
75
                geomName);
76
        this.geometry = geometry;
77
        this.geometryTrans = geometry.cloneGeometry();
78
        this.projection = projection;
79
        this.builder = builder;
80

    
81
        IProjection fad_proj = fad.getSRS();
82
        ICoordTrans ct = null;
83

    
84
        if (fad_proj != null && !fad_proj.equals(projection)) {
85
            ct = projection.getCT(fad_proj);
86
        }
87

    
88
        if (ct != null) {
89
            geometryTrans.reProject(ct);
90
        }
91
        this.geomName = geomName;
92

    
93
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
94

    
95
    }
96

    
97
    @Override
98
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
99
        try {
100
            Geometry geom;
101
            if (isDefault) {
102
                Feature feature = (Feature) data.getContextValue("feature");
103
                geom = feature.getDefaultGeometry();
104

    
105
            } else {
106
                geom = (Geometry) data.getDataValue(geomName);
107
            }
108
            if (geom == null) {
109
                return Boolean.FALSE;
110
            }
111
            return geometryTrans.touches(geom);
112

    
113
        } catch (Exception e) {
114
            throw new EvaluatorException(e);
115
        }
116
    }
117

    
118
    @Override
119
    public String getName() {
120
        return "touches with geometry";
121
    }
122

    
123
    @Override
124
    public String getSQL() {
125
        return this.builder.set(
126
                builder.ST_Touches(
127
                        builder.geometry(geometry, projection),
128
                        builder.column(geomName)
129
                )
130
        ).toString();
131
    }
132
}