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 @ 44043

History | View | Annotate | Download (3.78 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 43034 jjdelcerro
 * 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 40559 jjdelcerro
 *
11 43034 jjdelcerro
 * 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 40559 jjdelcerro
 *
16 43034 jjdelcerro
 * 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 40559 jjdelcerro
 *
20 43034 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40559 jjdelcerro
 */
23 40435 jjdelcerro
package org.gvsig.fmap.mapcontext.layers.vectorial;
24
25
import org.cresques.cts.ICoordTrans;
26
import org.cresques.cts.IProjection;
27 44043 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
28 40435 jjdelcerro
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 43034 jjdelcerro
36
37 40435 jjdelcerro
public class ContainsGeometryEvaluator extends AbstractEvaluator {
38
39 43034 jjdelcerro
    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 43739 jjdelcerro
    private final FeatureAttributeDescriptor fad;
46 44043 jjdelcerro
47 43034 jjdelcerro
    ContainsGeometryEvaluator(
48
            Geometry geometry,
49
            IProjection projection,
50
            FeatureType featureType,
51
            String geomName,
52
            ExpressionBuilder builder
53
    ) {
54 43739 jjdelcerro
        fad = (FeatureAttributeDescriptor) featureType.get(geomName);
55 43034 jjdelcerro
        this.projection = projection;
56
        this.builder = builder;
57
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(
58
                geomName);
59
        this.geometry = geometry;
60
        this.geometryTrans = geometry.cloneGeometry();
61 40435 jjdelcerro
62 43034 jjdelcerro
        IProjection fad_proj = fad.getSRS();
63
        ICoordTrans ct = null;
64
        if (fad_proj != null && !fad_proj.equals(projection)) {
65
            ct = projection.getCT(fad_proj);
66
        }
67 40435 jjdelcerro
68 43034 jjdelcerro
        if (ct != null) {
69
            geometryTrans.reProject(ct);
70
        }
71
        this.geomName = geomName;
72 40435 jjdelcerro
73 43034 jjdelcerro
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
74 40435 jjdelcerro
75 43034 jjdelcerro
    }
76 40435 jjdelcerro
77 43034 jjdelcerro
    @Override
78
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
79
        try {
80
            Geometry geom;
81
            if (isDefault) {
82
                Feature feature = (Feature) data.getContextValue("feature");
83
                geom = feature.getDefaultGeometry();
84 40435 jjdelcerro
85 43034 jjdelcerro
            } else {
86
                geom = (Geometry) data.getDataValue(geomName);
87
            }
88
            if (geom == null) {
89
                return false;
90
            }
91
            return geometryTrans.contains(geom);
92 40435 jjdelcerro
93 43034 jjdelcerro
        } catch (Exception e) {
94
            throw new EvaluatorException(e);
95
        }
96
    }
97 40435 jjdelcerro
98 43034 jjdelcerro
    @Override
99
    public String getName() {
100
        return "contains with geometry";
101
    }
102 40435 jjdelcerro
103 43034 jjdelcerro
    @Override
104
    public String getSQL() {
105
        return this.builder.set(
106
                builder.ST_Contains(
107
                        builder.geometry(geometry, projection),
108 44043 jjdelcerro
                        builder.column(fad.getName())
109 43034 jjdelcerro
                )
110
        ).toString();
111
    }
112 40435 jjdelcerro
113
}