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