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 / ContainsGeometryEvaluator.java @ 43040

History | View | Annotate | Download (4.38 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

    
37
public class ContainsGeometryEvaluator extends AbstractEvaluator {
38

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

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

    
84
        IProjection fad_proj = fad.getSRS();
85
        ICoordTrans ct = null;
86
        if (fad_proj != null && !fad_proj.equals(projection)) {
87
            ct = projection.getCT(fad_proj);
88
        }
89

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

    
95
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
96

    
97
    }
98

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

    
107
            } else {
108
                geom = (Geometry) data.getDataValue(geomName);
109
            }
110
            if (geom == null) {
111
                return false;
112
            }
113
            return geometryTrans.contains(geom);
114

    
115
        } catch (Exception e) {
116
            throw new EvaluatorException(e);
117
        }
118
    }
119

    
120
    @Override
121
    public String getName() {
122
        return "contains with geometry";
123
    }
124

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

    
135
}