Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extDalTransformEventTheme / src / org / gvsig / app / eventtheme / dal / feature / EventThemeTransform.java @ 29715

History | View | Annotate | Download (6.81 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.gvsig.fmap.dal.DataStore;
33
import org.gvsig.fmap.dal.DataTypes;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
36
import org.gvsig.fmap.dal.feature.EditableFeature;
37
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
38
import org.gvsig.fmap.dal.feature.EditableFeatureType;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.geom.GeometryLocator;
44
import org.gvsig.fmap.geom.GeometryManager;
45
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
46
import org.gvsig.fmap.geom.Geometry.TYPES;
47
import org.gvsig.fmap.geom.exception.CreateGeometryException;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.tools.persistence.PersistenceException;
50
import org.gvsig.tools.persistence.PersistentState;
51

    
52
/**
53
 * This class implements a transformation for a events theme. The original 
54
 * {@link DataStore} have to have a couple of attributes, one with the X 
55
 * coordinate and the second one with the Y coordinate. The result of 
56
 * the transformation is a {@link DataStore} that has a new geometric 
57
 * attribute and its value is a point with the coordinates specified in 
58
 * the original {@link DataStore}. 
59
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
60
 */
61
public class EventThemeTransform extends AbstractFeatureStoreTransform {
62
        private String xFieldName = null;
63
        private String yFieldName = null;
64
        private String geometryFieldName = null;
65
        private FeatureType originalFeatureType;
66
        private static final GeometryManager geometryManager = GeometryLocator.getGeometryManager();
67
                
68
        public EventThemeTransform() {
69
                super();
70
        }
71

    
72
        /**
73
         * This method initializes the transformation, sets the name of the parameters and 
74
         * sets the value of the {@link FeatureType} returned by the transformation. 
75
         * @param store
76
         * The original store. 
77
         * @param geometryFieldName
78
         * The field that contains the geometric attribute.
79
         * @param xFieldName
80
         * The field that contains the X coordinate.
81
         * @param yFieldName
82
         * The field that contains the Y coordinate.
83
         * @throws DataException
84
         */
85
        public void initialize(FeatureStore store, String geometryFieldName, String xFieldName, String yFieldName) throws DataException{
86
                setFeatureStore(store);
87
                this.xFieldName = xFieldName;
88
                this.yFieldName = yFieldName;
89
                if ((geometryFieldName == null) || (geometryFieldName.equals(""))){
90
                        this.geometryFieldName = "the_geom";
91
                }else{
92
                        this.geometryFieldName = geometryFieldName;
93
                }
94
                this.originalFeatureType = this.getFeatureStore()
95
                .getDefaultFeatureType();
96
                
97
                EditableFeatureType type = this.getFeatureStore().getDefaultFeatureType().getEditable();
98
                if (type.get(this.geometryFieldName) == null){
99
                        EditableFeatureAttributeDescriptor attributeDescriptor = type.add(this.geometryFieldName,  DataTypes.GEOMETRY);
100
                        attributeDescriptor.setGeometryType(TYPES.POINT);
101
                        attributeDescriptor.setGeometrySubType(SUBTYPES.GEOM2D);                        
102
                }
103
                
104
                type.setDefaultGeometryAttributeName(this.geometryFieldName);
105
                FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
106
                setFeatureTypes(Arrays.asList(types), types[0]);
107
        }
108
        
109
        /* (non-Javadoc)
110
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
111
         */
112
        public void applyTransform(Feature source, EditableFeature target)
113
                        throws DataException {
114
                this.copySourceToTarget(source, target);
115
                
116
                Object x = source.get(xFieldName);
117
                Object y = source.get(yFieldName);
118
                Double dx = Double.valueOf(x.toString());
119
                Double dy = Double.valueOf(y.toString());
120
                try {                        
121
                        Point point = geometryManager.createPoint(dx, dy, SUBTYPES.GEOM2D);
122
                        target.set(geometryFieldName, point);
123
                        target.setDefaultGeometry(point);                                
124
                } catch (CreateGeometryException e) {
125
                        throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e);
126
                }                
127
        }
128
        
129
        /**
130
         * @param source
131
         * @param target
132
         */
133
        private void copySourceToTarget(Feature source, EditableFeature target) {
134
                FeatureAttributeDescriptor attr, attrTrg;
135
                FeatureType ftSrc = source.getType();
136
                FeatureType ftTrg = target.getType();
137

    
138

    
139
                for (int i = 0; i < source.getType().size(); i++) {
140
                        attr = ftSrc.getAttributeDescriptor(i);
141
                        if (ftTrg.getIndex(attr.getName()) > -1) {
142
                                try {
143
                                        target.set(attr.getName(), source.get(i));
144
                                } catch (IllegalArgumentException e) {
145
                                        attrTrg = ftTrg.getAttributeDescriptor(attr.getName());
146
                                        target.set(attrTrg.getIndex(), attrTrg.getDefaultValue());
147
                                }
148

    
149
                        }
150
                }
151

    
152
        }
153

    
154
        /* (non-Javadoc)
155
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom(org.gvsig.fmap.dal.feature.FeatureType)
156
         */
157
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
158
                return this.originalFeatureType;
159
        }
160

    
161
        /* (non-Javadoc)
162
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#isTransformsOriginalValues()
163
         */
164
        public boolean isTransformsOriginalValues() {
165
                return true;
166
        }
167

    
168
        /* (non-Javadoc)
169
         * @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
170
         */
171
        public void saveToState(PersistentState state) throws PersistenceException {
172
                // TODO Auto-generated method stub
173
                
174
        }
175

    
176
        /* (non-Javadoc)
177
         * @see org.gvsig.tools.persistence.Persistent#setState(org.gvsig.tools.persistence.PersistentState)
178
         */
179
        public void setState(PersistentState state) throws PersistenceException {
180
                // TODO Auto-generated method stub
181
                
182
        }
183

    
184
        /* (non-Javadoc)
185
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
186
         */
187
        public void loadFromState(PersistentState state)
188
                        throws PersistenceException {
189
                // TODO Auto-generated method stub
190
                
191
        }
192

    
193
}
194