Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / MyTransform.java @ 42811

History | View | Annotate | Download (5.09 KB)

1 40559 jjdelcerro
/**
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 40435 jjdelcerro
25
package org.gvsig.fmap.dal.store.dbf;
26
27
import java.util.ArrayList;
28
import java.util.List;
29
30
import org.gvsig.fmap.dal.DataTypes;
31
import org.gvsig.fmap.dal.exception.DataException;
32
import org.gvsig.fmap.dal.exception.ReadException;
33
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
34
import org.gvsig.fmap.dal.feature.EditableFeature;
35
import org.gvsig.fmap.dal.feature.EditableFeatureType;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.dal.feature.FeatureType;
39
import org.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43
import org.gvsig.tools.persistence.PersistentState;
44
import org.gvsig.tools.persistence.exception.PersistenceException;
45
46
public class MyTransform extends AbstractFeatureStoreTransform {
47
48
    private String geomName;
49
        private String xname;
50
        private String yname;
51
52
    private GeometryManager geomManager;
53
54
        /**
55
         * A default constructor with out parameters
56
         */
57
        public MyTransform() {
58
                this.geomManager = GeometryLocator.getGeometryManager();
59
60
        }
61
62
        /**
63
         * Initializes the transform by assigning the source store and the names of
64
         * the necessary attributes.
65
         *
66
         * @param store
67
         *            source store.
68
         *
69
         * @param geomName
70
         *            name of the geometry attribute in the default feature type
71
         *            from the source store.
72
         *
73
         * @param xname
74
         *            name of the attribute containing the X coordinates
75
         *
76
         * @param yname
77
         *            name of the attribute containing the Y coordinates
78
         *
79
         * @throws DataException
80
         */
81
        public MyTransform initialize(FeatureStore store, String geomName,
82
                        String xname, String yname) throws DataException {
83
84
                // Initialize some data
85
                setFeatureStore(store);
86
                this.geomName = geomName;
87
                this.xname = xname;
88
                this.yname = yname;
89
90
                // obtain the feature type, add the new attribute
91
                // and keep a reference to the
92
                // resulting feature type
93
                EditableFeatureType type = getFeatureStore().getDefaultFeatureType()
94
                                .getEditable();
95
                type.add(geomName, DataTypes.GEOMETRY);
96
                List list = new ArrayList(1);
97
                list.add(type.getNotEditableCopy());
98
                setFeatureTypes(list, (FeatureType) list.get(0));
99
                return this;
100
        }
101
102
        /**
103
         * Applies this transform to a target editable feature, using data from the
104
         * source feature.
105
         */
106
        public void applyTransform(Feature source, EditableFeature target)
107
                        throws DataException {
108
109
                // copy source feature data over target feature
110
                target.copyFrom(source);
111
112
                // calculate and assign new attribute's value
113
                Geometry geom;
114
                try {
115
                        geom = geomManager.createPoint(source.getDouble(xname), source
116
                                        .getDouble(yname), Geometry.SUBTYPES.GEOM2D);
117
                } catch (CreateGeometryException e) {
118 42811 jjdelcerro
                        throw new XYTransformException(e);
119 40435 jjdelcerro
                }
120
                target.setGeometry(this.geomName, geom);
121
        }
122
123 42811 jjdelcerro
        public class XYTransformException extends DataException {
124
            private final static String MESSAGE_FORMAT = "Problems apply XYTransform, can't create point.";
125
            private final static String MESSAGE_KEY = "_XYTransformException";
126
            private static final long serialVersionUID = -3556249688758572523L;
127 40435 jjdelcerro
128 42811 jjdelcerro
            public XYTransformException(Throwable cause) {
129
                    super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
130
            }
131
        }
132
133 40435 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
134
                state.set("xname", xname);
135
                state.set("yname", yname);
136
                state.set("geomName", geomName);
137
        }
138
139
    public void loadFromState(PersistentState state)
140
                        throws PersistenceException {
141
                this.xname = state.getString("xname");
142
                this.yname = state.getString("yname");
143
                this.geomName = state.getString("geomName");
144
        }
145
146
    /**
147
         * Returns the FeatureType to use to get data from original store
148
         */
149
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
150
                EditableFeatureType ed = targetFeatureType.getEditable();
151
                ed.remove(this.geomName);
152
                return ed.getNotEditableCopy();
153
        }
154
155
        /**
156
         * Informs that original values of store don't will be modified
157
         */
158
        public boolean isTransformsOriginalValues() {
159
                return false;
160
        }
161
162
}