Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / featureiterators / PerformanceFeatureIteratorTest.java @ 12049

History | View | Annotate | Download (16.8 KB)

1
/*
2
 * Created on 28-may-2007
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: PerformanceFeatureIteratorTest.java 12049 2007-06-07 09:31:42Z azabala $
47
* $Log$
48
* Revision 1.3  2007-06-07 09:31:42  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.2  2007/05/30 20:12:41  azabala
52
* fastIteration = true optimized.
53
*
54
* Revision 1.1  2007/05/29 19:11:03  azabala
55
* *** empty log message ***
56
*
57
*
58
*/
59
package com.iver.cit.gvsig.fmap.featureiterators;
60

    
61
import java.awt.geom.Rectangle2D;
62

    
63
import junit.framework.TestCase;
64

    
65
import org.cresques.cts.ICoordTrans;
66

    
67
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
68
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
69
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
70
import com.iver.cit.gvsig.fmap.core.IFeature;
71
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
72
import com.iver.cit.gvsig.fmap.drivers.featureiterators.SpatialQueryFeatureIterator;
73
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
74
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
75

    
76

    
77
/**
78
 * Tests to probe feature iteration methods.
79
 * 
80
 * These test are not functional-test (performance).
81
 
82
 * @author azabala
83
 *
84
 */
85
public class PerformanceFeatureIteratorTest extends TestCase {
86
        
87
   static FLyrVect lyr;
88
   
89
        
90
        static{
91
                        try {
92
                                lyr = (FLyrVect) FeatureIteratorTest.newLayer("poly-valencia.shp", 
93
                                                                                FeatureIteratorTest.SHP_DRIVER_NAME);
94
                                lyr.setAvailable(true);
95
                        } catch (LoadLayerException e) {
96
                                // TODO Auto-generated catch block
97
                                e.printStackTrace();
98
                        }
99
        }
100
        
101
        
102
        public void test1() {
103
                try {
104
                        //pruebas de iteracion espacial
105
                        System.out.println("TEST 1: ESPACIAL CON FULL EXTENT Y REPROYECCI?N");
106
                        Rectangle2D rect = lyr.getFullExtent();
107
                        IFeature feature = null;
108
                        //fast iteration
109
                        long t0 = System.currentTimeMillis();
110
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
111
                        lyr.getSource().setSpatialIndex(null);
112
                        
113
                        //Sin indice espacial, rapida
114
                        //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
115
                        //de destino
116
                        ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.getCT(FeatureIteratorTest.newProjection);
117
                        rect = trans.convert(rect);
118
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect, 
119
                                                                                                                        null, 
120
                                                                                                                        FeatureIteratorTest.newProjection, 
121
                                                                                                                        true);
122
                        int numFeatures = 0;
123
                        while(iterator.hasNext()){
124
                                feature = iterator.next();
125
                                numFeatures++;
126
                        }
127
                        long t1 = System.currentTimeMillis();
128
                
129
                        //sin indice espacial, lenta
130
                        iterator = lyr.getSource().getFeatureIterator(rect, 
131
                                                                                                                        null, 
132
                                                                FeatureIteratorTest.newProjection, 
133
                                                                                                false);
134
                        int numFeatures2 = 0;
135
                        while(iterator.hasNext()){
136
                                feature = iterator.next();
137
                                numFeatures2++;
138
                        }
139
                        long t2 = System.currentTimeMillis();
140
                        
141
                        lyr.getSource().setSpatialIndex(spatialIndex);
142
                        long t3 = System.currentTimeMillis();
143
                        
144
                        //con indice espacial rapida
145
                        iterator = lyr.getSource().getFeatureIterator(rect, 
146
                                                                                                                        null, 
147
                                                                FeatureIteratorTest.newProjection, 
148
                                                                                                true);
149
                        int numFeatures3 = 0;
150
                        while(iterator.hasNext()){
151
                                feature = iterator.next();
152
                                numFeatures3++;
153
                        }
154
                        long t4 = System.currentTimeMillis();
155
                        //con indice espacial lenta
156
                        iterator = lyr.getSource().getFeatureIterator(rect, 
157
                                        null, 
158
                                        FeatureIteratorTest.newProjection, 
159
                                        false);
160
                        int numFeatures4 = 0;
161
                        while(iterator.hasNext()){
162
                        feature = iterator.next();
163
                        numFeatures4++;
164
                        }
165
                        long t5 = System.currentTimeMillis();
166
                        
167
                                                
168
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
169
                        System.out.println("Recuperados "+numFeatures);
170
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
171
                        System.out.println("Recuperados "+numFeatures3);
172
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
173
                        System.out.println("Recuperados "+numFeatures2);
174
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
175
                        System.out.println("Recuperados "+numFeatures4);
176
                        
177
                } catch (ReadDriverException e) {
178
                        // TODO Auto-generated catch block
179
                        e.printStackTrace();
180
                } catch (ExpansionFileReadException e) {
181
                        // TODO Auto-generated catch block
182
                        e.printStackTrace();
183
                }
184
        }
185

    
186

    
187
        
188
        //test to ask a feature over the limit (numfeatures) to low level shapefile driver
189
        //classes
190
//        public void test2(){
191
//                try {
192
//                        FLyrVect layer = (FLyrVect) FeatureIteratorTest.newLayer("poly-valencia.shp", FeatureIteratorTest.SHP_DRIVER_NAME);
193
//                        int numShapes = layer.getSource().getShapeCount();
194
//                        ReadableVectorial source = layer.getSource();
195
//                        for(int i = numShapes -1; i < (numShapes + 50); i++){
196
//                                source.getShape(i);
197
//                        }
198
//                        assertTrue(1 == 2);//si llega aqui, no pasa el test
199
//                } catch (LoadLayerException e) {
200
//                        // TODO Auto-generated catch block
201
//                        e.printStackTrace();
202
//                } catch (ReadDriverException e) {
203
//                        // TODO Auto-generated catch block
204
//                        e.printStackTrace();
205
//                } catch (ExpansionFileReadException e) {
206
//                        // TODO Auto-generated catch block
207
//                        e.printStackTrace();
208
//                }
209
//
210
//        }
211
        
212
        //test to compare fast iteration based in spatial index with precisse iteration
213
        //with a little filter area
214
        public void test3(){
215
                double xmin = 724000;
216
                double xmax = 725000;
217
                double ymin = 4373800;
218
                double ymax = 4374300;
219
                System.out.println("TEST 2: ESPACIAL CON RECTANGULO PEQUE?O Y REPROYECCI?N");
220
                Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, (xmax-xmin), (ymax-ymin));
221
                ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.
222
                                                        getCT(FeatureIteratorTest.newProjection);
223
                //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
224
                //de destino
225
                rect = trans.convert(rect);
226
                
227
                
228
                IFeature feature = null;
229
                //fast iteration
230
                try {
231
                        //fast iteration
232
                        long t0 = System.currentTimeMillis();
233
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
234
                        lyr.getSource().setSpatialIndex(null);
235
                        
236
                        //Sin indice espacial, rapida
237
                        
238
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect, 
239
                                                                                                                        null, 
240
                                                                                                                        FeatureIteratorTest.newProjection, 
241
                                                                                                                        true);
242
                        int numFeatures = 0;
243
                        while(iterator.hasNext()){
244
                                feature = iterator.next();
245
                                numFeatures++;
246
                        }
247
                        long t1 = System.currentTimeMillis();
248
                
249
                        //sin indice espacial, lenta
250
                        iterator = lyr.getSource().getFeatureIterator(rect, 
251
                                                                                                                        null, 
252
                                                                FeatureIteratorTest.newProjection, 
253
                                                                                                false);
254
                        int numFeatures2 = 0;
255
                        while(iterator.hasNext()){
256
                                feature = iterator.next();
257
                                numFeatures2++;
258
                        }
259
                        long t2 = System.currentTimeMillis();
260
                        
261
                        lyr.getSource().setSpatialIndex(spatialIndex);
262
                        long t3 = System.currentTimeMillis();
263
                        
264
                        //con indice espacial rapida
265
                        iterator = lyr.getSource().getFeatureIterator(rect, 
266
                                                                                                                        null, 
267
                                                                FeatureIteratorTest.newProjection, 
268
                                                                                                true);
269
                        int numFeatures3 = 0;
270
                        while(iterator.hasNext()){
271
                                feature = iterator.next();
272
                                numFeatures3++;
273
                        }
274
                        long t4 = System.currentTimeMillis();
275
                        //con indice espacial lenta
276
                        iterator = lyr.getSource().getFeatureIterator(rect, 
277
                                        null, 
278
                                        FeatureIteratorTest.newProjection, 
279
                                        false);
280
                        int numFeatures4 = 0;
281
                        while(iterator.hasNext()){
282
                        feature = iterator.next();
283
                        numFeatures4++;
284
                        }
285
                        long t5 = System.currentTimeMillis();
286
                        
287
                                                
288
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
289
                        System.out.println("Recuperados "+numFeatures);
290
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
291
                        System.out.println("Recuperados "+numFeatures3);
292
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
293
                        System.out.println("Recuperados "+numFeatures2);
294
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
295
                        System.out.println("Recuperados "+numFeatures4);
296
                        
297
                } catch (ExpansionFileReadException e) {
298
                        // TODO Auto-generated catch block
299
                        e.printStackTrace();
300
                } catch (ReadDriverException e) {
301
                        // TODO Auto-generated catch block
302
                        e.printStackTrace();
303
                }
304
                
305
        }
306
        
307
        
308
        //the same test as test1 but without reprojection
309
        public void test4() {
310
                try {
311
                        //pruebas de iteracion espacial
312
                        Rectangle2D rect = lyr.getFullExtent();
313
                        IFeature feature = null;
314
                        System.out.println("TEST 3: ESPACIAL CON FULL EXTENT SIN REPROYECCI?N");
315
                        //fast iteration
316
                        long t0 = System.currentTimeMillis();
317
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
318
                        lyr.getSource().setSpatialIndex(null);
319
                        
320
                        
321
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect, 
322
                                                                                                                        null, 
323
                                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT, 
324
                                                                                                                        true);
325
                        int numFeatures = 0;
326
                        while(iterator.hasNext()){
327
                                feature = iterator.next();
328
                                numFeatures++;
329
                        }
330
                        long t1 = System.currentTimeMillis();
331
                
332
                        //sin indice espacial, lenta
333
                        iterator = lyr.getSource().getFeatureIterator(rect, 
334
                                                                                                                        null, 
335
                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
336
                                                                                                false);
337
                        int numFeatures2 = 0;
338
                        while(iterator.hasNext()){
339
                                feature = iterator.next();
340
                                numFeatures2++;
341
                        }
342
                        long t2 = System.currentTimeMillis();
343
                        
344
                        lyr.getSource().setSpatialIndex(spatialIndex);
345
                        long t3 = System.currentTimeMillis();
346
                        
347
                        //con indice espacial rapida
348
                        iterator = lyr.getSource().getFeatureIterator(rect, 
349
                                                                                                                        null, 
350
                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
351
                                                                                                true);
352
                        int numFeatures3 = 0;
353
                        while(iterator.hasNext()){
354
                                feature = iterator.next();
355
                                numFeatures3++;
356
                        }
357
                        long t4 = System.currentTimeMillis();
358
                        //con indice espacial lenta
359
                        iterator = lyr.getSource().getFeatureIterator(rect, 
360
                                        null, 
361
                                        FeatureIteratorTest.PROJECTION_DEFAULT,
362
                                        false);
363
                        int numFeatures4 = 0;
364
                        while(iterator.hasNext()){
365
                        feature = iterator.next();
366
                        numFeatures4++;
367
                        }
368
                        long t5 = System.currentTimeMillis();
369
                        
370
                                                
371
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
372
                        System.out.println("Recuperados "+numFeatures);
373
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
374
                        System.out.println("Recuperados "+numFeatures3);
375
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
376
                        System.out.println("Recuperados "+numFeatures2);
377
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
378
                        System.out.println("Recuperados "+numFeatures4);
379
                        
380
                } catch (ReadDriverException e) {
381
                        // TODO Auto-generated catch block
382
                        e.printStackTrace();
383
                } catch (ExpansionFileReadException e) {
384
                        // TODO Auto-generated catch block
385
                        e.printStackTrace();
386
                }
387
        }
388
        
389
        //the same test as test3 without reprojection
390
        public void test5(){
391
                double xmin = 724000;
392
                double xmax = 725000;
393
                double ymin = 4373800;
394
                double ymax = 4374300;
395
                Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, (xmax-xmin), (ymax-ymin));
396
                System.out.println("TEST 4: ESPACIAL CON RECTANGULO PEQUE?O SIN REPROYECCI?N");
397
                IFeature feature = null;
398
                //fast iteration
399
                try {
400
                        //fast iteration
401
                        long t0 = System.currentTimeMillis();
402
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
403
                        lyr.getSource().setSpatialIndex(null);
404
                        
405
                        //Sin indice espacial, rapida
406
                        
407
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect, 
408
                                                                                                                        null, 
409
                                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
410
                                                                                                                        true);
411
                        int numFeatures = 0;
412
                        while(iterator.hasNext()){
413
                                feature = iterator.next();
414
                                numFeatures++;
415
                        }
416
                        long t1 = System.currentTimeMillis();
417
                
418
                        //sin indice espacial, lenta
419
                        iterator = lyr.getSource().getFeatureIterator(rect, 
420
                                                                                                                        null, 
421
                                                                                                                FeatureIteratorTest.PROJECTION_DEFAULT,
422
                                                                                                false);
423
                        int numFeatures2 = 0;
424
                        while(iterator.hasNext()){
425
                                feature = iterator.next();
426
                                numFeatures2++;
427
                        }
428
                        long t2 = System.currentTimeMillis();
429
                        
430
                        lyr.getSource().setSpatialIndex(spatialIndex);
431
                        long t3 = System.currentTimeMillis();
432
                        
433
                        //con indice espacial rapida
434
                        iterator = lyr.getSource().getFeatureIterator(rect, 
435
                                                                                                                        null, 
436
                                                                                                                FeatureIteratorTest.PROJECTION_DEFAULT,
437
                                                                                                true);
438
                        int numFeatures3 = 0;
439
                        while(iterator.hasNext()){
440
                                feature = iterator.next();
441
                                numFeatures3++;
442
                        }
443
                        long t4 = System.currentTimeMillis();
444
                        //con indice espacial lenta
445
                        iterator = lyr.getSource().getFeatureIterator(rect, 
446
                                        null, 
447
                                        FeatureIteratorTest.PROJECTION_DEFAULT,
448
                                        false);
449
                        int numFeatures4 = 0;
450
                        while(iterator.hasNext()){
451
                        feature = iterator.next();
452
                        numFeatures4++;
453
                        }
454
                        long t5 = System.currentTimeMillis();
455
                        
456
                                                
457
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
458
                        System.out.println("Recuperados "+numFeatures);
459
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
460
                        System.out.println("Recuperados "+numFeatures3);
461
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
462
                        System.out.println("Recuperados "+numFeatures2);
463
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
464
                        System.out.println("Recuperados "+numFeatures4);
465
                        
466
                } catch (ExpansionFileReadException e) {
467
                        // TODO Auto-generated catch block
468
                        e.printStackTrace();
469
                } catch (ReadDriverException e) {
470
                        // TODO Auto-generated catch block
471
                        e.printStackTrace();
472
                }
473
                
474
        }
475
        
476
        /** 
477
         Este test hay que refinarlo. Simplemente es un intento, mediante simulacion, de encontrar el valor ideal
478
         de rectangulo de consulta para discernir cuando una iteraci?n debe hacer uso de la caracteristica boundedshapes
479
         (leer rectangulos sin leer la geometria, a modo de 'indice espacial') y cuando no
480
        */
481
        public void test6(){
482
                double xmin = 724000;
483
                double width = 1000;
484
                double ymin = 4373800;
485
                double height = 500;
486
                System.out.println("TEST 5: BUSQUEDA DEL LIMITE OPTIMO ENTRE BOUNDEDSHAPES Y PRECISSE PARA ITERACIONES R?PIDAS");
487
                //fast iteration
488
                try {
489
                        //fast iteration
490
                        lyr.getSource().setSpatialIndex(null);
491
                        
492
                        
493
                        double BOUND_FACTOR = SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR;
494
                        double lyrWidth = lyr.getSource().getFullExtent().getWidth();
495
                        Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, width, height);
496
                        while(width <= lyrWidth){
497
                                
498
                                ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.
499
                                                                        getCT(FeatureIteratorTest.newProjection);
500
                                //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
501
                                //de destino
502
                                rect = trans.convert(rect);
503
//                                SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR = 4d;
504
                                BOUND_FACTOR = SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR;
505
                                while (BOUND_FACTOR >= 1){
506
                                        long t0 = System.currentTimeMillis();
507
                                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect, 
508
                                                                                                                                        null, 
509
                                                                                                                                        FeatureIteratorTest.newProjection, 
510
                                                                                                                                        true);
511
                                        while(iterator.hasNext()){
512
                                                iterator.next();
513
                                        }
514
                                        long t1 = System.currentTimeMillis();
515
                                        
516
                                        
517
                                        Rectangle2D driverExtent = lyr.getSource().getFullExtent();
518
                                        double areaExtent = rect.getWidth() * rect.getHeight();
519
                                        double areaFullExtent = driverExtent.getWidth() *
520
                                                                         driverExtent.getHeight();
521
                                        System.out.println("areaExtent="+areaExtent+", areaFullExtent="+areaFullExtent);
522
                                        System.out.println("full/BoundFactor="+(areaFullExtent / BOUND_FACTOR));
523
                                        System.out.println("BOUND_F="+BOUND_FACTOR+";time="+(t1-t0));
524
                                        BOUND_FACTOR /= 2d;
525
//                                        SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR = BOUND_FACTOR;
526
                                }//while
527
                                width *= 3;
528
                                height *= 3;
529
                                rect = new Rectangle2D.Double(xmin, ymin, width, height);
530
                        }//while
531
                        
532
                } catch (ExpansionFileReadException e) {
533
                        // TODO Auto-generated catch block
534
                        e.printStackTrace();
535
                } catch (ReadDriverException e) {
536
                        // TODO Auto-generated catch block
537
                        e.printStackTrace();
538
                }
539
        }
540
}
541
        
542
        
543
        
544
        
545

    
546