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

History | View | Annotate | Download (4.49 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 OutGeometryEvaluator 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 envelopeProjection
48
     * @param featureType
49
     * @param geomName
50
     * @deprecated  use @{link org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory}
51
     */
52
    public OutGeometryEvaluator(
53
            Geometry geometry,
54
            IProjection envelopeProjection, 
55
            FeatureType featureType,
56
            String geomName
57
    ) {
58
        this(
59
                geometry, 
60
                envelopeProjection, 
61
                featureType, 
62
                geomName, 
63
                SpatialEvaluatorsFactory.getInstance().createBuilder()
64
        );
65
    }   
66
    
67
    OutGeometryEvaluator(
68
            Geometry geometry,
69
            IProjection envelopeProjection, 
70
            FeatureType featureType,
71
            String geomName,
72
            ExpressionBuilder builder
73
        ) {
74
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
75
                .get(geomName);
76
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(geomName);
77
        this.geometry = geometry;
78
        this.geometryTrans = geometry.cloneGeometry();
79
        this.projection = envelopeProjection;
80
        this.builder = builder;
81

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

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

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

    
94
        this.getFieldsInfo().addFieldValue(geomName);
95

    
96
    }
97

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

    
106
            } else {
107
                geom = (Geometry) data.getDataValue(geomName);
108
            }
109
            if (geom == null) {
110
                return Boolean.FALSE;
111
            }
112
            return !geometryTrans.contains(geom) && !geometryTrans.intersects(geom);
113

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

    
119
    @Override
120
    public String getName() {
121
        return "out of geometry";
122
    }
123

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

    
136
}