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

View differences:

DisjointGeometryEvaluator.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
 */
43
public class DisjointGeometryEvaluator extends AbstractEvaluator {
44 35

  
45
	private String geomName;
46
	private Geometry geometry;
47
	private Geometry geometryTrans;
48
	private String srs;
49
	private boolean isDefault;
50
	private String geometryWKT;
51 36

  
52
	DisjointGeometryEvaluator(Geometry geometry,
53
			IProjection projection,
54
			FeatureType featureType,
55
			String geomName) {
56
		FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
57
				.get(geomName);
58
		this.isDefault = featureType.getDefaultGeometryAttributeName().equals(
59
				geomName);
60
		this.geometry = geometry;
61
		this.geometryTrans = geometry.cloneGeometry();
62
		this.srs = projection.getAbrev();
63
		
64
		IProjection fad_proj = fad.getSRS();
65
		ICoordTrans ct = null;
66
		
67
		if (fad_proj != null && !fad_proj.equals(projection)) {
68
		    ct = projection.getCT(fad_proj);
69
		}
37
public class DisjointGeometryEvaluator extends AbstractEvaluator {
70 38

  
71
		if (ct != null) {
72
			geometryTrans.reProject(ct);
73
		}
74
		this.geomName = geomName;
39
    private final String geomName;
40
    private final Geometry geometry;
41
    private final Geometry geometryTrans;
42
    private final boolean isDefault;
43
    private final ExpressionBuilder builder;
44
    private final IProjection projection;
45
    
46
    DisjointGeometryEvaluator(
47
            Geometry geometry,
48
            IProjection projection,
49
            FeatureType featureType,
50
            String geomName,
51
            ExpressionBuilder builder
52
        ) {
53
        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) featureType
54
                .get(geomName);
55
        this.isDefault = featureType.getDefaultGeometryAttributeName().equals(geomName);
56
        this.geometry = geometry;
57
        this.geometryTrans = geometry.cloneGeometry();
58
        this.projection = projection;
59
        this.builder = builder;
60
        
61
        IProjection fad_proj = fad.getSRS();
62
        ICoordTrans ct = null;
75 63

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

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

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

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

  
95
		} catch (Exception e) {
96
			throw new EvaluatorException(e);
97
		}
98
	}
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();
99 84

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

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

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

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

Also available in: Unified diff