Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / vectorial / ReprojectDefaultGeometry.java @ 40559

History | View | Annotate | Download (8.56 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42
* MA  02110-1301, USA.
43
*
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 IVER T.I   {{Task}}
49
*/
50

    
51
/**
52
 *
53
 */
54
package org.gvsig.fmap.mapcontext.layers.vectorial;
55

    
56
import java.util.ArrayList;
57
import java.util.Iterator;
58
import java.util.List;
59

    
60
import org.cresques.cts.ICoordTrans;
61
import org.cresques.cts.IProjection;
62
import org.gvsig.fmap.dal.exception.DataException;
63
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform;
64
import org.gvsig.fmap.dal.feature.EditableFeature;
65
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
66
import org.gvsig.fmap.dal.feature.EditableFeatureType;
67
import org.gvsig.fmap.dal.feature.Feature;
68
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
69
import org.gvsig.fmap.dal.feature.FeatureStore;
70
import org.gvsig.fmap.dal.feature.FeatureType;
71
import org.gvsig.fmap.geom.Geometry;
72
import org.gvsig.tools.ToolsLocator;
73
import org.gvsig.tools.dynobject.DynStruct;
74
import org.gvsig.tools.persistence.PersistenceManager;
75
import org.gvsig.tools.persistence.PersistentState;
76
import org.gvsig.tools.persistence.exception.PersistenceException;
77

    
78
/**
79
 * @author jmvivo
80
 *
81
 */
82
public class ReprojectDefaultGeometry extends AbstractFeatureStoreTransform {
83

    
84
        private IProjection targetSRS;
85
        private ICoordTrans ct;
86
        private IProjection sourceSRS;
87
        private FeatureType orgDefFeatureType;
88
        private List orgFeatureTypes;
89

    
90
        public ReprojectDefaultGeometry(){
91
                
92
        }
93
        
94
//        public ReprojectDefaultGeometry(IProjection targetSRS){
95
//                super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
96
//                this.targetSRS = targetSRS;
97
//                this.ct = null;
98
//        }
99
//        
100
//        public ReprojectDefaultGeometry(ICoordTrans ct){
101
//                super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
102
//                this.ct = ct;
103
//        }
104
                
105
        public IProjection getTargetSRS() {
106
                return targetSRS;
107
        }
108

    
109
        public void setTargetSRS(IProjection targetSRS) {
110
                this.targetSRS = targetSRS;
111
        }
112
        
113
//        public void setCoordTrans(ICoordTrans ct){
114
//                this.ct = ct;
115
//                if ( getFeatureStore() != null ){
116
//                        setFeatureStore(getFeatureStore());
117
//                }
118
//        }
119

    
120
        /* (non-Javadoc)
121
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
122
         */
123
        public void applyTransform(Feature source, EditableFeature target)
124
                        throws DataException {
125
                FeatureAttributeDescriptor attr;
126
                Iterator iter = target.getType().iterator();
127
                int defGeomIndex = target.getType().getDefaultGeometryAttributeIndex();
128
                while (iter.hasNext()) {
129
                        attr = (FeatureAttributeDescriptor)iter.next();
130
                        if (attr.getIndex() == defGeomIndex) {
131
                                Geometry geom =source.getDefaultGeometry();
132
                                geom.reProject(ct);
133
                                target.setDefaultGeometry(geom);
134
                        } else {
135
                                target.set(attr.getIndex(), source.get(attr.getName()));
136
                        }
137

    
138
                }
139

    
140
        }
141

    
142
        public void setFeatureStore(FeatureStore store) {
143
                try {
144
                        orgDefFeatureType = store.getDefaultFeatureType();
145
                        orgFeatureTypes = store.getFeatureTypes();
146
                        EditableFeatureType defFType = orgDefFeatureType
147
                                        .getEditable();
148
                        EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) defFType
149
                                        .getAttributeDescriptor(defFType
150
                                                        .getDefaultGeometryAttributeName());
151
//                        if (ct!=null){
152
//                                targetSRS = ct.getPDest();
153
//                                sourceSRS = ct.getPOrig();
154
//                        } else {
155
                                sourceSRS = attr.getSRS();
156
                                ct = sourceSRS.getCT(targetSRS);
157
//                        }
158
                        attr.setSRS(this.targetSRS);
159
                        FeatureType defaultType = defFType.getNotEditableCopy();
160
                        List types = new ArrayList();
161
                        Iterator iter = orgFeatureTypes.iterator();
162
                        FeatureType tmp;
163
                        while (iter.hasNext()) {
164
                                tmp = (FeatureType) iter.next();
165
                                if (tmp.getId().equals(defaultType.getId())) {
166
                                        types.add(defaultType);
167
                                } else {
168
                                        types.add(tmp);
169
                                }
170
                        }
171

    
172
                        this.setFeatureTypes(types, defaultType);
173
                } catch (DataException e) {
174
                        // FIXME
175
                        throw new RuntimeException(e);
176
                }
177

    
178
                super.setFeatureStore(store);
179
        }
180

    
181
        /* (non-Javadoc)
182
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom(org.gvsig.fmap.dal.feature.FeatureType)
183
         */
184
        public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
185
                EditableFeatureType result = null;
186
                FeatureType tmp;
187
                EditableFeatureAttributeDescriptor attr;
188
                Iterator iter = orgFeatureTypes.iterator();
189
                Iterator iterAttr;
190
                while (iter.hasNext()) {
191
                        tmp = (FeatureType) iter.next();
192
                        if (tmp.getId().equals(targetFeatureType.getId())) {
193
                                result = tmp.getEditable();
194
                                iterAttr = result.iterator();
195
                                while (iterAttr.hasNext()) {
196
                                        attr = (EditableFeatureAttributeDescriptor) iterAttr.next();
197
                                        if (targetFeatureType.getIndex(attr.getName()) < 0) {
198
                                                iterAttr.remove();
199
                                        }
200
                                }
201
                                break;
202
                        }
203
                }
204
                return result.getNotEditableCopy();
205
        }
206

    
207
        /* (non-Javadoc)
208
         * @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#isTransformsOriginalValues()
209
         */
210
        public boolean isTransformsOriginalValues() {
211
                return true;
212
        }
213

    
214
        /* (non-Javadoc)
215
         * @see org.gvsig.tools.persistence.Persistent#loadState(org.gvsig.tools.persistence.PersistentState)
216
         */
217
        public void saveToState(PersistentState state) throws PersistenceException {
218
                super.saveToState(state);
219
                
220
                state.set("source_crs", this.sourceSRS);
221
                state.set("source_defaultFeatureType", this.orgDefFeatureType);
222
                state.set("source_featureTypes", this.orgFeatureTypes);
223
                state.set("target_crs", this.targetSRS);
224
                state.set("coordtrans", this.ct);
225
        }
226

    
227
        /* (non-Javadoc)
228
         * @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
229
         */
230
        public void loadFromState(PersistentState state) throws PersistenceException {
231
                super.loadFromState(state);
232
                this.sourceSRS = (IProjection) state.get("source_crs");
233
                this.orgDefFeatureType = (FeatureType) state.get("source_defaultFeatureType");
234
                this.orgFeatureTypes = new ArrayList( (List) state.get("source_featureTypes"));
235
                this.targetSRS = (IProjection) state.get("target_crs");
236
                this.ct = (ICoordTrans) state.get("coordtrans");
237
        }
238

    
239
        public static void registerPersistent() {
240
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
241
                DynStruct definition = manager.addDefinition(
242
                                ReprojectDefaultGeometry.class,
243
                                "ReprojectDefaultGeometry",
244
                                "ReprojectDefaultGeometry Persistence definition",
245
                                null, 
246
                                null
247
                );
248
                definition.extend( manager.getDefinition(AbstractFeatureStoreTransform.class));
249
                
250
                definition.addDynFieldObject("source_crs")
251
                        .setClassOfValue(IProjection.class)
252
                        .setMandatory(true);
253
                definition.addDynFieldObject("source_defaultFeatureType")
254
                        .setClassOfValue(FeatureType.class)
255
                        .setMandatory(true);
256
                definition.addDynFieldList("source_featureTypes")
257
                        .setClassOfItems(FeatureType.class)
258
                        .setMandatory(true);
259
                definition.addDynFieldObject("target_crs")
260
                        .setClassOfValue(IProjection.class)
261
                        .setMandatory(true);
262
                definition.addDynFieldObject("coordtrans")
263
                        .setClassOfValue(ICoordTrans.class)
264
                        .setMandatory(true);
265

    
266
        }
267
}