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