Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / shp / TestSHP.java @ 26061

History | View | Annotate | Download (9.03 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
* 2008 IVER T.I. S.A.   {{Task}}
26
*/
27

    
28
package org.gvsig.fmap.dal.store.shp;
29

    
30
import java.io.File;
31
import java.util.Iterator;
32

    
33
import org.gvsig.fmap.dal.DALFileLibrary;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore;
37
import org.gvsig.fmap.dal.feature.EditableFeature;
38
import org.gvsig.fmap.dal.feature.Feature;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureSet;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
44
import org.gvsig.fmap.dal.index.spatial.jts.JTSIndexLibrary;
45
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
46
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
47
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
48
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
49

    
50
public class TestSHP extends BaseTestEditableFeatureStore {
51

    
52
        protected boolean testSHPInitialized = false;
53

    
54

    
55
        private FilesystemServerExplorer myExplorer;
56

    
57

    
58
        public static final File file_prueba = new File(TestSHP.class.getResource(
59
                        "data/prueba.shp").getFile());
60
        public static final File file_prueba_destino = new File(file_prueba
61
                        .getParent()
62
                        + "/pruebaTemp.shp");
63
        public static final File file_pruebaNull = new File(TestSHP.class
64
                        .getResource("data/pruebaNull.shp").getFile());
65

    
66
        public static final File file_poly_valencia = new File(TestSHP.class
67
                        .getResource("data/poly-valencia.shp").getFile());
68

    
69

    
70
        protected void setUp() throws Exception {
71
                super.setUp();
72

    
73
                if (testSHPInitialized) {
74
                        return;
75
                }
76

    
77
                DALFileLibrary libFile = new DALFileLibrary();
78
                libFile.initialize();
79
                libFile.postInitialize();
80

    
81
                SHPLibrary shpLib = new SHPLibrary();
82
                shpLib.initialize();
83
                shpLib.postInitialize();
84

    
85
                JTSIndexLibrary jtsIndex = new JTSIndexLibrary();
86
                jtsIndex.initialize();
87
                jtsIndex.postInitialize();
88

    
89
                testSHPInitialized = true;
90

    
91

    
92
        }
93

    
94
        public void testSimpleIteration() {
95

    
96
                FeatureStore store = null;
97
                SHPStoreParameters shpParameters = null;
98

    
99
                try {
100
                        shpParameters = (SHPStoreParameters) dataManager
101
                                        .createStoreParameters(SHPStoreProvider.NAME);
102

    
103
                        //                        shpParameters.setFile(file_poly_valencia);
104
                        shpParameters.setFile(file_prueba);
105

    
106
                        store = (FeatureStore) dataManager.createStore(shpParameters);
107
                        FeatureSet set;
108
                        FeatureType type = store.getDefaultFeatureType();
109
                        set = store.getFeatureSet();
110

    
111
                        System.out.println("Num:" + set.getSize());
112
                        Iterator it = set.iterator();
113
                        Iterator ftIt;
114
                        FeatureAttributeDescriptor desc;
115

    
116
                        int i = 0;
117
                        Feature feature;
118
                        while (it.hasNext()) {
119
                                ftIt = type.iterator();
120

    
121
                                feature = (Feature) it.next();
122
                                //                                while (ftIt.hasNext()) {
123
                                //                                        desc = (FeatureAttributeDescriptor) ftIt.next();
124
                                //                                        System.out.println(desc.getName() + ":"
125
                                //                                                        + feature.get(desc.getIndex()));
126
                                //
127
                                //                                }
128
                                //                                System.out.println(feature.get("NOMBRE"));
129
                                System.out.print(feature.getDefaultEnvelope() + "\t");
130
                                //                                System.out.print(feature.getDefaultGeometry() + "\t");
131
                                //                                System.out.println(feature.get("NOMBRE"));
132
                                System.out.println("row:" + i);
133
                                i++;
134

    
135

    
136
                        }
137

    
138
                        set.dispose();
139

    
140
                        store.dispose();
141

    
142

    
143
                } catch (DataException e3) {
144
                        e3.printStackTrace();
145
                        fail();
146
                        return;
147
                }
148

    
149

    
150
        }
151
        public void testEditing(Object x) {
152
                FeatureStore store = null;
153
                SHPStoreParameters shpParameters = null;
154

    
155
                try {
156
                        shpParameters = (SHPStoreParameters) dataManager
157
                                        .createStoreParameters(SHPStoreProvider.NAME);
158

    
159
                        shpParameters.setFile(file_poly_valencia);
160

    
161
                        store = (FeatureStore) dataManager.createStore(shpParameters);
162
                        FeatureSet set;
163
                        FeatureType type = store.getDefaultFeatureType();
164
                        System.err.println("Antes de la edici?n");
165
                        set = store.getFeatureSet();
166
                        System.out.println("Num:" + set.getSize());
167
                        Iterator it = set.iterator();
168
                        Iterator ftIt;
169
                        FeatureAttributeDescriptor desc;
170

    
171
                        int i = 0;
172
                        Feature feature;
173
                        while (it.hasNext()) {
174
                                ftIt = type.iterator();
175

    
176
                                feature = (Feature) it.next();
177
                                //                                while (ftIt.hasNext()) {
178
                                //                                        desc = (FeatureAttributeDescriptor) ftIt.next();
179
                                //                                        System.out.println(desc.getName() + ":"
180
                                //                                                        + feature.get(desc.getIndex()));
181
                                //
182
                                //                                }
183
                                //                                System.out.println(feature.get("NOMBRE"));
184
                                System.out.print(feature.getDefaultEnvelope() + "\t");
185
                                //                                System.out.print(feature.getDefaultGeometry() + "\t");
186
                                //                                System.out.println(feature.get("NOMBRE"));
187
                                System.out.println("row:" + i);
188
                                i++;
189

    
190

    
191
                        }
192

    
193
                        set.dispose();
194
                        store.edit();
195
                        EditableFeature ef = store.createNewFeature();
196
                        store.insert(ef);
197
                        store.finishEditing();
198
                        System.err.println("Despu?s de la edici?n");
199
                        set = store.getFeatureSet();
200
                        System.out.println("Num:" + set.getSize());
201
                        it = set.iterator();
202

    
203
                        i = 0;
204
                        while (it.hasNext()) {
205
                                ftIt = type.iterator();
206

    
207
                                feature = (Feature) it.next();
208
                                //                                while (ftIt.hasNext()) {
209
                                //                                        desc = (FeatureAttributeDescriptor) ftIt.next();
210
                                //                                        System.out.println(desc.getName() + ":"
211
                                //                                                        + feature.get(desc.getIndex()));
212
                                //
213
                                //                                }
214
                                //                                System.out.println(feature.get("NOMBRE"));
215
                                System.out.print(feature.getDefaultEnvelope() + "\t");
216
                                //                                System.out.print(feature.getDefaultGeometry() + "\t");
217
                                //                                System.out.println(feature.get("NOMBRE"));
218
                                System.out.println("row:" + i);
219
                                i++;
220

    
221

    
222
                        }
223

    
224
                        set.dispose();
225

    
226
                        store.dispose();
227

    
228

    
229
                } catch (DataException e3) {
230
                        e3.printStackTrace();
231
                        fail();
232
                        return;
233
                }
234
        }
235

    
236
        public void testExport(Object x) {
237
                DBFStoreParameters dbfParameters = null;
238

    
239
                try {
240
                        dbfParameters = (DBFStoreParameters) dataManager
241
                                        .createStoreParameters(DBFStoreProvider.NAME);
242

    
243
                        dbfParameters.setFile(file_prueba);
244

    
245
                        FeatureStore store = (FeatureStore) dataManager
246
                                        .createStore(dbfParameters);
247
                        FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) dataManager.createServerExplorerParameters(FilesystemServerExplorerParameters.DYNCLASS_NAME);
248
                        explorerParams.setRoot(file_prueba.getParent());
249

    
250
                        FilesystemServerExplorer explorer=(FilesystemServerExplorer) dataManager.createServerExplorer(explorerParams);
251

    
252
                        NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(file_prueba_destino);
253

    
254
                        store.export(explorer, newParams);
255

    
256
                        FeatureStore result = (FeatureStore)dataManager.createStore(newParams);
257

    
258
                        FeatureSet set = result.getFeatureSet();
259
                        FeatureSet originalSet=store.getFeatureSet();
260
                        assertEquals(set.getSize(), originalSet.getSize());
261

    
262
                        Iterator originalIter=originalSet.iterator();
263
                        Iterator iter = set.iterator();
264
                        while (iter.hasNext()) {
265
                                assertEquals(originalIter.next(),iter.next());
266
                        }
267

    
268
                        result.dispose();
269
                        explorer.remove(newParams);
270

    
271
                } catch (DataException e3) {
272
                        e3.printStackTrace();
273
                        fail();
274
                        return;
275
                }
276

    
277
        }
278

    
279
        /*
280
         * (non-Javadoc)
281
         * 
282
         * @see
283
         * org.gvsig.fmap.dal.feature.BaseTestFeatureStore#getDefaultDataStoreParameters
284
         * ()
285
         */
286
        public DataStoreParameters getDefaultDataStoreParameters()
287
                        throws DataException {
288
                SHPStoreParameters shpParameters = (SHPStoreParameters) dataManager
289
                                .createStoreParameters(SHPStoreProvider.NAME);
290

    
291
                //                        shpParameters.setFile(file_poly_valencia);
292
                shpParameters.setFile(file_prueba);
293
                return shpParameters;
294
        }
295

    
296
        /*
297
         * (non-Javadoc)
298
         * 
299
         * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
300
         */
301
        public boolean hasExplorer() {
302
                return true;
303
        }
304

    
305
        /*
306
         * (non-Javadoc)
307
         * 
308
         * @seeorg.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore#
309
         * getDefaultNewDataStoreParameters()
310
         */
311
        public NewFeatureStoreParameters getDefaultNewDataStoreParameters()
312
                        throws DataException {
313
                if (this.myExplorer == null) {
314
                        SHPStoreParameters parameters = (SHPStoreParameters) this
315
                                        .getDefaultDataStoreParameters();
316
                        FeatureStore store = (FeatureStore) dataManager
317
                                        .createStore(parameters);
318
                        myExplorer = (FilesystemServerExplorer) store.getExplorer();
319
                        store.dispose();
320
                }
321

    
322
                return (NewFeatureStoreParameters) myExplorer
323
                                .getAddParameters(file_prueba_destino);
324
        }
325

    
326
}