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

View differences:

OverlapsGeometryEvaluator.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

  
35 36
/**
36 37
 *
37 38
 * @author Vicente Caballero Navarro
......
39 40
 */
40 41
public class OverlapsGeometryEvaluator extends AbstractEvaluator {
41 42

  
42
	private String geomName;
43
	private Geometry geometry;
44
	private Geometry geometryTrans;
45
	private String srs;
46
	private boolean isDefault;
43
    private final String geomName;
44
    private final Geometry geometry;
45
    private final Geometry geometryTrans;
46
    private final boolean isDefault;
47
    private final ExpressionBuilder builder;
48
    private final IProjection projection;
47 49

  
48
	OverlapsGeometryEvaluator(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;
50
    OverlapsGeometryEvaluator(
51
            Geometry geometry,
52
            IProjection projection,
53
            FeatureType featureType,
54
            String geomName,
55
            ExpressionBuilder builder
56
        ) {
57
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
58
                .get(geomName);
59
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(geomName);
60
        this.geometry = geometry;
61
        this.geometryTrans = geometry.cloneGeometry();
62
        this.projection = projection;
63
        this.builder = builder;
64
        
65
        IProjection fad_proj = fad.getSRS();
66
        ICoordTrans ct = null;
70 67

  
71
		this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
68
        if (fad_proj != null && !fad_proj.equals(projection)) {
69
            ct = projection.getCT(fad_proj);
70
        }
72 71

  
73
	}
72
        if (ct != null) {
73
            geometryTrans.reProject(ct);
74
        }
75
        this.geomName = geomName;
74 76

  
75
	public Object evaluate(EvaluatorData data) throws EvaluatorException {
77
        this.getFieldsInfo().addMatchFieldValue(geomName, geometryTrans);
76 78

  
77
		try {
78
			Geometry geom = null;
79
			if (isDefault) {
80
				Feature feature = (Feature) data.getContextValue("feature");
81
				geom = feature.getDefaultGeometry();
79
    }
82 80

  
83
			} else {
84
				geom = (Geometry) data.getDataValue(geomName);
85
			}
86
                        if ( geom == null ) {
87
                            return Boolean.FALSE;
88
                        }
89
			return new Boolean(geometryTrans.overlaps(geom));
81
    @Override
82
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
90 83

  
91
		} catch (Exception e) {
92
			throw new EvaluatorException(e);
93
		}
94
	}
84
        try {
85
            Geometry geom;
86
            if (isDefault) {
87
                Feature feature = (Feature) data.getContextValue("feature");
88
                geom = feature.getDefaultGeometry();
95 89

  
96
	public String getName() {
97
		return "overlaps with geometry";
98
	}
90
            } else {
91
                geom = (Geometry) data.getDataValue(geomName);
92
            }
93
            if (geom == null) {
94
                return Boolean.FALSE;
95
            }
96
            return geometryTrans.overlaps(geom);
99 97

  
100
	public String getSQL() {
101
		return " ST_overlaps(ST_GeomFromText('" + geometry.toString() + "', " + "'"
102
				+ srs + "'" + "), " + geomName + ") ";
103
	}
98
        } catch (Exception e) {
99
            throw new EvaluatorException(e);
100
        }
101
    }
104 102

  
103
    @Override
104
    public String getName() {
105
        return "overlaps with geometry";
106
    }
107

  
108
    @Override
109
    public String getSQL() {
110
        return this.builder.set(
111
                builder.ST_Overlaps(
112
                        builder.geometry(geometry, projection),
113
                        builder.column(geomName)
114
                )
115
        ).toString();
116
    }
117
    
118
    
105 119
}

Also available in: Unified diff