root / branches / Mobile_Compatible_Hito_1 / libFMap / src-test / org / gvsig / data / datastores / vectorial / file / shp / SHPTest.java @ 21606
History | View | Annotate | Download (12.7 KB)
1 |
package org.gvsig.data.datastores.vectorial.file.shp; |
---|---|
2 |
|
3 |
import java.awt.Shape; |
4 |
import java.io.File; |
5 |
import java.text.DateFormat; |
6 |
import java.text.ParseException; |
7 |
import java.util.Date; |
8 |
import java.util.Iterator; |
9 |
|
10 |
import junit.framework.TestCase; |
11 |
|
12 |
import org.apache.log4j.Logger; |
13 |
import org.gvsig.data.DataManager; |
14 |
import org.gvsig.data.IDataCollection; |
15 |
import org.gvsig.data.IDataStoreParameters; |
16 |
import org.gvsig.data.datastores.vectorial.file.shp.utils.SHP; |
17 |
import org.gvsig.data.datastores.vectorial.file.shp_jni.JNISHPStore; |
18 |
import org.gvsig.data.datastores.vectorial.file.shp_mem.MemSHPStore; |
19 |
import org.gvsig.data.datastores.vectorial.file.shp_util.IGeometricDataStore; |
20 |
import org.gvsig.data.exception.CloseException; |
21 |
import org.gvsig.data.exception.InitializeException; |
22 |
import org.gvsig.data.exception.OpenException; |
23 |
import org.gvsig.data.exception.ReadException; |
24 |
import org.gvsig.data.vectorial.IFeature; |
25 |
import org.gvsig.data.vectorial.IFeatureType; |
26 |
|
27 |
import es.prodevelop.geodetic.utils.conversion.GeoUtils; |
28 |
import es.prodevelop.gvsig.mobile.fmap.core.FGeometry; |
29 |
import es.prodevelop.gvsig.mobile.fmap.core.FMultiPoint2D; |
30 |
import es.prodevelop.gvsig.mobile.fmap.core.FPoint2D; |
31 |
import es.prodevelop.gvsig.mobile.fmap.core.FPolygon2D; |
32 |
import es.prodevelop.gvsig.mobile.fmap.core.FPolyline2D; |
33 |
import es.prodevelop.gvsig.mobile.fmap.util.ResourceReader; |
34 |
import es.prodevelop.gvsig.mobile.fmap.util.Utils; |
35 |
import es.prodevelop.gvsig.mobile.fmap.util.graph2d.Graph2DUtilities; |
36 |
|
37 |
/**
|
38 |
* This class tests the three SHP drivers implemented for gvSIG Mobile.
|
39 |
* These drivers are compatible with the new data access model (june 2008)
|
40 |
* and will share a lot of code with gvsig Desktop.
|
41 |
*
|
42 |
* The right testdata folder is needed.
|
43 |
*
|
44 |
*
|
45 |
* @author jldominguez
|
46 |
*
|
47 |
*/
|
48 |
public class SHPTest extends TestCase { |
49 |
|
50 |
private static Logger logger = Logger.getLogger(SHPTest.class); |
51 |
|
52 |
/**
|
53 |
* Test IDs
|
54 |
*/
|
55 |
public static final int TEST_PUNTO = 0; |
56 |
public static final int TEST_MPUNTO = 1; |
57 |
public static final int TEST_LINEA = 2; |
58 |
public static final int TEST_POLIGONO = 3; |
59 |
public static final int TEST_NULL = 4; |
60 |
public static final int TEST_ATT = 5; |
61 |
|
62 |
private File filePunto, fileMPunto, fileLinea, filePoligono, fileNull, fileAtt; |
63 |
|
64 |
public static void main(String[] args) { |
65 |
junit.textui.TestRunner.run(SHPTest.class); |
66 |
} |
67 |
|
68 |
protected void setUp() throws Exception { |
69 |
super.setUp();
|
70 |
} |
71 |
|
72 |
public void testInitialize() { |
73 |
|
74 |
if (Utils.USING_PDA) {
|
75 |
filePunto = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "puntos_grande.shp"); |
76 |
fileMPunto = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "multipuntos_grande.shp"); |
77 |
fileLinea = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "lineas_grande.shp"); |
78 |
filePoligono = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "poligonos_grande.shp"); |
79 |
fileNull = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "pruebaNull.shp"); |
80 |
fileAtt = ResourceReader.getPdaTestFile(PerformanceTest.SD_FOLDER, "testdata", "atttest.shp"); |
81 |
} else {
|
82 |
filePunto = ResourceReader.getResourceFile("testdata", "puntos_grande.shp"); |
83 |
fileMPunto = ResourceReader.getResourceFile("testdata", "multipuntos_grande.shp"); |
84 |
fileLinea = ResourceReader.getResourceFile("testdata", "lineas_grande.shp"); |
85 |
filePoligono = ResourceReader.getResourceFile("testdata", "poligonos_grande.shp"); |
86 |
fileNull = ResourceReader.getResourceFile("testdata", "pruebaNull.shp"); |
87 |
fileAtt = ResourceReader.getResourceFile("testdata", "atttest.shp"); |
88 |
} |
89 |
|
90 |
new SHPStore();
|
91 |
doTest(SHPStore.DATASTORE_NAME); |
92 |
new MemSHPStore();
|
93 |
doTest(MemSHPStore.DATASTORE_NAME); |
94 |
new JNISHPStore();
|
95 |
doTest(JNISHPStore.DATASTORE_NAME); |
96 |
} |
97 |
|
98 |
/**
|
99 |
* Performs a test with a given data sorce name
|
100 |
*
|
101 |
* @see SHPStore
|
102 |
* @see MemSHPStore
|
103 |
* @see JNISHPStore
|
104 |
*
|
105 |
* @param ds_name
|
106 |
*/
|
107 |
public void doTest(String ds_name) { |
108 |
|
109 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + filePunto.getName() + "..."); |
110 |
shp(filePunto, TEST_PUNTO, ds_name); |
111 |
|
112 |
if (ds_name.compareTo(JNISHPStore.DATASTORE_NAME) != 0) { |
113 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + fileMPunto.getName() + "..."); |
114 |
shp(fileMPunto, TEST_MPUNTO, ds_name); |
115 |
} |
116 |
|
117 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + fileLinea.getName() + "..."); |
118 |
shp(fileLinea, TEST_LINEA, ds_name); |
119 |
|
120 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + filePoligono.getName() + "..."); |
121 |
shp(filePoligono, TEST_POLIGONO, ds_name); |
122 |
|
123 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + fileNull.getName() + "..."); |
124 |
shp(fileNull, TEST_NULL, ds_name); |
125 |
|
126 |
logger.debug("DATASOURCE: " + ds_name + ", FILE = " + fileAtt.getName() + "..."); |
127 |
shp(fileAtt, TEST_ATT, ds_name); |
128 |
// shp(fileGeometryNull);
|
129 |
} |
130 |
|
131 |
private void shp(File file, int fileid, String datastore_name) { |
132 |
DataManager manager = DataManager.getManager(); |
133 |
IGeometricDataStore store = null;
|
134 |
SHPStoreParameters shpParameters=null;
|
135 |
shpParameters =(SHPStoreParameters) manager.createDataStoreParameters(datastore_name); |
136 |
shpParameters.setFile(file); |
137 |
shpParameters.setDBFFile(SHP.getDbfFile(file)); |
138 |
shpParameters.setSHXFile(SHP.getShxFile(file)); |
139 |
|
140 |
try {
|
141 |
store = (IGeometricDataStore) |
142 |
manager.createDataStore((IDataStoreParameters)shpParameters); |
143 |
} catch (InitializeException e) {
|
144 |
// TODO Auto-generated catch block
|
145 |
e.printStackTrace(); |
146 |
} |
147 |
try {
|
148 |
store.open(); |
149 |
} catch (OpenException e2) {
|
150 |
// TODO Auto-generated catch block
|
151 |
e2.printStackTrace(); |
152 |
} |
153 |
|
154 |
IFeatureType ft = store.getDefaultFeatureType(); |
155 |
IDataCollection dataCollection = null;
|
156 |
try {
|
157 |
dataCollection = store.getDataCollection(ft,null,null); |
158 |
} catch (ReadException e1) {
|
159 |
e1.printStackTrace(); |
160 |
} |
161 |
Iterator iter = dataCollection.iterator();
|
162 |
int feature_count = 0; |
163 |
|
164 |
while (iter.hasNext()) {
|
165 |
IFeature feature = (IFeature)iter.next(); |
166 |
|
167 |
|
168 |
boolean simple = (datastore_name.compareTo(JNISHPStore.DATASTORE_NAME) == 0); |
169 |
check(feature, ft, fileid, simple); |
170 |
feature_count++; |
171 |
} |
172 |
iter=null;
|
173 |
dataCollection.dispose(); |
174 |
|
175 |
try {
|
176 |
store.close(); |
177 |
store.dispose(); |
178 |
} catch (CloseException e) {
|
179 |
// TODO Auto-generated catch block
|
180 |
e.printStackTrace(); |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
private void check(IFeature feature, IFeatureType att_desc, int fileid, boolean simpletypes) { |
186 |
|
187 |
double expected = 0; |
188 |
|
189 |
switch (fileid) {
|
190 |
|
191 |
case TEST_PUNTO:
|
192 |
expected = feature.getDouble("X");
|
193 |
FPoint2D p = (FPoint2D) ((FGeometry) feature.getDefaultGeometry()).getInternalShape(); |
194 |
checkPoint(feature, p, expected, simpletypes); |
195 |
break;
|
196 |
case TEST_MPUNTO:
|
197 |
checkMPoint(feature, simpletypes); |
198 |
break;
|
199 |
case TEST_LINEA:
|
200 |
expected = feature.getDouble("LEN");
|
201 |
FPolyline2D pl = (FPolyline2D) ((FGeometry) feature.getDefaultGeometry()).getInternalShape(); |
202 |
checkLineLength(feature, pl, expected, simpletypes); |
203 |
break;
|
204 |
case TEST_POLIGONO:
|
205 |
expected = feature.getDouble("AREA");
|
206 |
FPolygon2D pg = (FPolygon2D) ((FGeometry) feature.getDefaultGeometry()).getInternalShape(); |
207 |
checkPolygonArea(feature, pg, expected, simpletypes); |
208 |
break;
|
209 |
case TEST_NULL:
|
210 |
checkNullGeometry(feature); |
211 |
break;
|
212 |
case TEST_ATT:
|
213 |
checkAttributes(feature, simpletypes); |
214 |
break;
|
215 |
} |
216 |
} |
217 |
|
218 |
private void checkAttributes(IFeature feat, boolean simpl) { |
219 |
|
220 |
if (! simpl) {
|
221 |
Date d = feat.getDate("ATT_DATE"); |
222 |
String str = d.toString().toLowerCase();
|
223 |
boolean boo =
|
224 |
((str.indexOf("9999") != -1) |
225 |
&& ( |
226 |
(str.indexOf("12") != -1) |
227 |
|| (str.indexOf("dec") != -1) |
228 |
|| (str.indexOf("dic") != -1)) |
229 |
&& (str.indexOf("31") != -1)); |
230 |
assertTrue("Found a date different to 9999 12 31", boo);
|
231 |
|
232 |
} |
233 |
|
234 |
String str = feat.getString("ATT_STRING"); |
235 |
assertEquals(str, "DFGDFGDFG");
|
236 |
|
237 |
double real = feat.getDouble("ATT_REAL"); |
238 |
boolean boo = ((real == 4.4444) || (real == 4)); |
239 |
assertTrue("REAL = " + real + ", EXP = 4, 4.4444", boo); |
240 |
|
241 |
boolean bool = feat.getBoolean("ATT_BOOLEA"); |
242 |
assertTrue("EXP = FALSE, RET = " + (!bool), !bool);
|
243 |
|
244 |
if (simpl) {
|
245 |
double d = feat.getDouble("ATT_INTEGE"); |
246 |
assertTrue("REAL = " + d + ", EXP = 44444", d == 44444.0); |
247 |
} else {
|
248 |
int integ = feat.getInt("ATT_INTEGE"); |
249 |
assertTrue("REAL = " + integ + ", EXP = 44444", integ == 44444); |
250 |
} |
251 |
} |
252 |
|
253 |
private void checkPolygonArea(IFeature feature, FPolygon2D poly, double expected, boolean simpl) { |
254 |
|
255 |
int id = 0; |
256 |
if (simpl) {
|
257 |
id = (int) Math.round(feature.getDouble("GUIA_ID")); |
258 |
} else {
|
259 |
id = feature.getInt("GUIA_ID");
|
260 |
} |
261 |
|
262 |
double ar = Graph2DUtilities.getPathIteratorArea(poly.getPathIterator(null)); |
263 |
assertTrue("AREA = " + ar + ", EXP = " + expected, acceptablySimilar(expected, ar, 0.1)); |
264 |
|
265 |
switch (id) {
|
266 |
case 174996: |
267 |
assertTrue("AREA = " + ar + ", EXP = " + 357.7, acceptablySimilar(ar, 357.7, 0.1)); |
268 |
break;
|
269 |
case 569303133: |
270 |
assertTrue("AREA = " + ar + ", EXP = " + 409.46, acceptablySimilar(ar, 409.46, 0.1)); |
271 |
break;
|
272 |
case 1229222137: |
273 |
assertTrue("AREA = " + ar + ", EXP = " + 49.285, acceptablySimilar(ar, 49.285, 0.1)); |
274 |
break;
|
275 |
case 1795370359: |
276 |
assertTrue("AREA = " + ar + ", EXP = " + 98.165, acceptablySimilar(ar, 98.165, 0.1)); |
277 |
break;
|
278 |
} |
279 |
} |
280 |
|
281 |
private void checkLineLength(IFeature feature, FPolyline2D pl, double expected, boolean simpl) { |
282 |
int color = 0; |
283 |
int eleva = 0; |
284 |
if (simpl) {
|
285 |
color = (int) Math.round(feature.getDouble("COLOR")); |
286 |
eleva = (int) Math.round(feature.getDouble("ELEVACION")); |
287 |
} else {
|
288 |
color = feature.getInt("COLOR");
|
289 |
eleva = feature.getInt("ELEVACION");
|
290 |
} |
291 |
|
292 |
|
293 |
double len = GeoUtils.getIteratorLengthAndLast(pl.getPathIterator(null))[0]; |
294 |
assertTrue("Not good: " + len + " and " + expected, acceptablySimilar(expected, len, 0.05)); |
295 |
|
296 |
switch (color) {
|
297 |
case 888830465: |
298 |
assertEquals(eleva, -1519669334);
|
299 |
break;
|
300 |
case 135691458: |
301 |
assertEquals(eleva, -1594772521);
|
302 |
break;
|
303 |
case 631009463: |
304 |
assertEquals(eleva, -409958687);
|
305 |
break;
|
306 |
case 601164227: |
307 |
assertEquals(eleva, -1632831996);
|
308 |
break;
|
309 |
case 1766834083: |
310 |
assertEquals(eleva, -671796992);
|
311 |
break;
|
312 |
} |
313 |
} |
314 |
|
315 |
private void checkMPoint(IFeature feature, boolean simpl) { |
316 |
|
317 |
int tipocont = 0; |
318 |
if (simpl) {
|
319 |
tipocont = (int) Math.round(feature.getDouble("TIPOCONT")); |
320 |
} else {
|
321 |
tipocont = feature.getInt("GUIA_ID");
|
322 |
} |
323 |
|
324 |
String nomb = feature.getString("FIRST_NOMB"); |
325 |
FMultiPoint2D mp = (FMultiPoint2D) feature.getDefaultGeometry(); |
326 |
|
327 |
switch (tipocont) {
|
328 |
case 116: |
329 |
assertEquals(nomb, "MARIN (EL)");
|
330 |
assertTrue("Checking presence of coordinate X: ", oneOfXInMPoint(mp, 272463.83)); |
331 |
break;
|
332 |
case 118: |
333 |
assertEquals(nomb, "SALAMANCA");
|
334 |
assertTrue("Checking presence of coordinate X: ", oneOfXInMPoint(mp, 276024.43)); |
335 |
break;
|
336 |
case 15035: |
337 |
assertEquals(nomb, "ORBISO");
|
338 |
assertTrue("Checking presence of coordinate X: ", oneOfXInMPoint(mp, 554478.48)); |
339 |
break;
|
340 |
case 19586: |
341 |
assertEquals(nomb, "VALDEZORRAS");
|
342 |
assertTrue("Checking presence of coordinate X: ", oneOfXInMPoint(mp, 241343.25)); |
343 |
break;
|
344 |
case 30000: |
345 |
assertEquals(nomb, "ORZOLA");
|
346 |
assertTrue("Checking presence of coordinate X: ", oneOfXInMPoint(mp, -619386.36)); |
347 |
break;
|
348 |
} |
349 |
} |
350 |
|
351 |
private boolean oneOfXInMPoint(Shape pointorpoints, double d) { |
352 |
|
353 |
if (pointorpoints instanceof FMultiPoint2D) { |
354 |
FMultiPoint2D mp = (FMultiPoint2D) pointorpoints; |
355 |
int np = mp.getNumPoints();
|
356 |
for (int i=0; i<np; i++) { |
357 |
double itemx = mp.getPoint(i).getX();
|
358 |
if (acceptablySimilar(d, itemx, 0.01)) return true; |
359 |
} |
360 |
return false; |
361 |
} else {
|
362 |
FPoint2D p = (FPoint2D) pointorpoints; |
363 |
return acceptablySimilar(d, p.getX(), 0.01); |
364 |
} |
365 |
} |
366 |
|
367 |
private void checkNullGeometry(IFeature feature) { |
368 |
Object obj = feature.getDefaultGeometry();
|
369 |
assertNotNull("Checking not NULL geometry: ", obj);
|
370 |
} |
371 |
|
372 |
|
373 |
private void checkPoint(IFeature feature, FPoint2D point, double expected, boolean simpl) { |
374 |
int id = 0; |
375 |
|
376 |
if (simpl) {
|
377 |
id = (int) Math.round(feature.getDouble("NUMEROENT")); |
378 |
} else {
|
379 |
id = feature.getInt("NUMEROENT");
|
380 |
} |
381 |
|
382 |
String nomb = feature.getString("NOMBRE"); |
383 |
assertTrue(acceptablySimilar(expected, point.getX(), 0.01));
|
384 |
|
385 |
switch (id) {
|
386 |
case 58: |
387 |
assertEquals(nomb, "MARIN (EL)");
|
388 |
break;
|
389 |
case 59: |
390 |
assertEquals(nomb, "SALAMANCA");
|
391 |
break;
|
392 |
case 2825: |
393 |
assertEquals(nomb, "NAVAHONDILLA");
|
394 |
break;
|
395 |
case 6474: |
396 |
assertEquals(nomb, "CENTENERA");
|
397 |
break;
|
398 |
case 27761: |
399 |
assertEquals(nomb, "EVAN DE ARRIBA");
|
400 |
break;
|
401 |
} |
402 |
} |
403 |
|
404 |
|
405 |
private boolean acceptablySimilar(double expec, double area, double tol) { |
406 |
double diff = Math.abs(expec - area); |
407 |
return ((diff / Math.abs((expec + area) / 2)) < tol); |
408 |
} |
409 |
|
410 |
|
411 |
|
412 |
// public void test_Resources(){
|
413 |
//
|
414 |
// DataManager manager = DataManager.getManager();
|
415 |
// Register.selfRegister();
|
416 |
//
|
417 |
// SHPStoreParameters shpParameters;
|
418 |
//
|
419 |
// shpParameters=(SHPStoreParameters)manager.createDataStoreParameters(SHPStore.DATASTORE_NAME);
|
420 |
// shpParameters.setFile(fileNormal);
|
421 |
//
|
422 |
//
|
423 |
// DataStoreTest.doFileResourceTest(shpParameters);
|
424 |
// }
|
425 |
|
426 |
} |