Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / shp / TestSHP.java @ 27042

History | View | Annotate | Download (9.24 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
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary;
50

    
51
public class TestSHP extends BaseTestEditableFeatureStore {
52

    
53
        protected boolean testSHPInitialized = false;
54

    
55

    
56
        private FilesystemServerExplorer myExplorer;
57

    
58

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

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

    
70

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

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

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

    
82
                DefaultGeometryLibrary lib = new DefaultGeometryLibrary();
83
                lib.initialize();
84
                lib.postInitialize();
85

    
86
                SHPLibrary shpLib = new SHPLibrary();
87
                shpLib.initialize();
88
                shpLib.postInitialize();
89

    
90
                JTSIndexLibrary jtsIndex = new JTSIndexLibrary();
91
                jtsIndex.initialize();
92
                jtsIndex.postInitialize();
93

    
94
                testSHPInitialized = true;
95

    
96

    
97
        }
98

    
99
        public void testSimpleIteration() {
100

    
101
                FeatureStore store = null;
102
                SHPStoreParameters shpParameters = null;
103

    
104
                try {
105
                        shpParameters = (SHPStoreParameters) dataManager
106
                                        .createStoreParameters(SHPStoreProvider.NAME);
107

    
108
                        //                        shpParameters.setFile(file_poly_valencia);
109
                        shpParameters.setFile(file_prueba);
110

    
111
                        store = (FeatureStore) dataManager.createStore(shpParameters);
112
                        FeatureSet set;
113
                        FeatureType type = store.getDefaultFeatureType();
114
                        set = store.getFeatureSet();
115

    
116
                        System.out.println("Num:" + set.getSize());
117
                        Iterator it = set.iterator();
118
                        Iterator ftIt;
119
                        FeatureAttributeDescriptor desc;
120

    
121
                        int i = 0;
122
                        Feature feature;
123
                        while (it.hasNext()) {
124
                                ftIt = type.iterator();
125

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

    
140

    
141
                        }
142

    
143
                        set.dispose();
144

    
145
                        store.dispose();
146

    
147

    
148
                } catch (DataException e3) {
149
                        e3.printStackTrace();
150
                        fail();
151
                        return;
152
                }
153

    
154

    
155
        }
156
        public void testEditing(Object x) {
157
                FeatureStore store = null;
158
                SHPStoreParameters shpParameters = null;
159

    
160
                try {
161
                        shpParameters = (SHPStoreParameters) dataManager
162
                                        .createStoreParameters(SHPStoreProvider.NAME);
163

    
164
                        shpParameters.setFile(file_poly_valencia);
165

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

    
176
                        int i = 0;
177
                        Feature feature;
178
                        while (it.hasNext()) {
179
                                ftIt = type.iterator();
180

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

    
195

    
196
                        }
197

    
198
                        set.dispose();
199
                        store.edit();
200
                        EditableFeature ef = store.createNewFeature();
201
                        store.insert(ef);
202
                        store.finishEditing();
203
                        System.err.println("Despu?s de la edici?n");
204
                        set = store.getFeatureSet();
205
                        System.out.println("Num:" + set.getSize());
206
                        it = set.iterator();
207

    
208
                        i = 0;
209
                        while (it.hasNext()) {
210
                                ftIt = type.iterator();
211

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

    
226

    
227
                        }
228

    
229
                        set.dispose();
230

    
231
                        store.dispose();
232

    
233

    
234
                } catch (DataException e3) {
235
                        e3.printStackTrace();
236
                        fail();
237
                        return;
238
                }
239
        }
240

    
241
        public void testExport(Object x) {
242
                DBFStoreParameters dbfParameters = null;
243

    
244
                try {
245
                        dbfParameters = (DBFStoreParameters) dataManager
246
                                        .createStoreParameters(DBFStoreProvider.NAME);
247

    
248
                        dbfParameters.setFile(file_prueba);
249

    
250
                        FeatureStore store = (FeatureStore) dataManager
251
                                        .createStore(dbfParameters);
252
                        FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) dataManager.createServerExplorerParameters(FilesystemServerExplorerParameters.DYNCLASS_NAME);
253
                        explorerParams.setRoot(file_prueba.getParent());
254

    
255
                        FilesystemServerExplorer explorer=(FilesystemServerExplorer) dataManager.createServerExplorer(explorerParams);
256

    
257
                        NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(file_prueba_destino);
258

    
259
                        store.export(explorer, newParams);
260

    
261
                        FeatureStore result = (FeatureStore)dataManager.createStore(newParams);
262

    
263
                        FeatureSet set = result.getFeatureSet();
264
                        FeatureSet originalSet=store.getFeatureSet();
265
                        assertEquals(set.getSize(), originalSet.getSize());
266

    
267
                        Iterator originalIter=originalSet.iterator();
268
                        Iterator iter = set.iterator();
269
                        while (iter.hasNext()) {
270
                                assertEquals(originalIter.next(),iter.next());
271
                        }
272

    
273
                        result.dispose();
274
                        explorer.remove(newParams);
275

    
276
                } catch (DataException e3) {
277
                        e3.printStackTrace();
278
                        fail();
279
                        return;
280
                }
281

    
282
        }
283

    
284
        /*
285
         * (non-Javadoc)
286
         *
287
         * @see
288
         * org.gvsig.fmap.dal.feature.BaseTestFeatureStore#getDefaultDataStoreParameters
289
         * ()
290
         */
291
        public DataStoreParameters getDefaultDataStoreParameters()
292
                        throws DataException {
293
                SHPStoreParameters shpParameters = (SHPStoreParameters) dataManager
294
                                .createStoreParameters(SHPStoreProvider.NAME);
295

    
296
                //                        shpParameters.setFile(file_poly_valencia);
297
                shpParameters.setFile(file_prueba);
298
                return shpParameters;
299
        }
300

    
301
        /*
302
         * (non-Javadoc)
303
         *
304
         * @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
305
         */
306
        public boolean hasExplorer() {
307
                return true;
308
        }
309

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

    
327
                return (NewFeatureStoreParameters) myExplorer
328
                                .getAddParameters(file_prueba_destino);
329
        }
330

    
331
        public boolean usesResources() {
332
                return true;
333
        }
334

    
335
}