Revision 43034 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/WithinGeometryEvaluator.java

View differences:

WithinGeometryEvaluator.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 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.
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 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.
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 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.
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.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.fmap.mapcontext.layers.vectorial;
25 24

  
26 25
import org.cresques.cts.ICoordTrans;
27 26
import org.cresques.cts.IProjection;
27
import org.gvsig.fmap.dal.ExpressionBuilder;
28 28
import org.gvsig.fmap.dal.feature.Feature;
29 29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
30 30
import org.gvsig.fmap.dal.feature.FeatureType;
......
32 32
import org.gvsig.tools.evaluator.AbstractEvaluator;
33 33
import org.gvsig.tools.evaluator.EvaluatorData;
34 34
import org.gvsig.tools.evaluator.EvaluatorException;
35
/**
36
 *
37
 * @author Vicente Caballero Navarro
38
 *
39
 */
35

  
40 36
public class WithinGeometryEvaluator extends AbstractEvaluator {
41 37

  
42
	private String geomName;
43
	private Geometry geometry;
44
	private Geometry geometryTrans;
45
	private String srs;
46
	private boolean isDefault;
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;
47 44

  
48
	WithinGeometryEvaluator(Geometry geometry,
49
			IProjection envelopeProjection, FeatureType featureType,
50
			String geomName) {
51
		FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
52
				.get(geomName);
53
		this.isDefault = featureType.getDefaultGeometryAttributeName().equals(
54
				geomName);
55
		this.geometry = geometry;
56
		this.geometryTrans = geometry.cloneGeometry();
57
		this.srs = envelopeProjection.getAbrev();
58
		
59
		IProjection fad_proj = fad.getSRS();
60
		ICoordTrans ct = null;
61
		
62
		if (fad_proj != null && !fad_proj.equals(envelopeProjection)) {
63
		    ct = envelopeProjection.getCT(fad_proj);
64
		}
65
		
66
		if (ct != null) {
67
			geometryTrans.reProject(ct);
68
		}
69
		this.geomName = geomName;
45
    WithinGeometryEvaluator(
46
            Geometry geometry,
47
            IProjection projection,
48
            FeatureType featureType,
49
            String geomName,
50
            ExpressionBuilder builder
51
        ) {
52
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
53
                .get(geomName);
54
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(
55
                geomName);
56
        this.geometry = geometry;
57
        this.geometryTrans = geometry.cloneGeometry();
58
        this.projection = projection;
59
        this.builder = builder;
70 60

  
71
		this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
61
        IProjection fad_proj = fad.getSRS();
62
        ICoordTrans ct = null;
72 63

  
73
	}
64
        if (fad_proj != null && !fad_proj.equals(projection)) {
65
            ct = projection.getCT(fad_proj);
66
        }
74 67

  
75
	public Object evaluate(EvaluatorData data) throws EvaluatorException {
76
		try {
77
			Geometry geom = null;
78
			if (isDefault) {
79
				Feature feature = (Feature) data.getContextValue("feature");
80
				geom =feature.getDefaultGeometry();
68
        if (ct != null) {
69
            geometryTrans.reProject(ct);
70
        }
71
        this.geomName = geomName;
81 72

  
82
			} else {
83
				geom = (Geometry) data.getDataValue(geomName);
84
			}
85
                        if ( geom == null ) {
86
                            return Boolean.FALSE;
87
                        }
88
			return new Boolean(geometryTrans.within(geom));
73
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
89 74

  
90
		} catch (Exception e) {
91
			throw new EvaluatorException(e);
92
		}
93
	}
75
    }
94 76

  
95
	public String getName() {
96
		return "within with geometry";
97
	}
77
    @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();
98 84

  
99
	public String getSQL() {
100
		return " ST_within(ST_GeomFromText('" + geometry.toString() + "', " + "'"
101
				+ srs + "'" + "), " + geomName + ") ";
102
	}
85
            } else {
86
                geom = (Geometry) data.getDataValue(geomName);
87
            }
88
            if (geom == null) {
89
                return false;
90
            }
91
            return geometryTrans.within(geom);
103 92

  
93
        } catch (Exception e) {
94
            throw new EvaluatorException(e);
95
        }
96
    }
97

  
98
    @Override
99
    public String getName() {
100
        return "within with geometry";
101
    }
102

  
103
    @Override
104
    public String getSQL() {
105
        return this.builder.set(
106
                builder.ST_Within(
107
                        builder.geometry(geometry, projection),
108
                        builder.column(geomName)
109
                )
110
        ).toString();
111
    }
104 112
}

Also available in: Unified diff