svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extDalTransformEventTheme / src / org / gvsig / app / eventtheme / dal / feature / EventThemeTransform.java @ 37522
History | View | Annotate | Download (10.7 KB)
1 |
/* 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.app.eventtheme.dal.feature; |
29 |
|
30 |
import java.util.Arrays; |
31 |
|
32 |
import org.cresques.cts.IProjection; |
33 |
|
34 |
import org.gvsig.fmap.dal.DataStore; |
35 |
import org.gvsig.fmap.dal.DataTypes; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.exception.InitializeException; |
38 |
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
39 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 |
import org.gvsig.fmap.dal.feature.Feature; |
43 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
47 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
48 |
import org.gvsig.fmap.geom.GeometryLocator; |
49 |
import org.gvsig.fmap.geom.GeometryManager; |
50 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
51 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
52 |
import org.gvsig.fmap.geom.primitive.Envelope; |
53 |
import org.gvsig.fmap.geom.primitive.Point; |
54 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
55 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
56 |
import org.gvsig.tools.ToolsLocator; |
57 |
import org.gvsig.tools.dynobject.DynStruct; |
58 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
59 |
import org.gvsig.tools.persistence.PersistenceManager; |
60 |
import org.gvsig.tools.persistence.PersistentState; |
61 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
62 |
|
63 |
/**
|
64 |
* This class implements a transformation for a events theme. The original
|
65 |
* {@link DataStore} have to have a couple of attributes, one with the X
|
66 |
* coordinate and the second one with the Y coordinate. The result of
|
67 |
* the transformation is a {@link DataStore} that has a new geometric
|
68 |
* attribute and its value is a point with the coordinates specified in
|
69 |
* the original {@link DataStore}.
|
70 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
71 |
*/
|
72 |
public class EventThemeTransform extends AbstractFeatureStoreTransform { |
73 |
|
74 |
public static final String PERSISTENCE_DEFINITION_NAME = "EventThemeTransform"; |
75 |
|
76 |
private String xFieldName = null; |
77 |
private String yFieldName = null; |
78 |
private String geometryFieldName = null; |
79 |
private IProjection projection = null; |
80 |
private FeatureType originalFeatureType;
|
81 |
private GeometryManager geometryManager = GeometryLocator.getGeometryManager();
|
82 |
private Envelope envelope;
|
83 |
|
84 |
public EventThemeTransform() {
|
85 |
super();
|
86 |
geometryManager = GeometryLocator.getGeometryManager(); |
87 |
} |
88 |
|
89 |
/**
|
90 |
* This method initializes the transformation, sets the name of the parameters and
|
91 |
* sets the value of the {@link FeatureType} returned by the transformation.
|
92 |
* @param store
|
93 |
* The original store.
|
94 |
* @param geometryFieldName
|
95 |
* The field that contains the geometric attribute.
|
96 |
* @param xFieldName
|
97 |
* The field that contains the X coordinate.
|
98 |
* @param yFieldName
|
99 |
* The field that contains the Y coordinate.
|
100 |
* @throws DataException
|
101 |
*/
|
102 |
public void initialize(FeatureStore store, String geometryFieldName, String xFieldName, String yFieldName, |
103 |
IProjection projection) throws DataException{
|
104 |
setFeatureStore(store); |
105 |
this.xFieldName = xFieldName;
|
106 |
this.yFieldName = yFieldName;
|
107 |
this.projection = projection;
|
108 |
if ((geometryFieldName == null) || (geometryFieldName.equals(""))){ |
109 |
this.geometryFieldName = "the_geom"; |
110 |
}else{
|
111 |
this.geometryFieldName = geometryFieldName;
|
112 |
} |
113 |
this.originalFeatureType = this.getFeatureStore() |
114 |
.getDefaultFeatureType(); |
115 |
|
116 |
EditableFeatureType type = originalFeatureType.getEditable(); |
117 |
if (type.get(this.geometryFieldName) == null){ |
118 |
EditableFeatureAttributeDescriptor attributeDescriptor = type.add(this.geometryFieldName, DataTypes.GEOMETRY);
|
119 |
try {
|
120 |
attributeDescriptor.setGeometryType(geometryManager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM2D)); |
121 |
} catch (GeometryTypeNotSupportedException e) {
|
122 |
throw new InitializeException(e); |
123 |
} catch (GeometryTypeNotValidException e) {
|
124 |
throw new InitializeException(e); |
125 |
} |
126 |
attributeDescriptor.setSRS(projection); |
127 |
} |
128 |
|
129 |
try {
|
130 |
envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D); |
131 |
} catch (CreateEnvelopeException e) {
|
132 |
throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(e); |
133 |
} |
134 |
|
135 |
type.setDefaultGeometryAttributeName(this.geometryFieldName);
|
136 |
FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() }; |
137 |
setFeatureTypes(Arrays.asList(types), types[0]); |
138 |
} |
139 |
|
140 |
/* (non-Javadoc)
|
141 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
|
142 |
*/
|
143 |
public void applyTransform(Feature source, EditableFeature target) |
144 |
throws DataException {
|
145 |
this.copySourceToTarget(source, target);
|
146 |
|
147 |
try {
|
148 |
Point point = geometryManager.createPoint(
|
149 |
new Double(source.get(xFieldName).toString()), |
150 |
new Double(source.get(yFieldName).toString()), |
151 |
SUBTYPES.GEOM2D); |
152 |
target.set(geometryFieldName, point); |
153 |
target.setDefaultGeometry(point); |
154 |
envelope.add(point.getEnvelope()); |
155 |
} catch (CreateGeometryException e) {
|
156 |
throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e); |
157 |
} |
158 |
} |
159 |
|
160 |
/**
|
161 |
* @param source
|
162 |
* @param target
|
163 |
*/
|
164 |
private void copySourceToTarget(Feature source, EditableFeature target) { |
165 |
FeatureAttributeDescriptor attr, attrTrg; |
166 |
FeatureType ftSrc = source.getType(); |
167 |
FeatureType ftTrg = target.getType(); |
168 |
|
169 |
|
170 |
for (int i = 0; i < source.getType().size(); i++) { |
171 |
attr = ftSrc.getAttributeDescriptor(i); |
172 |
if (ftTrg.getIndex(attr.getName()) > -1) { |
173 |
try {
|
174 |
target.set(attr.getName(), source.get(i)); |
175 |
} catch (IllegalArgumentException e) { |
176 |
attrTrg = ftTrg.getAttributeDescriptor(attr.getName()); |
177 |
target.set(attrTrg.getIndex(), attrTrg.getDefaultValue()); |
178 |
} |
179 |
|
180 |
} |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
/* (non-Javadoc)
|
186 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom(org.gvsig.fmap.dal.feature.FeatureType)
|
187 |
*/
|
188 |
public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
189 |
return this.originalFeatureType; |
190 |
} |
191 |
|
192 |
/* (non-Javadoc)
|
193 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#isTransformsOriginalValues()
|
194 |
*/
|
195 |
public boolean isTransformsOriginalValues() { |
196 |
return true; |
197 |
} |
198 |
|
199 |
public static void registerPersistent() { |
200 |
PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager(); |
201 |
|
202 |
if( persistenceManager.getDefinition(AbstractFeatureStoreTransform.class) == null ) { |
203 |
AbstractFeatureStoreTransform.registerPersistent(); |
204 |
} |
205 |
|
206 |
DynStruct definition = persistenceManager.getDefinition(PERSISTENCE_DEFINITION_NAME); |
207 |
|
208 |
if (definition == null){ |
209 |
definition = persistenceManager.addDefinition( |
210 |
EventThemeTransform.class, |
211 |
PERSISTENCE_DEFINITION_NAME, |
212 |
"EventThemeTransform Persistence definition",
|
213 |
null,
|
214 |
null
|
215 |
); |
216 |
definition.extend(PersistenceManager.PERSISTENCE_NAMESPACE, |
217 |
ABSTRACT_FEATURESTORE_DYNCLASS_NAME); |
218 |
|
219 |
definition.addDynFieldString("geometryFieldName").setMandatory(true); |
220 |
definition.addDynFieldString("xFieldName").setMandatory(true); |
221 |
definition.addDynFieldString("yFieldName").setMandatory(true); |
222 |
definition.addDynFieldObject("projection").setType(DataTypes.CRS);
|
223 |
} |
224 |
} |
225 |
|
226 |
/* (non-Javadoc)
|
227 |
* @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
|
228 |
*/
|
229 |
public void saveToState(PersistentState state) throws PersistenceException { |
230 |
super.saveToState(state);
|
231 |
state.set("geometryFieldName", this.geometryFieldName); |
232 |
state.set("xFieldName", this.xFieldName); |
233 |
state.set("yFieldName", this.yFieldName); |
234 |
state.set("projection", this.projection); |
235 |
} |
236 |
|
237 |
/* (non-Javadoc)
|
238 |
* @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
|
239 |
*/
|
240 |
public void loadFromState(PersistentState state) |
241 |
throws PersistenceException {
|
242 |
super.loadFromState(state);
|
243 |
String geometryFieldName = state.getString("geometryFieldName"); |
244 |
String xFieldName = state.getString("xFieldName"); |
245 |
String yFieldName = state.getString("yFieldName"); |
246 |
IProjection projection = (IProjection)state.get("projection");
|
247 |
try {
|
248 |
initialize(getFeatureStore(), geometryFieldName, xFieldName, yFieldName, projection); |
249 |
} catch (DataException e) {
|
250 |
throw new PersistenceException("Impossible to create the transform", e); |
251 |
} |
252 |
} |
253 |
|
254 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
255 |
if (DataStore.METADATA_CRS.equals(name)){
|
256 |
return projection;
|
257 |
}else if(DataStore.METADATA_ENVELOPE.equals(name)){ |
258 |
return envelope;
|
259 |
} |
260 |
return null; |
261 |
} |
262 |
|
263 |
public boolean hasDynValue(String name) { |
264 |
return ((DataStore.METADATA_CRS.equals(name)) ||
|
265 |
(DataStore.METADATA_ENVELOPE.equals(name))); |
266 |
} |
267 |
} |
268 |
|