Revision 43020 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/IntersectsGeometryEvaluator.java

View differences:

IntersectsGeometryEvaluator.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;
31
import org.gvsig.fmap.dal.SQLBuilder;
32 32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35 33
import org.gvsig.tools.evaluator.AbstractEvaluator;
36 34
import org.gvsig.tools.evaluator.EvaluatorData;
37 35
import org.gvsig.tools.evaluator.EvaluatorException;
38 36

  
39 37
public class IntersectsGeometryEvaluator extends AbstractEvaluator {
40 38

  
41
	private String geomName;
42
	private Geometry geometry;
43
	private String geometryWKT = null;
44
	private Geometry geometryTrans;
45
	private String srs;
46
	private boolean isDefault;
39
    private String geomName;
40
    private Geometry geometry;
41
    private Geometry geometryTrans;
42
    private boolean isDefault;
43
    private ExpressionBuilder condition;
47 44

  
48
	public IntersectsGeometryEvaluator(Geometry geometry,
49
			IProjection projection, 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 = projection.getAbrev();
58
		ICoordTrans ct = null;
59
		
60
		IProjection ipro = fad.getSRS();
61
		if (ipro != null && !ipro.equals(projection)) {
62
		    ct = projection.getCT(ipro);
63
		}
64
		
65
		if (ct != null) {
66
			geometryTrans.reProject(ct);
67
		}
68
		this.geomName = geomName;
45
    IntersectsGeometryEvaluator(Geometry geometry,
46
            IProjection projection, FeatureType featureType,
47
            String geomName,
48
            ExpressionBuilder builder) {
49
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
50
                .get(geomName);
51
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(geomName);
52
        this.geometry = geometry;
53
        this.geometryTrans = geometry.cloneGeometry();
54
        ICoordTrans ct = null;
69 55

  
70
		this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
56
        IProjection ipro = fad.getSRS();
57
        if (ipro != null && !ipro.equals(projection)) {
58
            ct = projection.getCT(ipro);
59
        }
71 60

  
72
	}
61
        if (ct != null) {
62
            geometryTrans.reProject(ct);
63
        }
64
        this.geomName = geomName;
65
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
66
        this.condition = builder.set(
67
                builder.ST_Intersects(
68
                        builder.geometry(geometry, projection), 
69
                        builder.column(geomName)
70
                )
71
        );
72
    }
73 73

  
74
	public Object evaluate(EvaluatorData data) throws EvaluatorException {
75
		try {
76
			Geometry geom = null;
77
			if (isDefault) {
78
				Feature feature = (Feature) data.getContextValue("feature");
79
				geom =feature.getDefaultGeometry();
74
    @Override
75
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
76
        try {
77
            Geometry geom ;
78
            if (isDefault) {
79
                Feature feature = (Feature) data.getContextValue("feature");
80
                geom = feature.getDefaultGeometry();
80 81

  
81
			} else {
82
				geom = (Geometry) data.getDataValue(geomName);
83
			}
84
                        if( geom==null ) {
85
                            return Boolean.FALSE;
86
                        }
87
			return new Boolean(geometryTrans.intersects(geom));
82
            } else {
83
                geom = (Geometry) data.getDataValue(geomName);
84
            }
85
            if (geom == null) {
86
                return Boolean.FALSE;
87
            }
88
            return geometryTrans.intersects(geom);
88 89

  
89
		} catch (Exception e) {
90
                    return Boolean.FALSE;
91
                    // throw new EvaluatorException(e);
92
		}
93
	}
90
        } catch (Exception e) {
91
            return Boolean.FALSE;
92
        }
93
    }
94 94

  
95
	public String getName() {
96
		return "intersects with geometry";
97
	}
95
    @Override
96
    public String getName() {
97
        return "intersects with geometry";
98
    }
98 99

  
99
	public String getSQL() {
100
		if (geometryWKT == null) {
101
			try {
102
				geometryWKT = geometry.convertToWKT();
103
			} catch (GeometryOperationNotSupportedException e) {
104
				throw new DataEvaluatorRuntimeException(e);
105
			} catch (GeometryOperationException e) {
106
				throw new DataEvaluatorRuntimeException(e);
107
			}
108
		}
109
		return " ST_intersects(ST_GeomFromText('" + geometryWKT + "', " + "'"
110
				+ srs + "'" + "), " + geomName + ") ";
111
	}
100
    @Override
101
    public String getSQL() {
102
        return this.condition.toString();
103
    }
112 104

  
113 105
}

Also available in: Unified diff