Statistics
| Revision:

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

History | View | Annotate | Download (9.52 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.DataStoreParameters;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore;
36
import org.gvsig.fmap.dal.feature.DisposableIterator;
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.serverexplorer.filesystem.FilesystemServerExplorer;
45
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
46
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters;
47
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider;
48
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer;
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
                        + File.separatorChar + "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
                new DefaultLibrariesInitializer().fullInitialize();
78

    
79
//                DALFileLibrary libFile = new DALFileLibrary();
80
//                libFile.initialize();
81
//
82
//                DefaultGeometryLibrary defGeomLib = new DefaultGeometryLibrary();
83
//                defGeomLib.initialize();
84
//
85
//                ProjectionLibrary projLib = new ProjectionLibrary();
86
//                projLib.initialize();
87
//
88
//                CresquesCtsLibrary cresquesLib = new CresquesCtsLibrary();
89
//                cresquesLib.initialize();
90
//
91
//                SHPLibrary shpLib = new SHPLibrary();
92
//                shpLib.initialize();
93
//
94
//
95
//                JTSIndexLibrary jtsIndex = new JTSIndexLibrary();
96
//                jtsIndex.initialize();
97
//
98
//                defGeomLib.postInitialize();
99
//                projLib.postInitialize();
100
//                cresquesLib.postInitialize();
101
//                libFile.postInitialize();
102
//                shpLib.postInitialize();
103
//                jtsIndex.postInitialize();
104

    
105
                testSHPInitialized = true;
106

    
107

    
108
        }
109

    
110
        public void testSimpleIteration() throws Exception {
111

    
112
                FeatureStore store = null;
113
                SHPStoreParameters shpParameters = null;
114

    
115
                shpParameters = (SHPStoreParameters) dataManager
116
                                .createStoreParameters(SHPStoreProvider.NAME);
117

    
118
                // shpParameters.setFile(file_poly_valencia);
119
                shpParameters.setFile(file_prueba);
120

    
121
                shpParameters.setSRSID("EPSG:23030");
122

    
123
                store = (FeatureStore) dataManager.createStore(shpParameters);
124
                FeatureSet set;
125
                FeatureType type = store.getDefaultFeatureType();
126
                set = store.getFeatureSet();
127

    
128
                System.out.println("Num:" + set.getSize());
129
                DisposableIterator it = set.iterator();
130
                Iterator ftIt;
131
                FeatureAttributeDescriptor desc;
132

    
133
                int i = 0;
134
                Feature feature;
135
                while (it.hasNext()) {
136
                        ftIt = type.iterator();
137

    
138
                        feature = (Feature) it.next();
139
                        // while (ftIt.hasNext()) {
140
                        // desc = (FeatureAttributeDescriptor) ftIt.next();
141
                        // System.out.println(desc.getName() + ":"
142
                        // + feature.get(desc.getIndex()));
143
                        //
144
                        // }
145
                        // System.out.println(feature.get("NOMBRE"));
146
                        System.out.print(feature.getDefaultEnvelope() + "\t");
147
                        // System.out.print(feature.getDefaultGeometry() + "\t");
148
                        // System.out.println(feature.get("NOMBRE"));
149
                        System.out.println("row:" + i);
150
                        i++;
151

    
152

    
153
                }
154

    
155
                it.dispose();
156
                set.dispose();
157

    
158
                store.dispose();
159

    
160
        }
161

    
162
        public void testEditing(Object x) throws Exception {
163
                FeatureStore store = null;
164
                SHPStoreParameters shpParameters = null;
165

    
166
                shpParameters = (SHPStoreParameters) dataManager
167
                                .createStoreParameters(SHPStoreProvider.NAME);
168

    
169
                shpParameters.setFile(file_poly_valencia);
170

    
171
                store = (FeatureStore) dataManager.createStore(shpParameters);
172
                FeatureSet set;
173
                FeatureType type = store.getDefaultFeatureType();
174
                System.err.println("Antes de la edici?n");
175
                set = store.getFeatureSet();
176
                System.out.println("Num:" + set.getSize());
177
                DisposableIterator it = set.iterator();
178
                Iterator ftIt;
179
                FeatureAttributeDescriptor desc;
180

    
181
                int i = 0;
182
                Feature feature;
183
                while (it.hasNext()) {
184
                        ftIt = type.iterator();
185

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

    
200

    
201
                }
202

    
203
                it.dispose();
204
                set.dispose();
205
                store.edit();
206
                EditableFeature ef = store.createNewFeature();
207
                store.insert(ef);
208
                store.finishEditing();
209
                System.err.println("Despu?s de la edici?n");
210
                set = store.getFeatureSet();
211
                System.out.println("Num:" + set.getSize());
212
                it = set.iterator();
213

    
214
                i = 0;
215
                while (it.hasNext()) {
216
                        ftIt = type.iterator();
217

    
218
                        feature = (Feature) it.next();
219
                        // while (ftIt.hasNext()) {
220
                        // desc = (FeatureAttributeDescriptor) ftIt.next();
221
                        // System.out.println(desc.getName() + ":"
222
                        // + feature.get(desc.getIndex()));
223
                        //
224
                        // }
225
                        // System.out.println(feature.get("NOMBRE"));
226
                        System.out.print(feature.getDefaultEnvelope() + "\t");
227
                        // System.out.print(feature.getDefaultGeometry() + "\t");
228
                        // System.out.println(feature.get("NOMBRE"));
229
                        System.out.println("row:" + i);
230
                        i++;
231

    
232

    
233
                }
234

    
235
                it.dispose();
236
                set.dispose();
237

    
238
                store.dispose();
239

    
240
        }
241

    
242
        public void testExport(Object x) throws Exception {
243
                DBFStoreParameters dbfParameters = null;
244

    
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
253
                                .createServerExplorerParameters(FilesystemServerExplorerParameters.DYNCLASS_NAME);
254
                explorerParams.setRoot(file_prueba.getParent());
255

    
256
                FilesystemServerExplorer explorer = (FilesystemServerExplorer) dataManager
257
                                .createServerExplorer(explorerParams);
258

    
259
                NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer
260
                                .getAddParameters(file_prueba_destino);
261

    
262
                store.export(explorer, newParams);
263

    
264
                FeatureStore result = (FeatureStore) dataManager.createStore(newParams);
265

    
266
                FeatureSet set = result.getFeatureSet();
267
                FeatureSet originalSet = store.getFeatureSet();
268
                assertEquals(set.getSize(), originalSet.getSize());
269

    
270
                DisposableIterator originalIter = originalSet.iterator();
271
                DisposableIterator iter = set.iterator();
272
                while (iter.hasNext()) {
273
                        assertEquals(originalIter.next(), iter.next());
274
                }
275

    
276
                originalIter.dispose();
277
                iter.dispose();
278
                result.dispose();
279
                explorer.remove(newParams);
280

    
281
        }
282

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

    
295
                //                        shpParameters.setFile(file_poly_valencia);
296
                shpParameters.setFile(file_prueba);
297
                shpParameters.setSRSID("EPSG:23030");
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 Exception {
318
                SHPStoreParameters parameters = (SHPStoreParameters) this
319
                                .getDefaultDataStoreParameters();
320

    
321
                if (this.myExplorer == null) {
322
                        FeatureStore store = (FeatureStore) dataManager
323
                                        .createStore(parameters);
324
                        myExplorer = (FilesystemServerExplorer) store.getExplorer();
325
                        store.dispose();
326
                }
327

    
328
                SHPNewStoreParameters result = (SHPNewStoreParameters) myExplorer
329
                                .getAddParameters(file_prueba_destino);
330

    
331
                result.setSRS(parameters.getSRS());
332
                return result;
333
        }
334

    
335
        public boolean usesResources() {
336
                return true;
337
        }
338

    
339
        public boolean resourcesNotifyChanges() {
340
                return true;
341
        }
342

    
343
}