svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dal / src-test / org / gvsig / fmap / dal / feature / impl / MyTransform.java @ 25587
History | View | Annotate | Download (2.97 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import org.gvsig.fmap.dal.DataTypes; |
4 |
import org.gvsig.fmap.dal.exception.DataException; |
5 |
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
6 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
7 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
8 |
import org.gvsig.fmap.dal.feature.Feature; |
9 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
10 |
import org.gvsig.fmap.geom.Geometry; |
11 |
import org.gvsig.fmap.geom.GeometryFactory; |
12 |
import org.gvsig.fmap.geom.GeometryManager; |
13 |
import org.gvsig.tools.persistence.PersistenceException; |
14 |
import org.gvsig.tools.persistence.PersistentState; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* This transform adds a new attribute of type Geometry to the
|
19 |
* original store's default FeatureType. When applying the transform
|
20 |
* to a single feature this new attribute is assigned the value of a
|
21 |
* point whose coordinates proceed from two numeric attributes from the
|
22 |
* store, called xname, yname.
|
23 |
*
|
24 |
*/
|
25 |
class MyTransform extends AbstractFeatureStoreTransform { |
26 |
|
27 |
private FeatureStore store;
|
28 |
private String geomName; |
29 |
private String xname; |
30 |
private String yname; |
31 |
|
32 |
/**
|
33 |
* Empty default constructor
|
34 |
*/
|
35 |
public MyTransform() {
|
36 |
} |
37 |
|
38 |
/**
|
39 |
* Initializes the transform by assigning the source store and the names of the necessary attributes.
|
40 |
*
|
41 |
* @param store
|
42 |
* source store.
|
43 |
*
|
44 |
* @param geomName
|
45 |
* name of the geometry attribute in the default feature type from the source store.
|
46 |
*
|
47 |
* @param xname
|
48 |
* name of the attribute containing the X coordinates
|
49 |
*
|
50 |
* @param yname
|
51 |
* name of the attribute containing the Y coordinates
|
52 |
*
|
53 |
* @throws DataException
|
54 |
*/
|
55 |
public void initialize(FeatureStore store, String geomName, String xname, String yname) throws DataException { |
56 |
|
57 |
// Initialize some data
|
58 |
this.store = store;
|
59 |
this.geomName = geomName;
|
60 |
this.xname = xname;
|
61 |
this.yname = yname;
|
62 |
|
63 |
// obtain the feature type, add the new attribute and keep a reference to the resulting feature type
|
64 |
EditableFeatureType type = store.getDefaultFeatureType().getEditable(); |
65 |
type.add(geomName, DataTypes.GEOMETRY); |
66 |
setDefaultFeatureType(type.getNotEditableCopy()); |
67 |
} |
68 |
|
69 |
/**
|
70 |
* Applies this transform to a target editable feature, using data from the
|
71 |
* source feature.
|
72 |
*/
|
73 |
public void applyTransform(Feature source, EditableFeature target) |
74 |
throws DataException {
|
75 |
|
76 |
// copy source feature data over target feature
|
77 |
target.copyFrom(source); |
78 |
|
79 |
// calculate and assign new attribute's value
|
80 |
GeometryFactory geomFactory = GeometryManager.getInstance().getGeometryFactory(); |
81 |
Geometry geom = geomFactory.createPoint2D( |
82 |
source.getDouble(xname), |
83 |
source.getDouble(yname) |
84 |
); |
85 |
target.setGeometry(this.geomName, geom);
|
86 |
} |
87 |
|
88 |
public void loadState(PersistentState state) throws PersistenceException { |
89 |
state.setTheClass(this);
|
90 |
state.set("store", store.getState());
|
91 |
state.set("geomName", this.geomName); |
92 |
state.set("xname", this.xname); |
93 |
state.set("yname", this.yname); |
94 |
} |
95 |
|
96 |
public void setState(PersistentState state) throws PersistenceException { |
97 |
} |
98 |
|
99 |
} |