root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / shp / TestSHP.java @ 27723
History | View | Annotate | Download (9.35 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.cresques.ProjectionLibrary; |
34 |
import org.cresques.impl.CresquesCtsLibrary; |
35 |
import org.gvsig.fmap.dal.DALFileLibrary; |
36 |
import org.gvsig.fmap.dal.DataStoreParameters; |
37 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore; |
39 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
41 |
import org.gvsig.fmap.dal.feature.Feature; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
47 |
import org.gvsig.fmap.dal.index.spatial.jts.JTSIndexLibrary; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
50 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
51 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider; |
52 |
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary; |
53 |
|
54 |
public class TestSHP extends BaseTestEditableFeatureStore { |
55 |
|
56 |
protected boolean testSHPInitialized = false; |
57 |
|
58 |
|
59 |
private FilesystemServerExplorer myExplorer;
|
60 |
|
61 |
|
62 |
public static final File file_prueba = new File(TestSHP.class.getResource( |
63 |
"data/prueba.shp").getFile());
|
64 |
public static final File file_prueba_destino = new File(file_prueba |
65 |
.getParent() |
66 |
+ "/pruebaTemp.shp");
|
67 |
public static final File file_pruebaNull = new File(TestSHP.class |
68 |
.getResource("data/pruebaNull.shp").getFile());
|
69 |
|
70 |
public static final File file_poly_valencia = new File(TestSHP.class |
71 |
.getResource("data/poly-valencia.shp").getFile());
|
72 |
|
73 |
|
74 |
protected void setUp() throws Exception { |
75 |
super.setUp();
|
76 |
|
77 |
if (testSHPInitialized) {
|
78 |
return;
|
79 |
} |
80 |
|
81 |
DALFileLibrary libFile = new DALFileLibrary();
|
82 |
libFile.initialize(); |
83 |
|
84 |
DefaultGeometryLibrary defGeomLib = new DefaultGeometryLibrary();
|
85 |
defGeomLib.initialize(); |
86 |
|
87 |
ProjectionLibrary projLib = new ProjectionLibrary();
|
88 |
projLib.initialize(); |
89 |
|
90 |
CresquesCtsLibrary cresquesLib = new CresquesCtsLibrary();
|
91 |
cresquesLib.initialize(); |
92 |
|
93 |
SHPLibrary shpLib = new SHPLibrary();
|
94 |
shpLib.initialize(); |
95 |
|
96 |
|
97 |
JTSIndexLibrary jtsIndex = new JTSIndexLibrary();
|
98 |
jtsIndex.initialize(); |
99 |
|
100 |
defGeomLib.postInitialize(); |
101 |
projLib.postInitialize(); |
102 |
cresquesLib.postInitialize(); |
103 |
libFile.postInitialize(); |
104 |
shpLib.postInitialize(); |
105 |
jtsIndex.postInitialize(); |
106 |
|
107 |
testSHPInitialized = true;
|
108 |
|
109 |
|
110 |
} |
111 |
|
112 |
public void testSimpleIteration() throws Exception { |
113 |
|
114 |
FeatureStore store = null;
|
115 |
SHPStoreParameters shpParameters = null;
|
116 |
|
117 |
shpParameters = (SHPStoreParameters) dataManager |
118 |
.createStoreParameters(SHPStoreProvider.NAME); |
119 |
|
120 |
// shpParameters.setFile(file_poly_valencia);
|
121 |
shpParameters.setFile(file_prueba); |
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 |
return shpParameters;
|
298 |
} |
299 |
|
300 |
/*
|
301 |
* (non-Javadoc)
|
302 |
*
|
303 |
* @see org.gvsig.fmap.dal.feature.BaseTestFeatureStore#hasExplorer()
|
304 |
*/
|
305 |
public boolean hasExplorer() { |
306 |
return true; |
307 |
} |
308 |
|
309 |
/*
|
310 |
* (non-Javadoc)
|
311 |
*
|
312 |
* @seeorg.gvsig.fmap.dal.feature.BaseTestEditableFeatureStore#
|
313 |
* getDefaultNewDataStoreParameters()
|
314 |
*/
|
315 |
public NewFeatureStoreParameters getDefaultNewDataStoreParameters()
|
316 |
throws Exception { |
317 |
if (this.myExplorer == null) { |
318 |
SHPStoreParameters parameters = (SHPStoreParameters) this
|
319 |
.getDefaultDataStoreParameters(); |
320 |
FeatureStore store = (FeatureStore) dataManager |
321 |
.createStore(parameters); |
322 |
myExplorer = (FilesystemServerExplorer) store.getExplorer(); |
323 |
store.dispose(); |
324 |
} |
325 |
|
326 |
return (NewFeatureStoreParameters) myExplorer
|
327 |
.getAddParameters(file_prueba_destino); |
328 |
} |
329 |
|
330 |
public boolean usesResources() { |
331 |
return true; |
332 |
} |
333 |
|
334 |
} |