root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src-test / org / gvsig / fmap / dal / store / shp / TestSHP.java @ 25789
History | View | Annotate | Download (7.67 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 junit.framework.TestCase; |
34 |
|
35 |
import org.gvsig.fmap.dal.DALFileLibrary; |
36 |
import org.gvsig.fmap.dal.DALLibrary; |
37 |
import org.gvsig.fmap.dal.DALLocator; |
38 |
import org.gvsig.fmap.dal.DataManager; |
39 |
import org.gvsig.fmap.dal.exception.DataException; |
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.serverexplorer.filesystem.FilesystemServerExplorer; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
49 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreParameters; |
50 |
import org.gvsig.fmap.dal.store.dbf.DBFStoreProvider; |
51 |
import org.gvsig.fmap.dal.store.dbf.TestDBF; |
52 |
|
53 |
public class TestSHP extends TestCase { |
54 |
|
55 |
public static final File file_prueba = new File(TestSHP.class.getResource( |
56 |
"data/prueba.shp").getFile());
|
57 |
public static final File file_prueba_destino= new File(TestDBF.class.getResource( |
58 |
"data").getFile()+"/pruebaTemp.shp"); |
59 |
public static final File file_pruebaNull = new File(TestSHP.class |
60 |
.getResource("data/pruebaNull.shp").getFile());
|
61 |
|
62 |
public static final File file_poly_valencia = new File(TestSHP.class |
63 |
.getResource("data/poly-valencia.shp").getFile());
|
64 |
|
65 |
private static DataManager dataManager = null; |
66 |
|
67 |
|
68 |
protected void setUp() throws Exception { |
69 |
super.setUp();
|
70 |
DALLibrary lib = new DALLibrary();
|
71 |
lib.initialize(); |
72 |
lib.postInitialize(); |
73 |
|
74 |
DALFileLibrary libFile = new DALFileLibrary();
|
75 |
libFile.initialize(); |
76 |
libFile.postInitialize(); |
77 |
|
78 |
SHPLibrary shpLib = new SHPLibrary();
|
79 |
shpLib.initialize(); |
80 |
shpLib.postInitialize(); |
81 |
|
82 |
dataManager = DALLocator.getDataManager(); |
83 |
} |
84 |
|
85 |
public void testSimpleIteration() { |
86 |
|
87 |
FeatureStore store = null;
|
88 |
SHPStoreParameters shpParameters = null;
|
89 |
|
90 |
try {
|
91 |
shpParameters = (SHPStoreParameters) dataManager |
92 |
.createStoreParameters(SHPStoreProvider.NAME); |
93 |
|
94 |
shpParameters.setFile(file_poly_valencia); |
95 |
|
96 |
store = (FeatureStore) dataManager.createStore(shpParameters); |
97 |
FeatureSet set; |
98 |
FeatureType type = store.getDefaultFeatureType(); |
99 |
set = store.getFeatureSet(); |
100 |
|
101 |
System.out.println("Num:" + set.getSize()); |
102 |
Iterator it = set.iterator();
|
103 |
Iterator ftIt;
|
104 |
FeatureAttributeDescriptor desc; |
105 |
|
106 |
int i = 0; |
107 |
Feature feature; |
108 |
while (it.hasNext()) {
|
109 |
ftIt = type.iterator(); |
110 |
|
111 |
feature = (Feature) it.next(); |
112 |
// while (ftIt.hasNext()) {
|
113 |
// desc = (FeatureAttributeDescriptor) ftIt.next();
|
114 |
// System.out.println(desc.getName() + ":"
|
115 |
// + feature.get(desc.getIndex()));
|
116 |
//
|
117 |
// }
|
118 |
// System.out.println(feature.get("NOMBRE"));
|
119 |
System.out.print(feature.getDefaultEnvelope() + "\t"); |
120 |
// System.out.print(feature.getDefaultGeometry() + "\t");
|
121 |
// System.out.println(feature.get("NOMBRE"));
|
122 |
System.out.println("row:" + i); |
123 |
i++; |
124 |
|
125 |
|
126 |
} |
127 |
|
128 |
set.dispose(); |
129 |
|
130 |
store.dispose(); |
131 |
|
132 |
|
133 |
} catch (DataException e3) {
|
134 |
e3.printStackTrace(); |
135 |
fail(); |
136 |
return;
|
137 |
} |
138 |
|
139 |
|
140 |
} |
141 |
public void testEditing(){ |
142 |
FeatureStore store = null;
|
143 |
SHPStoreParameters shpParameters = null;
|
144 |
|
145 |
try {
|
146 |
shpParameters = (SHPStoreParameters) dataManager |
147 |
.createStoreParameters(SHPStoreProvider.NAME); |
148 |
|
149 |
shpParameters.setFile(file_poly_valencia); |
150 |
|
151 |
store = (FeatureStore) dataManager.createStore(shpParameters); |
152 |
FeatureSet set; |
153 |
FeatureType type = store.getDefaultFeatureType(); |
154 |
System.err.println("Antes de la edici?n"); |
155 |
set = store.getFeatureSet(); |
156 |
System.out.println("Num:" + set.getSize()); |
157 |
Iterator it = set.iterator();
|
158 |
Iterator ftIt;
|
159 |
FeatureAttributeDescriptor desc; |
160 |
|
161 |
int i = 0; |
162 |
Feature feature; |
163 |
while (it.hasNext()) {
|
164 |
ftIt = type.iterator(); |
165 |
|
166 |
feature = (Feature) it.next(); |
167 |
// while (ftIt.hasNext()) {
|
168 |
// desc = (FeatureAttributeDescriptor) ftIt.next();
|
169 |
// System.out.println(desc.getName() + ":"
|
170 |
// + feature.get(desc.getIndex()));
|
171 |
//
|
172 |
// }
|
173 |
// System.out.println(feature.get("NOMBRE"));
|
174 |
System.out.print(feature.getDefaultEnvelope() + "\t"); |
175 |
// System.out.print(feature.getDefaultGeometry() + "\t");
|
176 |
// System.out.println(feature.get("NOMBRE"));
|
177 |
System.out.println("row:" + i); |
178 |
i++; |
179 |
|
180 |
|
181 |
} |
182 |
|
183 |
set.dispose(); |
184 |
store.edit(); |
185 |
EditableFeature ef = store.createNewFeature(); |
186 |
store.insert(ef); |
187 |
store.finishEditing(); |
188 |
System.err.println("Despu?s de la edici?n"); |
189 |
set = store.getFeatureSet(); |
190 |
System.out.println("Num:" + set.getSize()); |
191 |
it = set.iterator(); |
192 |
|
193 |
i = 0;
|
194 |
while (it.hasNext()) {
|
195 |
ftIt = type.iterator(); |
196 |
|
197 |
feature = (Feature) it.next(); |
198 |
// while (ftIt.hasNext()) {
|
199 |
// desc = (FeatureAttributeDescriptor) ftIt.next();
|
200 |
// System.out.println(desc.getName() + ":"
|
201 |
// + feature.get(desc.getIndex()));
|
202 |
//
|
203 |
// }
|
204 |
// System.out.println(feature.get("NOMBRE"));
|
205 |
System.out.print(feature.getDefaultEnvelope() + "\t"); |
206 |
// System.out.print(feature.getDefaultGeometry() + "\t");
|
207 |
// System.out.println(feature.get("NOMBRE"));
|
208 |
System.out.println("row:" + i); |
209 |
i++; |
210 |
|
211 |
|
212 |
} |
213 |
|
214 |
set.dispose(); |
215 |
|
216 |
store.dispose(); |
217 |
|
218 |
|
219 |
} catch (DataException e3) {
|
220 |
e3.printStackTrace(); |
221 |
fail(); |
222 |
return;
|
223 |
} |
224 |
} |
225 |
public void testExport() { |
226 |
DBFStoreParameters dbfParameters = null;
|
227 |
|
228 |
try {
|
229 |
dbfParameters = (DBFStoreParameters) dataManager |
230 |
.createStoreParameters(DBFStoreProvider.NAME); |
231 |
|
232 |
dbfParameters.setFile(file_prueba); |
233 |
|
234 |
FeatureStore store = (FeatureStore) dataManager |
235 |
.createStore(dbfParameters); |
236 |
FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) dataManager.createServerExplorerParameters(FilesystemServerExplorerParameters.DYNCLASS_NAME); |
237 |
explorerParams.setRoot(file_prueba.getParent()); |
238 |
|
239 |
FilesystemServerExplorer explorer=(FilesystemServerExplorer) dataManager.createServerExplorer(explorerParams); |
240 |
|
241 |
NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(file_prueba_destino); |
242 |
|
243 |
store.export(explorer, newParams); |
244 |
|
245 |
FeatureStore result = (FeatureStore)dataManager.createStore(newParams); |
246 |
|
247 |
FeatureSet set = result.getFeatureSet(); |
248 |
FeatureSet originalSet=store.getFeatureSet(); |
249 |
assertEquals(set.getSize(), originalSet.getSize()); |
250 |
|
251 |
Iterator originalIter=originalSet.iterator();
|
252 |
Iterator iter = set.iterator();
|
253 |
while (iter.hasNext()) {
|
254 |
assertEquals(originalIter.next(),iter.next()); |
255 |
} |
256 |
|
257 |
result.dispose(); |
258 |
explorer.remove(newParams); |
259 |
|
260 |
} catch (DataException e3) {
|
261 |
e3.printStackTrace(); |
262 |
fail(); |
263 |
return;
|
264 |
} |
265 |
|
266 |
} |
267 |
|
268 |
} |