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 / IntersectsEnvelopeEvaluator.java @ 43355

History | View | Annotate | Download (4.45 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.IProjection;
26
import org.gvsig.fmap.dal.ExpressionBuilder;
27

    
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.fmap.geom.primitive.Envelope;
33
import org.gvsig.tools.evaluator.AbstractEvaluator;
34
import org.gvsig.tools.evaluator.EvaluatorData;
35
import org.gvsig.tools.evaluator.EvaluatorException;
36

    
37
public class IntersectsEnvelopeEvaluator extends AbstractEvaluator {
38

    
39
    private final Envelope envelope;
40
    private final String geomName;
41
    private final boolean isDefault;
42
    private final String defaultGeometryAttributeName;
43
    private final IProjection projection;
44
    private final ExpressionBuilder builder;
45
    
46
    /**
47
     * @param envelope
48
     * @param envelopeProjection
49
     * @param featureType
50
     * @param geomName
51
     * @deprecated  use @{link org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory}
52
     */
53
    public IntersectsEnvelopeEvaluator(
54
            Envelope envelope,
55
            IProjection envelopeProjection, 
56
            FeatureType featureType,
57
            String geomName
58
    ) {
59
        this(
60
                envelope, 
61
                envelopeProjection, 
62
                featureType, 
63
                geomName, 
64
                SpatialEvaluatorsFactory.getInstance().createBuilder()
65
        );
66
    }   
67
    
68
    IntersectsEnvelopeEvaluator(
69
            Envelope envelope,
70
            IProjection envelopeProjection, 
71
            FeatureType featureType,
72
            String geomName,
73
            ExpressionBuilder builder
74
        ) {
75
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType.get(geomName);
76
        defaultGeometryAttributeName = featureType.getDefaultGeometryAttributeName();
77
        this.isDefault = defaultGeometryAttributeName.equals(geomName);
78
        this.envelope = envelope;
79
        this.projection = envelopeProjection;
80
        this.builder = builder;
81
        this.geomName = geomName;
82
        this.getFieldsInfo().addMatchFieldValue(geomName, envelope);
83
    }
84

    
85
    @Override
86
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
87
        Envelope featureEnvelope;
88
        Geometry geom ;
89
        if (isDefault) {
90
            geom = (Geometry) data.getDataValue(defaultGeometryAttributeName);
91
            featureEnvelope = ((Feature) data.getContextValue("feature")).getDefaultEnvelope();
92
        } else {
93
            geom = (Geometry) data.getDataValue(geomName);
94
            if (geom == null) {
95
                return false;
96
            }
97
            featureEnvelope = geom.getEnvelope();
98
        }
99
        if (envelope.intersects(featureEnvelope)) {
100
            return envelope.intersects(geom);
101
        }
102
        return false;
103
    }
104

    
105
    @Override
106
    public String getName() {
107
        return "intersects envelope";
108
    }
109

    
110
    @Override
111
    public String getSQL() {
112
        // Los gestores de BBDD no usan indices si se envuelve la columna 
113
        // geometria con la funcion ST_envelope.
114
        
115
        return builder.set(
116
                builder.ST_Intersects(
117
                        builder.geometry(envelope.getGeometry(), this.projection), 
118
                        // builder.ST_Envelope(builder.column(geomName))
119
                        builder.column(geomName)
120
                )
121
        ).toString();
122
    }
123
}