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 @ 40559
History | View | Annotate | Download (5.53 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 | /* gvSIG. Geographic Information System of the Valencian Government
|
25 | *
|
||
26 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
27 | * of the Valencian Government (CIT)
|
||
28 | *
|
||
29 | * This program is free software; you can redistribute it and/or
|
||
30 | * modify it under the terms of the GNU General Public License
|
||
31 | * as published by the Free Software Foundation; either version 2
|
||
32 | * of the License, or (at your option) any later version.
|
||
33 | *
|
||
34 | * This program is distributed in the hope that it will be useful,
|
||
35 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
36 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
37 | * GNU General Public License for more details.
|
||
38 | *
|
||
39 | * You should have received a copy of the GNU General Public License
|
||
40 | * along with this program; if not, write to the Free Software
|
||
41 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
42 | * MA 02110-1301, USA.
|
||
43 | *
|
||
44 | */
|
||
45 | |||
46 | /*
|
||
47 | * AUTHORS (In addition to CIT):
|
||
48 | * 2009 IVER T.I {{Task}}
|
||
49 | */
|
||
50 | |||
51 | package org.gvsig.fmap.dal.store.dbf; |
||
52 | |||
53 | import java.util.ArrayList; |
||
54 | import java.util.List; |
||
55 | |||
56 | import org.gvsig.fmap.dal.DataTypes; |
||
57 | import org.gvsig.fmap.dal.exception.DataException; |
||
58 | import org.gvsig.fmap.dal.exception.ReadException; |
||
59 | import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
||
60 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
61 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
62 | import org.gvsig.fmap.dal.feature.Feature; |
||
63 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
64 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
65 | import org.gvsig.fmap.geom.Geometry; |
||
66 | import org.gvsig.fmap.geom.GeometryLocator; |
||
67 | import org.gvsig.fmap.geom.GeometryManager; |
||
68 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
69 | import org.gvsig.tools.persistence.PersistentState; |
||
70 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
71 | |||
72 | public class MyTransform extends AbstractFeatureStoreTransform { |
||
73 | |||
74 | private String geomName; |
||
75 | private String xname; |
||
76 | private String yname; |
||
77 | |||
78 | private GeometryManager geomManager;
|
||
79 | |||
80 | /**
|
||
81 | * A default constructor with out parameters
|
||
82 | */
|
||
83 | public MyTransform() {
|
||
84 | this.geomManager = GeometryLocator.getGeometryManager();
|
||
85 | |||
86 | } |
||
87 | |||
88 | /**
|
||
89 | * Initializes the transform by assigning the source store and the names of
|
||
90 | * the necessary attributes.
|
||
91 | *
|
||
92 | * @param store
|
||
93 | * source store.
|
||
94 | *
|
||
95 | * @param geomName
|
||
96 | * name of the geometry attribute in the default feature type
|
||
97 | * from the source store.
|
||
98 | *
|
||
99 | * @param xname
|
||
100 | * name of the attribute containing the X coordinates
|
||
101 | *
|
||
102 | * @param yname
|
||
103 | * name of the attribute containing the Y coordinates
|
||
104 | *
|
||
105 | * @throws DataException
|
||
106 | */
|
||
107 | public MyTransform initialize(FeatureStore store, String geomName, |
||
108 | String xname, String yname) throws DataException { |
||
109 | |||
110 | // Initialize some data
|
||
111 | setFeatureStore(store); |
||
112 | this.geomName = geomName;
|
||
113 | this.xname = xname;
|
||
114 | this.yname = yname;
|
||
115 | |||
116 | // obtain the feature type, add the new attribute
|
||
117 | // and keep a reference to the
|
||
118 | // resulting feature type
|
||
119 | EditableFeatureType type = getFeatureStore().getDefaultFeatureType() |
||
120 | .getEditable(); |
||
121 | type.add(geomName, DataTypes.GEOMETRY); |
||
122 | List list = new ArrayList(1); |
||
123 | list.add(type.getNotEditableCopy()); |
||
124 | setFeatureTypes(list, (FeatureType) list.get(0));
|
||
125 | return this; |
||
126 | } |
||
127 | |||
128 | /**
|
||
129 | * Applies this transform to a target editable feature, using data from the
|
||
130 | * source feature.
|
||
131 | */
|
||
132 | public void applyTransform(Feature source, EditableFeature target) |
||
133 | throws DataException {
|
||
134 | |||
135 | // copy source feature data over target feature
|
||
136 | target.copyFrom(source); |
||
137 | |||
138 | // calculate and assign new attribute's value
|
||
139 | Geometry geom; |
||
140 | try {
|
||
141 | geom = geomManager.createPoint(source.getDouble(xname), source |
||
142 | .getDouble(yname), Geometry.SUBTYPES.GEOM2D); |
||
143 | } catch (CreateGeometryException e) {
|
||
144 | throw new ReadException("XYTranform", e); |
||
145 | } |
||
146 | target.setGeometry(this.geomName, geom);
|
||
147 | } |
||
148 | |||
149 | |||
150 | public void saveToState(PersistentState state) throws PersistenceException { |
||
151 | state.set("xname", xname);
|
||
152 | state.set("yname", yname);
|
||
153 | state.set("geomName", geomName);
|
||
154 | } |
||
155 | |||
156 | public void loadFromState(PersistentState state) |
||
157 | throws PersistenceException {
|
||
158 | this.xname = state.getString("xname"); |
||
159 | this.yname = state.getString("yname"); |
||
160 | this.geomName = state.getString("geomName"); |
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Returns the FeatureType to use to get data from original store
|
||
165 | */
|
||
166 | public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
||
167 | EditableFeatureType ed = targetFeatureType.getEditable(); |
||
168 | ed.remove(this.geomName);
|
||
169 | return ed.getNotEditableCopy();
|
||
170 | } |
||
171 | |||
172 | /**
|
||
173 | * Informs that original values of store don't will be modified
|
||
174 | */
|
||
175 | public boolean isTransformsOriginalValues() { |
||
176 | return false; |
||
177 | } |
||
178 | |||
179 | } |