Statistics
| Revision:

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

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

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

    
74
        /**
75
         * This method initializes the transformation, sets the name of the parameters and 
76
         * sets the value of the {@link FeatureType} returned by the transformation. 
77
         * @param store
78
         * The original store. 
79
         * @param geometryFieldName
80
         * The field that contains the geometric attribute.
81
         * @param xFieldName
82
         * The field that contains the X coordinate.
83
         * @param yFieldName
84
         * The field that contains the Y coordinate.
85
         * @throws DataException
86
         */
87
        public void initialize(FeatureStore store, String geometryFieldName, String xFieldName, String yFieldName) throws DataException{
88
                setFeatureStore(store);
89
                this.xFieldName = xFieldName;
90
                this.yFieldName = yFieldName;
91
                if ((geometryFieldName == null) || (geometryFieldName.equals(""))){
92
                        this.geometryFieldName = "the_geom";
93
                }else{
94
                        this.geometryFieldName = geometryFieldName;
95
                }
96
                this.originalFeatureType = this.getFeatureStore()
97
                .getDefaultFeatureType();
98
                
99
                EditableFeatureType type = originalFeatureType.getEditable();
100
                if (type.get(this.geometryFieldName) == null){
101
                        EditableFeatureAttributeDescriptor attributeDescriptor = type.add(this.geometryFieldName,  DataTypes.GEOMETRY);
102
                        attributeDescriptor.setGeometryType(TYPES.POINT);
103
                        attributeDescriptor.setGeometrySubType(SUBTYPES.GEOM2D);
104
                        attributeDescriptor.setSRS(CRSFactory.getCRS("EPSG:23030"));
105
                }
106
                
107
                type.setDefaultGeometryAttributeName(this.geometryFieldName);
108
                FeatureType[] types = new FeatureType[] { type.getNotEditableCopy() };
109
                setFeatureTypes(Arrays.asList(types), types[0]);
110
        }
111
        
112
        /* (non-Javadoc)
113
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
114
         */
115
        public void applyTransform(Feature source, EditableFeature target)
116
                        throws DataException {
117
                this.copySourceToTarget(source, target);
118

    
119
                try {                        
120
                        Point point = geometryManager.createPoint(source.getDouble(xFieldName),
121
                                        source.getDouble(yFieldName),
122
                                        SUBTYPES.GEOM2D);
123
                        target.set(geometryFieldName, point);
124
                        target.setDefaultGeometry(point);                                
125
                } catch (CreateGeometryException e) {
126
                        throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e);
127
                }                
128
        }
129
        
130
        /**
131
         * @param source
132
         * @param target
133
         */
134
        private void copySourceToTarget(Feature source, EditableFeature target) {
135
                FeatureAttributeDescriptor attr, attrTrg;
136
                FeatureType ftSrc = source.getType();
137
                FeatureType ftTrg = target.getType();
138

    
139

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

    
150
                        }
151
                }
152

    
153
        }
154

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

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

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

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

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

    
194
}
195