svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / feature / impl / MyTransform.java @ 40435
History | View | Annotate | Download (3.86 KB)
1 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
---|---|---|---|
2 | |||
3 | import java.util.Arrays; |
||
4 | |||
5 | import org.gvsig.fmap.dal.DataTypes; |
||
6 | import org.gvsig.fmap.dal.exception.DataException; |
||
7 | import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
||
8 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
9 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
10 | import org.gvsig.fmap.dal.feature.Feature; |
||
11 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
12 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
13 | import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
||
14 | import org.gvsig.fmap.geom.GeometryLocator; |
||
15 | import org.gvsig.fmap.geom.GeometryManager; |
||
16 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
17 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
18 | import org.gvsig.fmap.geom.primitive.Point; |
||
19 | import org.gvsig.tools.persistence.PersistentState; |
||
20 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
21 | |||
22 | /**
|
||
23 | *
|
||
24 | * This transform adds a new attribute of type Geometry to the original store's
|
||
25 | * default FeatureType. When applying the transform to a single feature this new
|
||
26 | * attribute is assigned the value of a point whose coordinates proceed from two
|
||
27 | * numeric attributes from the store, called xname, yname.
|
||
28 | *
|
||
29 | */
|
||
30 | class MyTransform extends AbstractFeatureStoreTransform { |
||
31 | |||
32 | private FeatureType originalType;
|
||
33 | private String geomName; |
||
34 | private String xname; |
||
35 | private String yname; |
||
36 | |||
37 | /**
|
||
38 | * Empty default constructor
|
||
39 | */
|
||
40 | public MyTransform() {
|
||
41 | } |
||
42 | |||
43 | /**
|
||
44 | * Initializes the transform by assigning the source store and the names of
|
||
45 | * the necessary attributes.
|
||
46 | *
|
||
47 | * @param store
|
||
48 | * source store.
|
||
49 | *
|
||
50 | * @param geomName
|
||
51 | * name of the geometry attribute in the default feature type
|
||
52 | * from the source store.
|
||
53 | *
|
||
54 | * @param xname
|
||
55 | * name of the attribute containing the X coordinates
|
||
56 | *
|
||
57 | * @param yname
|
||
58 | * name of the attribute containing the Y coordinates
|
||
59 | *
|
||
60 | * @throws DataException
|
||
61 | */
|
||
62 | public void initialize(FeatureStore store, String geomName, String xname, String yname) throws DataException { |
||
63 | |||
64 | // Initialize some data
|
||
65 | this.setFeatureStore(store);
|
||
66 | this.geomName = geomName;
|
||
67 | this.xname = xname;
|
||
68 | this.yname = yname;
|
||
69 | |||
70 | this.originalType = store.getDefaultFeatureType();
|
||
71 | // obtain the feature type, add the new attribute and keep a reference to the resulting feature type
|
||
72 | EditableFeatureType type = store.getDefaultFeatureType().getEditable(); |
||
73 | type.add(geomName, DataTypes.GEOMETRY); |
||
74 | FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() }; |
||
75 | setFeatureTypes(Arrays.asList(types), types[0]); |
||
76 | } |
||
77 | |||
78 | /**
|
||
79 | * Applies this transform to a target editable feature, using data from the
|
||
80 | * source feature.
|
||
81 | */
|
||
82 | public void applyTransform(Feature source, EditableFeature target) |
||
83 | throws DataException {
|
||
84 | |||
85 | // copy source feature data over target feature
|
||
86 | target.copyFrom(source); |
||
87 | |||
88 | // calculate and assign new attribute's value
|
||
89 | GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
90 | Point point;
|
||
91 | try {
|
||
92 | point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
93 | point.setX(source.getDouble(xname)); |
||
94 | point.setY(source.getDouble(yname)); |
||
95 | target.setGeometry(this.geomName, point);
|
||
96 | } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
97 | throw new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public void saveToState(PersistentState state) throws PersistenceException { |
||
102 | state.set("geomName", this.geomName); |
||
103 | state.set("xname", this.xname); |
||
104 | state.set("yname", this.yname); |
||
105 | } |
||
106 | |||
107 | public void loadFromState(PersistentState state) throws PersistenceException { |
||
108 | } |
||
109 | |||
110 | /*
|
||
111 | * (non-Javadoc)
|
||
112 | *
|
||
113 | * @see
|
||
114 | * org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom
|
||
115 | * (org.gvsig.fmap.dal.feature.FeatureType)
|
||
116 | */
|
||
117 | public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
||
118 | return originalType;
|
||
119 | } |
||
120 | |||
121 | public boolean isTransformsOriginalValues() { |
||
122 | return false; |
||
123 | } |
||
124 | |||
125 | } |