Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / feature / impl / MyTransform.java @ 40559

History | View | Annotate | Download (4.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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.
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.
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.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.impl;
25

    
26
import java.util.Arrays;
27

    
28
import org.gvsig.fmap.dal.DataTypes;
29
import org.gvsig.fmap.dal.exception.DataException;
30
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
31
import org.gvsig.fmap.dal.feature.EditableFeature;
32
import org.gvsig.fmap.dal.feature.EditableFeatureType;
33
import org.gvsig.fmap.dal.feature.Feature;
34
import org.gvsig.fmap.dal.feature.FeatureStore;
35
import org.gvsig.fmap.dal.feature.FeatureType;
36
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
37
import org.gvsig.fmap.geom.GeometryLocator;
38
import org.gvsig.fmap.geom.GeometryManager;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.Geometry.TYPES;
41
import org.gvsig.fmap.geom.primitive.Point;
42
import org.gvsig.tools.persistence.PersistentState;
43
import org.gvsig.tools.persistence.exception.PersistenceException;
44

    
45
/**
46
 *
47
 * This transform adds a new attribute of type Geometry to the original store's
48
 * default FeatureType. When applying the transform to a single feature this new
49
 * attribute is assigned the value of a point whose coordinates proceed from two
50
 * numeric attributes from the store, called xname, yname.
51
 *
52
 */
53
class MyTransform extends AbstractFeatureStoreTransform {
54

    
55
        private FeatureType originalType;
56
        private String geomName;
57
        private String xname;
58
        private String yname;
59

    
60
        /**
61
         * Empty default constructor
62
         */
63
        public MyTransform() {
64
        }
65

    
66
        /**
67
         * Initializes the transform by assigning the source store and the names of
68
         * the necessary attributes.
69
         *
70
         * @param store
71
         *            source store.
72
         *
73
         * @param geomName
74
         *            name of the geometry attribute in the default feature type
75
         *            from the source store.
76
         *
77
         * @param xname
78
         *            name of the attribute containing the X coordinates
79
         *
80
         * @param yname
81
         *            name of the attribute containing the Y coordinates
82
         *
83
         * @throws DataException
84
         */
85
        public void initialize(FeatureStore store, String geomName, String xname, String yname) throws DataException {
86

    
87
                // Initialize some data
88
                this.setFeatureStore(store);
89
                this.geomName = geomName;
90
                this.xname = xname;
91
                this.yname = yname;
92

    
93
                this.originalType = store.getDefaultFeatureType();
94
                // obtain the feature type, add the new attribute and keep a reference to the resulting feature type
95
                EditableFeatureType type = store.getDefaultFeatureType().getEditable();
96
                type.add(geomName, DataTypes.GEOMETRY);
97
                FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
98
                setFeatureTypes(Arrays.asList(types), types[0]);
99
        }
100

    
101
        /**
102
         * Applies this transform to a target editable feature, using data from the
103
         * source feature.
104
         */
105
        public void applyTransform(Feature source, EditableFeature target)
106
                        throws DataException {
107

    
108
                // copy source feature data over target feature
109
                target.copyFrom(source);
110

    
111
                // calculate and assign new attribute's value
112
                GeometryManager geomManager = GeometryLocator.getGeometryManager();
113
                Point point;
114
                try {
115
                        point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
116
                        point.setX(source.getDouble(xname));
117
                        point.setY(source.getDouble(yname));
118
                        target.setGeometry(this.geomName, point);
119
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
120
                throw new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e);
121
                }
122
        }
123

    
124
        public void saveToState(PersistentState state) throws PersistenceException {
125
                state.set("geomName", this.geomName);
126
                state.set("xname", this.xname);
127
                state.set("yname", this.yname);
128
        }
129

    
130
        public void loadFromState(PersistentState state) throws PersistenceException {
131
        }
132

    
133
        /*
134
         * (non-Javadoc)
135
         *
136
         * @see
137
         * org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom
138
         * (org.gvsig.fmap.dal.feature.FeatureType)
139
         */
140
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
141
                return originalType;
142
        }
143

    
144
        public boolean isTransformsOriginalValues() {
145
                return false;
146
        }
147

    
148
}