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/EqualsGeometryEvaluator.java

View differences:

EqualsGeometryEvaluator.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;
28
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException;
27
import org.gvsig.fmap.dal.ExpressionBuilder;
29 28
import org.gvsig.fmap.dal.feature.Feature;
30 29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
31 30
import org.gvsig.fmap.dal.feature.FeatureType;
32 31
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35 32
import org.gvsig.tools.evaluator.AbstractEvaluator;
36 33
import org.gvsig.tools.evaluator.EvaluatorData;
37 34
import org.gvsig.tools.evaluator.EvaluatorException;
38
/**
39
 *
40
 * @author Vicente Caballero Navarro
41
 *
42
 */
35

  
43 36
public class EqualsGeometryEvaluator extends AbstractEvaluator {
44 37

  
45
	private String geomName;
46
	private Geometry geometry;
47
	private String geometryWKT = null;
48
	private Geometry geometryTrans;
49
	private String srs;
50
	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;
51 44

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

  
70
		if (ct != null) {
71
			geometryTrans.reProject(ct);
72
		}
73
		this.geomName = geomName;
60
        IProjection fad_proj = fad.getSRS();
61
        ICoordTrans ct = null;
74 62

  
75
		this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
63
        if (fad_proj != null && !fad_proj.equals(projection)) {
64
            ct = projection.getCT(fad_proj);
65
        }
76 66

  
77
	}
67
        if (ct != null) {
68
            geometryTrans.reProject(ct);
69
        }
70
        this.geomName = geomName;
78 71

  
79
	public Object evaluate(EvaluatorData data) throws EvaluatorException {
80
		try {
81
			Geometry geom = null;
82
			if (isDefault) {
83
				Feature feature = (Feature) data.getContextValue("feature");
84
				geom =feature.getDefaultGeometry();
72
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
85 73

  
86
			} else {
87
				geom = (Geometry) data.getDataValue(geomName);
88
			}
89
                        if ( geom == null ) {
90
                            return Boolean.FALSE;
91
                        }
92
			return new Boolean(geometryTrans.equals(geom));
74
    }
93 75

  
94
		} catch (Exception e) {
95
			throw new EvaluatorException(e);
96
		}
97
	}
76
    @Override
77
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
78
        try {
79
            Geometry geom;
80
            if (isDefault) {
81
                Feature feature = (Feature) data.getContextValue("feature");
82
                geom = feature.getDefaultGeometry();
98 83

  
99
	public String getName() {
100
		return "equals with geometry";
101
	}
84
            } else {
85
                geom = (Geometry) data.getDataValue(geomName);
86
            }
87
            if (geom == null) {
88
                return Boolean.FALSE;
89
            }
90
            return geometryTrans.equals(geom);
102 91

  
103
	public String getSQL() {
104
		if (geometryWKT == null) {
105
			try {
106
				geometryWKT = geometry.convertToWKT();
107
			} catch (GeometryOperationNotSupportedException e) {
108
				throw new DataEvaluatorRuntimeException(e);
109
			} catch (GeometryOperationException e) {
110
				throw new DataEvaluatorRuntimeException(e);
111
			}
112
		}
113
		return " ST_equals(ST_GeomFromText('" + geometryWKT + "', " + "'"
114
				+ srs + "'" + "), " + geomName + ") ";
115
	}
92
        } catch (Exception e) {
93
            throw new EvaluatorException(e);
94
        }
95
    }
116 96

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

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

  
117 112
}

Also available in: Unified diff