Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / impl / MyTransform.java @ 28076

History | View | Annotate | Download (3.58 KB)

1
package org.gvsig.fmap.dal.feature.impl;
2

    
3
import java.util.Arrays;
4

    
5
import org.gvsig.fmap.dal.DataTypes;
6
import org.gvsig.fmap.dal.exception.DataException;
7
import org.gvsig.fmap.dal.feature.*;
8
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
9
import org.gvsig.fmap.geom.GeometryLocator;
10
import org.gvsig.fmap.geom.GeometryManager;
11
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
12
import org.gvsig.fmap.geom.Geometry.TYPES;
13
import org.gvsig.fmap.geom.primitive.Point;
14
import org.gvsig.tools.persistence.PersistenceException;
15
import org.gvsig.tools.persistence.PersistentState;
16

    
17
/**
18
 *
19
 * This transform adds a new attribute of type Geometry to the original store's
20
 * default FeatureType. When applying the transform to a single feature this new
21
 * attribute is assigned the value of a point whose coordinates proceed from two
22
 * numeric attributes from the store, called xname, yname.
23
 *
24
 */
25
class MyTransform extends AbstractFeatureStoreTransform {
26

    
27
        private FeatureType originalType;
28
        private String geomName;
29
        private String xname;
30
        private String yname;
31

    
32
        /**
33
         * Empty default constructor
34
         */
35
        public MyTransform() {
36
        }
37

    
38
        /**
39
         * Initializes the transform by assigning the source store and the names of
40
         * the necessary attributes.
41
         *
42
         * @param store
43
         *            source store.
44
         *
45
         * @param geomName
46
         *            name of the geometry attribute in the default feature type
47
         *            from the source store.
48
         *
49
         * @param xname
50
         *            name of the attribute containing the X coordinates
51
         *
52
         * @param yname
53
         *            name of the attribute containing the Y coordinates
54
         *
55
         * @throws DataException
56
         */
57
        public void initialize(FeatureStore store, String geomName, String xname, String yname) throws DataException {
58

    
59
                // Initialize some data
60
                this.setFeatureStore(store);
61
                this.geomName = geomName;
62
                this.xname = xname;
63
                this.yname = yname;
64

    
65
                this.originalType = store.getDefaultFeatureType();
66
                // obtain the feature type, add the new attribute and keep a reference to the resulting feature type
67
                EditableFeatureType type = store.getDefaultFeatureType().getEditable();
68
                type.add(geomName, DataTypes.GEOMETRY);
69
                FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
70
                setFeatureTypes(Arrays.asList(types), types[0]);
71
        }
72

    
73
        /**
74
         * Applies this transform to a target editable feature, using data from the
75
         * source feature.
76
         */
77
        public void applyTransform(Feature source, EditableFeature target)
78
                        throws DataException {
79

    
80
                // copy source feature data over target feature
81
                target.copyFrom(source);
82

    
83
                // calculate and assign new attribute's value
84
                GeometryManager geomManager = GeometryLocator.getGeometryManager();
85
                Point point;
86
                try {
87
                        point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
88
                        point.setX(source.getDouble(xname));
89
                        point.setY(source.getDouble(yname));
90
                        target.setGeometry(this.geomName, point);
91
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
92
                throw new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e);
93
                }
94
        }
95

    
96
        public void saveToState(PersistentState state) throws PersistenceException {
97
                state.set("geomName", this.geomName);
98
                state.set("xname", this.xname);
99
                state.set("yname", this.yname);
100
        }
101

    
102
        public void setState(PersistentState state) throws PersistenceException {
103
        }
104

    
105
        /*
106
         * (non-Javadoc)
107
         *
108
         * @see
109
         * org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom
110
         * (org.gvsig.fmap.dal.feature.FeatureType)
111
         */
112
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
113
                return originalType;
114
        }
115

    
116
        public boolean isTransformsOriginalValues() {
117
                return false;
118
        }
119

    
120
}