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

History | View | Annotate | Download (4.19 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontext.layers.vectorial;
25

    
26
import org.cresques.cts.IProjection;
27

    
28
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
29
import org.gvsig.fmap.dal.feature.Feature;
30
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
31
import org.gvsig.fmap.dal.feature.FeatureType;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35
import org.gvsig.fmap.geom.primitive.Envelope;
36
import org.gvsig.tools.evaluator.AbstractEvaluator;
37
import org.gvsig.tools.evaluator.EvaluatorData;
38
import org.gvsig.tools.evaluator.EvaluatorException;
39

    
40
public class IntersectsEnvelopeEvaluator extends AbstractEvaluator {
41

    
42
        private Envelope envelope;
43
        private String geomName;
44
        private String envelopeWKT = null;
45
        private Envelope envelopeTrans;
46
        private boolean isDefault;
47
        private String srs;
48
        String defaultGeometryAttributeName;
49

    
50
        public IntersectsEnvelopeEvaluator(Envelope envelope,
51
                        IProjection envelopeProjection, FeatureType featureType,
52
                        String geomName) {
53
                FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
54
                                .get(geomName);
55

    
56
                defaultGeometryAttributeName = featureType.getDefaultGeometryAttributeName();
57
        this.isDefault = defaultGeometryAttributeName.equals(
58
                                geomName);
59
                this.envelope = envelope;
60
                // this.srs = envelopeProjection.getAbrev();
61
                // this.projection=CRSFactory.getCRS(fad.getSRS());
62
                // this.envelopeProjection=envelopeProjection;
63
//                ICoordTrans ct = null;
64
//                if (!this.srs.equals(fad.getSRS())) { // FIXME comparaci?n
65
//                        ct = envelopeProjection.getCT(fad.getSRS());
66
//                }
67
//                if (ct != null) {
68
//                        this.envelopeTrans = envelope.convert(ct);
69
//                } else {
70
                        this.envelopeTrans = envelope;
71
//                }
72
                this.geomName = geomName;
73

    
74
                this.srs = envelopeProjection.getAbrev();
75

    
76
                this.getFieldsInfo().addMatchFieldValue(geomName, envelopeTrans);
77

    
78
        }
79

    
80
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
81
        Envelope featureEnvelope;
82
        Geometry geom = null;
83
        if (isDefault) {
84
            geom = (Geometry) data.getDataValue(defaultGeometryAttributeName);
85
            featureEnvelope = ((Feature) data.getContextValue("feature")).getDefaultEnvelope();
86
        } else {
87
            geom = (Geometry) data.getDataValue(geomName);
88
            if (geom == null) {
89
                return Boolean.FALSE;
90
            }
91
            featureEnvelope = geom.getEnvelope();
92
        }
93
        if (envelopeTrans.intersects(featureEnvelope)) {
94
            return new Boolean(envelopeTrans.intersects(geom));
95
        }
96
        return Boolean.FALSE;
97
    }
98

    
99
        public String getName() {
100
                return "intersects envelope";
101
        }
102

    
103
        public String getSQL() {
104

    
105
                if (envelopeWKT == null) {
106
                        try {
107
                                envelopeWKT = (String) envelope.getGeometry().convertToWKT();
108
                        } catch (GeometryOperationNotSupportedException e) {
109
                                throw new DataEvaluatorRuntimeException(e);
110
                        } catch (GeometryOperationException e) {
111
                                throw new DataEvaluatorRuntimeException(e);
112
                        }
113
                }
114

    
115
                return " ST_intersects(ST_GeomFromText('" + envelopeWKT + "', '" + srs
116
                                + "'), ST_envelope(" + geomName + ")) ";
117
        }
118

    
119
}