Statistics
| Revision:

root / trunk / libraries / libFMap / src-test / com / iver / cit / gvsig / fmap / featureiterators / PerformanceFeatureIteratorTest.java @ 13904

History | View | Annotate | Download (16.9 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 13904 2007-09-20 08:08:29Z jaume $
47
* $Log$
48
* Revision 1.6  2007-09-20 08:08:29  jaume
49
* ReadExpansionFileException removed from this context
50
*
51
* Revision 1.5  2007/06/29 13:07:01  jaume
52
* +PictureLineSymbol
53
*
54
* Revision 1.4  2007/06/07 10:20:38  azabala
55
* includes closeIterator
56
*
57
* Revision 1.3  2007/06/07 09:31:42  azabala
58
* *** empty log message ***
59
*
60
* Revision 1.2  2007/05/30 20:12:41  azabala
61
* fastIteration = true optimized.
62
*
63
* Revision 1.1  2007/05/29 19:11:03  azabala
64
* *** empty log message ***
65
*
66
*
67
*/
68
package com.iver.cit.gvsig.fmap.featureiterators;
69

    
70
import java.awt.geom.Rectangle2D;
71

    
72
import junit.framework.TestCase;
73

    
74
import org.cresques.cts.ICoordTrans;
75

    
76
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
77
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
78
import com.iver.cit.gvsig.exceptions.layers.LoadLayerException;
79
import com.iver.cit.gvsig.fmap.core.IFeature;
80
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
81
import com.iver.cit.gvsig.fmap.drivers.featureiterators.SpatialQueryFeatureIterator;
82
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
83
import com.iver.cit.gvsig.fmap.spatialindex.ISpatialIndex;
84

    
85

    
86
/**
87
 * Tests to probe feature iteration methods.
88
 *
89
 * These test are not functional-test (performance).
90

91
 * @author azabala
92
 *
93
 */
94
public class PerformanceFeatureIteratorTest extends TestCase {
95

    
96
   static FLyrVect lyr;
97

    
98

    
99
        static{
100
                        try {
101
                                lyr = (FLyrVect) FeatureIteratorTest.newLayer("ejesc.shp",
102
                                                                                FeatureIteratorTest.SHP_DRIVER_NAME);
103
                                lyr.setAvailable(true);
104
                        } catch (LoadLayerException e) {
105
                                // TODO Auto-generated catch block
106
                                e.printStackTrace();
107
                        }
108
        }
109

    
110
        public void test1() {
111
                try {
112
                        //pruebas de iteracion espacial
113
                        System.out.println("TEST 1: ESPACIAL CON FULL EXTENT Y REPROYECCI?N");
114
                        Rectangle2D rect = lyr.getFullExtent();
115
                        IFeature feature = null;
116
                        //fast iteration
117
                        long t0 = System.currentTimeMillis();
118
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
119
                        lyr.getSource().setSpatialIndex(null);
120

    
121
                        //Sin indice espacial, rapida
122
                        //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
123
                        //de destino
124
                        ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.getCT(FeatureIteratorTest.newProjection);
125
                        rect = trans.convert(rect);
126
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect,
127
                                                                                                                        null,
128
                                                                                                                        FeatureIteratorTest.newProjection,
129
                                                                                                                        true);
130
                        int numFeatures = 0;
131
                        while(iterator.hasNext()){
132
                                feature = iterator.next();
133
                                numFeatures++;
134
                        }
135
                        long t1 = System.currentTimeMillis();
136
                        iterator.closeIterator();
137
                        //sin indice espacial, lenta
138
                        iterator = lyr.getSource().getFeatureIterator(rect,
139
                                                                                                                        null,
140
                                                                FeatureIteratorTest.newProjection,
141
                                                                                                false);
142
                        int numFeatures2 = 0;
143
                        while(iterator.hasNext()){
144
                                feature = iterator.next();
145
                                numFeatures2++;
146
                        }
147
                        long t2 = System.currentTimeMillis();
148
                        iterator.closeIterator();
149
                        lyr.getSource().setSpatialIndex(spatialIndex);
150
                        long t3 = System.currentTimeMillis();
151

    
152
                        //con indice espacial rapida
153
                        iterator = lyr.getSource().getFeatureIterator(rect,
154
                                                                                                                        null,
155
                                                                FeatureIteratorTest.newProjection,
156
                                                                                                true);
157
                        int numFeatures3 = 0;
158
                        while(iterator.hasNext()){
159
                                feature = iterator.next();
160
                                numFeatures3++;
161
                        }
162
                        long t4 = System.currentTimeMillis();
163
                        iterator.closeIterator();
164
                        //con indice espacial lenta
165
                        iterator = lyr.getSource().getFeatureIterator(rect,
166
                                        null,
167
                                        FeatureIteratorTest.newProjection,
168
                                        false);
169
                        int numFeatures4 = 0;
170
                        while(iterator.hasNext()){
171
                        feature = iterator.next();
172
                        numFeatures4++;
173
                        }
174
                        long t5 = System.currentTimeMillis();
175
                        iterator.closeIterator();
176

    
177
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
178
                        System.out.println("Recuperados "+numFeatures);
179
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
180
                        System.out.println("Recuperados "+numFeatures3);
181
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
182
                        System.out.println("Recuperados "+numFeatures2);
183
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
184
                        System.out.println("Recuperados "+numFeatures4);
185

    
186
                } catch (ReadDriverException e) {
187
                        // TODO Auto-generated catch block
188
                        e.printStackTrace();
189
                } 
190
        }
191

    
192

    
193

    
194
        //test to ask a feature over the limit (numfeatures) to low level shapefile driver
195
        //classes
196
//        public void test2(){
197
//                try {
198
//                        FLyrVect layer = (FLyrVect) FeatureIteratorTest.newLayer("poly-valencia.shp", FeatureIteratorTest.SHP_DRIVER_NAME);
199
//                        int numShapes = layer.getSource().getShapeCount();
200
//                        ReadableVectorial source = layer.getSource();
201
//                        for(int i = numShapes -1; i < (numShapes + 50); i++){
202
//                                source.getShape(i);
203
//                        }
204
//                        assertTrue(1 == 2);//si llega aqui, no pasa el test
205
//                } catch (LoadLayerException e) {
206
//                        // TODO Auto-generated catch block
207
//                        e.printStackTrace();
208
//                } catch (ReadDriverException e) {
209
//                        // TODO Auto-generated catch block
210
//                        e.printStackTrace();
211
//                } catch (ExpansionFileReadException e) {
212
//                        // TODO Auto-generated catch block
213
//                        e.printStackTrace();
214
//                }
215
//
216
//        }
217

    
218
        //test to compare fast iteration based in spatial index with precisse iteration
219
        //with a little filter area
220
        public void test3(){
221
                double xmin = 724000;
222
                double xmax = 725000;
223
                double ymin = 4373800;
224
                double ymax = 4374300;
225
                System.out.println("TEST 2: ESPACIAL CON RECTANGULO PEQUE?O Y REPROYECCI?N");
226
                Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, (xmax-xmin), (ymax-ymin));
227
                ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.
228
                                                        getCT(FeatureIteratorTest.newProjection);
229
                //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
230
                //de destino
231
                rect = trans.convert(rect);
232

    
233

    
234
                IFeature feature = null;
235
                //fast iteration
236
                try {
237
                        //fast iteration
238
                        long t0 = System.currentTimeMillis();
239
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
240
                        lyr.getSource().setSpatialIndex(null);
241

    
242
                        //Sin indice espacial, rapida
243

    
244
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect,
245
                                                                                                                        null,
246
                                                                                                                        FeatureIteratorTest.newProjection,
247
                                                                                                                        true);
248
                        int numFeatures = 0;
249
                        while(iterator.hasNext()){
250
                                feature = iterator.next();
251
                                numFeatures++;
252
                        }
253
                        long t1 = System.currentTimeMillis();
254
                        iterator.closeIterator();
255
                        //sin indice espacial, lenta
256
                        iterator = lyr.getSource().getFeatureIterator(rect,
257
                                                                                                                        null,
258
                                                                FeatureIteratorTest.newProjection,
259
                                                                                                false);
260
                        int numFeatures2 = 0;
261
                        while(iterator.hasNext()){
262
                                feature = iterator.next();
263
                                numFeatures2++;
264
                        }
265
                        long t2 = System.currentTimeMillis();
266
                        iterator.closeIterator();
267
                        lyr.getSource().setSpatialIndex(spatialIndex);
268
                        long t3 = System.currentTimeMillis();
269

    
270
                        //con indice espacial rapida
271
                        iterator = lyr.getSource().getFeatureIterator(rect,
272
                                                                                                                        null,
273
                                                                FeatureIteratorTest.newProjection,
274
                                                                                                true);
275
                        int numFeatures3 = 0;
276
                        while(iterator.hasNext()){
277
                                feature = iterator.next();
278
                                numFeatures3++;
279
                        }
280
                        long t4 = System.currentTimeMillis();
281
                        iterator.closeIterator();
282
                        //con indice espacial lenta
283
                        iterator = lyr.getSource().getFeatureIterator(rect,
284
                                        null,
285
                                        FeatureIteratorTest.newProjection,
286
                                        false);
287
                        int numFeatures4 = 0;
288
                        while(iterator.hasNext()){
289
                        feature = iterator.next();
290
                        numFeatures4++;
291
                        }
292
                        long t5 = System.currentTimeMillis();
293
                        iterator.closeIterator();
294

    
295
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
296
                        System.out.println("Recuperados "+numFeatures);
297
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
298
                        System.out.println("Recuperados "+numFeatures3);
299
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
300
                        System.out.println("Recuperados "+numFeatures2);
301
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
302
                        System.out.println("Recuperados "+numFeatures4);
303

    
304
                } catch (ExpansionFileReadException e) {
305
                        // TODO Auto-generated catch block
306
                        e.printStackTrace();
307
                } catch (ReadDriverException e) {
308
                        // TODO Auto-generated catch block
309
                        e.printStackTrace();
310
                }
311

    
312
        }
313

    
314

    
315
        //the same test as test1 but without reprojection
316
        public void test4() {
317
                try {
318
                        //pruebas de iteracion espacial
319
                        Rectangle2D rect = lyr.getFullExtent();
320
                        IFeature feature = null;
321
                        System.out.println("TEST 3: ESPACIAL CON FULL EXTENT SIN REPROYECCI?N");
322
                        //fast iteration
323
                        long t0 = System.currentTimeMillis();
324
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
325
                        lyr.getSource().setSpatialIndex(null);
326

    
327

    
328
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect,
329
                                                                                                                        null,
330
                                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
331
                                                                                                                        true);
332
                        int numFeatures = 0;
333
                        while(iterator.hasNext()){
334
                                feature = iterator.next();
335
                                numFeatures++;
336
                        }
337
                        long t1 = System.currentTimeMillis();
338

    
339
                        //sin indice espacial, lenta
340
                        iterator = lyr.getSource().getFeatureIterator(rect,
341
                                                                                                                        null,
342
                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
343
                                                                                                false);
344
                        int numFeatures2 = 0;
345
                        while(iterator.hasNext()){
346
                                feature = iterator.next();
347
                                numFeatures2++;
348
                        }
349
                        long t2 = System.currentTimeMillis();
350

    
351
                        lyr.getSource().setSpatialIndex(spatialIndex);
352
                        long t3 = System.currentTimeMillis();
353

    
354
                        //con indice espacial rapida
355
                        iterator = lyr.getSource().getFeatureIterator(rect,
356
                                                                                                                        null,
357
                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
358
                                                                                                true);
359
                        int numFeatures3 = 0;
360
                        while(iterator.hasNext()){
361
                                feature = iterator.next();
362
                                numFeatures3++;
363
                        }
364
                        long t4 = System.currentTimeMillis();
365
                        //con indice espacial lenta
366
                        iterator = lyr.getSource().getFeatureIterator(rect,
367
                                        null,
368
                                        FeatureIteratorTest.PROJECTION_DEFAULT,
369
                                        false);
370
                        int numFeatures4 = 0;
371
                        while(iterator.hasNext()){
372
                        feature = iterator.next();
373
                        numFeatures4++;
374
                        }
375
                        long t5 = System.currentTimeMillis();
376

    
377

    
378
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
379
                        System.out.println("Recuperados "+numFeatures);
380
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
381
                        System.out.println("Recuperados "+numFeatures3);
382
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
383
                        System.out.println("Recuperados "+numFeatures2);
384
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
385
                        System.out.println("Recuperados "+numFeatures4);
386

    
387
                } catch (ReadDriverException e) {
388
                        // TODO Auto-generated catch block
389
                        e.printStackTrace();
390
                } 
391
        }
392

    
393
        //the same test as test3 without reprojection
394
        public void test5(){
395
                double xmin = 724000;
396
                double xmax = 725000;
397
                double ymin = 4373800;
398
                double ymax = 4374300;
399
                Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, (xmax-xmin), (ymax-ymin));
400
                System.out.println("TEST 4: ESPACIAL CON RECTANGULO PEQUE?O SIN REPROYECCI?N");
401
                IFeature feature = null;
402
                //fast iteration
403
                try {
404
                        //fast iteration
405
                        long t0 = System.currentTimeMillis();
406
                        ISpatialIndex spatialIndex = lyr.getSource().getSpatialIndex();
407
                        lyr.getSource().setSpatialIndex(null);
408

    
409
                        //Sin indice espacial, rapida
410

    
411
                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect,
412
                                                                                                                        null,
413
                                                                                                                        FeatureIteratorTest.PROJECTION_DEFAULT,
414
                                                                                                                        true);
415
                        int numFeatures = 0;
416
                        while(iterator.hasNext()){
417
                                feature = iterator.next();
418
                                numFeatures++;
419
                        }
420
                        long t1 = System.currentTimeMillis();
421

    
422
                        //sin indice espacial, lenta
423
                        iterator = lyr.getSource().getFeatureIterator(rect,
424
                                                                                                                        null,
425
                                                                                                                FeatureIteratorTest.PROJECTION_DEFAULT,
426
                                                                                                false);
427
                        int numFeatures2 = 0;
428
                        while(iterator.hasNext()){
429
                                feature = iterator.next();
430
                                numFeatures2++;
431
                        }
432
                        long t2 = System.currentTimeMillis();
433

    
434
                        lyr.getSource().setSpatialIndex(spatialIndex);
435
                        long t3 = System.currentTimeMillis();
436

    
437
                        //con indice espacial rapida
438
                        iterator = lyr.getSource().getFeatureIterator(rect,
439
                                                                                                                        null,
440
                                                                                                                FeatureIteratorTest.PROJECTION_DEFAULT,
441
                                                                                                true);
442
                        int numFeatures3 = 0;
443
                        while(iterator.hasNext()){
444
                                feature = iterator.next();
445
                                numFeatures3++;
446
                        }
447
                        long t4 = System.currentTimeMillis();
448
                        //con indice espacial lenta
449
                        iterator = lyr.getSource().getFeatureIterator(rect,
450
                                        null,
451
                                        FeatureIteratorTest.PROJECTION_DEFAULT,
452
                                        false);
453
                        int numFeatures4 = 0;
454
                        while(iterator.hasNext()){
455
                        feature = iterator.next();
456
                        numFeatures4++;
457
                        }
458
                        long t5 = System.currentTimeMillis();
459

    
460

    
461
                        System.out.println((t1-t0)+" en la iteracion rapida sin indice espacial");
462
                        System.out.println("Recuperados "+numFeatures);
463
                        System.out.println((t4-t3)+" en la iteracion rapida con indice espacial");
464
                        System.out.println("Recuperados "+numFeatures3);
465
                        System.out.println((t2-t1)+" en la iteracion lenta sin indice espacial");
466
                        System.out.println("Recuperados "+numFeatures2);
467
                        System.out.println((t5-t4)+" en la iteracion lenta con indice espacial");
468
                        System.out.println("Recuperados "+numFeatures4);
469

    
470
                } catch (ExpansionFileReadException e) {
471
                        // TODO Auto-generated catch block
472
                        e.printStackTrace();
473
                } catch (ReadDriverException e) {
474
                        // TODO Auto-generated catch block
475
                        e.printStackTrace();
476
                }
477

    
478
        }
479

    
480
        /**
481
         Este test hay que refinarlo. Simplemente es un intento, mediante simulacion, de encontrar el valor ideal
482
         de rectangulo de consulta para discernir cuando una iteraci?n debe hacer uso de la caracteristica boundedshapes
483
         (leer rectangulos sin leer la geometria, a modo de 'indice espacial') y cuando no
484
        */
485
        public void test6(){
486
                double xmin = 724000;
487
                double width = 1000;
488
                double ymin = 4373800;
489
                double height = 500;
490
                System.out.println("TEST 5: BUSQUEDA DEL LIMITE OPTIMO ENTRE BOUNDEDSHAPES Y PRECISSE PARA ITERACIONES R?PIDAS");
491
                //fast iteration
492
                try {
493
                        //fast iteration
494
                        lyr.getSource().setSpatialIndex(null);
495

    
496

    
497
                        double BOUND_FACTOR = SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR;
498
                        double lyrWidth = lyr.getSource().getFullExtent().getWidth();
499
                        Rectangle2D rect = new Rectangle2D.Double(xmin, ymin, width, height);
500
                        while(width <= lyrWidth){
501

    
502
                                ICoordTrans trans = FeatureIteratorTest.PROJECTION_DEFAULT.
503
                                                                        getCT(FeatureIteratorTest.newProjection);
504
                                //si pedimos reproyeccion, el rectangulo de consulta debe estar en la proyeccion
505
                                //de destino
506
                                rect = trans.convert(rect);
507
//                                SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR = 4d;
508
                                BOUND_FACTOR = SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR;
509
                                while (BOUND_FACTOR >= 1){
510
                                        long t0 = System.currentTimeMillis();
511
                                        IFeatureIterator iterator = lyr.getSource().getFeatureIterator(rect,
512
                                                                                                                                        null,
513
                                                                                                                                        FeatureIteratorTest.newProjection,
514
                                                                                                                                        true);
515
                                        while(iterator.hasNext()){
516
                                                iterator.next();
517
                                        }
518
                                        long t1 = System.currentTimeMillis();
519

    
520

    
521
                                        Rectangle2D driverExtent = lyr.getSource().getFullExtent();
522
                                        double areaExtent = rect.getWidth() * rect.getHeight();
523
                                        double areaFullExtent = driverExtent.getWidth() *
524
                                                                         driverExtent.getHeight();
525
                                        System.out.println("areaExtent="+areaExtent+", areaFullExtent="+areaFullExtent);
526
                                        System.out.println("full/BoundFactor="+(areaFullExtent / BOUND_FACTOR));
527
                                        System.out.println("BOUND_F="+BOUND_FACTOR+";time="+(t1-t0));
528
                                        BOUND_FACTOR /= 2d;
529
//                                        SpatialQueryFeatureIterator.BOUNDED_SHAPES_FACTOR = BOUND_FACTOR;
530
                                }//while
531
                                width *= 3;
532
                                height *= 3;
533
                                rect = new Rectangle2D.Double(xmin, ymin, width, height);
534
                        }//while
535

    
536
                } catch (ExpansionFileReadException e) {
537
                        // TODO Auto-generated catch block
538
                        e.printStackTrace();
539
                } catch (ReadDriverException e) {
540
                        // TODO Auto-generated catch block
541
                        e.printStackTrace();
542
                }
543
        }
544
}
545

    
546

    
547

    
548

    
549

    
550