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

History | View | Annotate | Download (4.35 KB)

1 40559 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 43020 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 43020 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 43020 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 43020 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 43020 jjdelcerro
import org.gvsig.fmap.dal.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
36
public class IntersectsGeometryEvaluator extends AbstractEvaluator {
37
38 43040 jjdelcerro
    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 40435 jjdelcerro
45 43040 jjdelcerro
    /**
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 IntersectsGeometryEvaluator(
53
            Geometry geometry,
54
            IProjection projection,
55
            FeatureType featureType,
56
            String geomName
57
        ) {
58
        this(
59
                geometry,
60
                projection,
61
                featureType,
62
                geomName,
63
                SpatialEvaluatorsFactory.getInstance().createBuilder()
64
        );
65
    }
66
67
    IntersectsGeometryEvaluator(
68
            Geometry geometry,
69
            IProjection projection,
70
            FeatureType featureType,
71 43020 jjdelcerro
            String geomName,
72 43040 jjdelcerro
            ExpressionBuilder builder
73
        ) {
74 43020 jjdelcerro
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
75
                .get(geomName);
76
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(geomName);
77
        this.geometry = geometry;
78 43040 jjdelcerro
        this.projection = projection;
79
        this.builder = builder;
80 43020 jjdelcerro
        this.geometryTrans = geometry.cloneGeometry();
81
        ICoordTrans ct = null;
82 40435 jjdelcerro
83 43020 jjdelcerro
        IProjection ipro = fad.getSRS();
84
        if (ipro != null && !ipro.equals(projection)) {
85
            ct = projection.getCT(ipro);
86
        }
87 40435 jjdelcerro
88 43020 jjdelcerro
        if (ct != null) {
89
            geometryTrans.reProject(ct);
90
        }
91
        this.geomName = geomName;
92
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
93
    }
94 40435 jjdelcerro
95 43020 jjdelcerro
    @Override
96
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
97
        try {
98 43040 jjdelcerro
            Geometry geom;
99 43020 jjdelcerro
            if (isDefault) {
100
                Feature feature = (Feature) data.getContextValue("feature");
101
                geom = feature.getDefaultGeometry();
102 40435 jjdelcerro
103 43020 jjdelcerro
            } else {
104
                geom = (Geometry) data.getDataValue(geomName);
105
            }
106
            if (geom == null) {
107
                return Boolean.FALSE;
108
            }
109
            return geometryTrans.intersects(geom);
110 40435 jjdelcerro
111 43020 jjdelcerro
        } catch (Exception e) {
112
            return Boolean.FALSE;
113
        }
114
    }
115 40435 jjdelcerro
116 43020 jjdelcerro
    @Override
117
    public String getName() {
118
        return "intersects with geometry";
119
    }
120 40435 jjdelcerro
121 43020 jjdelcerro
    @Override
122
    public String getSQL() {
123 43040 jjdelcerro
        return builder.set(
124
                builder.ST_Intersects(
125
                        builder.geometry(geometry, this.projection),
126
                        builder.column(geomName)
127
                )
128
        ).toString();
129 43020 jjdelcerro
    }
130 40435 jjdelcerro
131
}