Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap / src-test / org / gvsig / fmap / drivers / iterators / spatialindex / SpatialIndexTest.java @ 20984

History | View | Annotate | Download (7.45 KB)

1
package org.gvsig.fmap.drivers.iterators.spatialindex;
2

    
3
import java.awt.geom.Point2D;
4
import java.awt.geom.Rectangle2D;
5
import java.io.File;
6
import java.net.URL;
7
import java.util.List;
8

    
9
import junit.framework.TestCase;
10

    
11
import org.cresques.cts.IProjection;
12
import org.gvsig.fmap.core.geometries.IGeometry;
13
import org.gvsig.fmap.crs.CRSFactory;
14
import org.gvsig.fmap.drivers.exceptions.ExpansionFileReadException;
15
import org.gvsig.fmap.drivers.exceptions.VisitorException;
16
import org.gvsig.fmap.drivers.iterators.spatialindex.PersistentRTreeJsi;
17
import org.gvsig.fmap.drivers.iterators.spatialindex.QuadtreeGt2;
18
import org.gvsig.fmap.drivers.iterators.spatialindex.SpatialIndexException;
19
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
20
import org.gvsig.fmap.mapcontext.layers.FBitSet;
21
import org.gvsig.fmap.mapcontext.layers.FLayer;
22
import org.gvsig.fmap.mapcontext.layers.LayerFactory;
23
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
24

    
25
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
26

    
27
public class SpatialIndexTest extends TestCase {
28

    
29
        // TODO MOVER TODO LO ESTATICO A UNA CLASE AUXILIAR QUE NO SEA JUNIT
30

    
31
        String fwAndamiDriverPath = "../_fwAndami/gvSIG/extensiones/com.iver.cit.gvsig/drivers";
32

    
33
        File baseDataPath;
34

    
35
        File baseDriversPath;
36

    
37
        String SHP_DRIVER_NAME = "gvSIG shp driver";
38

    
39
        IProjection PROJECTION_DEFAULT = CRSFactory.getCRS("EPSG:23030");
40
        
41
        FLyrVect cantabria;
42
        FLyrVect edificaciones;
43

    
44
        public SpatialIndexTest(String name) {
45
                super(name);
46
        }
47

    
48
        protected void setUp() throws Exception {
49
                super.setUp();
50
                URL url = SpatialIndexTest.class.getResource("testdata");
51
                if (url == null)
52
                        throw new Exception(
53
                                        "No se encuentra el directorio con datos de prueba");
54

    
55
                baseDataPath = new File(url.getFile());
56
                if (!baseDataPath.exists())
57
                        throw new Exception(
58
                                        "No se encuentra el directorio con datos de prueba");
59

    
60
                baseDriversPath = new File(fwAndamiDriverPath);
61
                if (!baseDriversPath.exists())
62
                        throw new Exception("Can't find drivers path: "
63
                                        + fwAndamiDriverPath);
64

    
65
                LayerFactory.setDriversPath(baseDriversPath.getAbsolutePath());
66
                if (LayerFactory.getDM().getDriverNames().length < 1)
67
                        throw new Exception("Can't find drivers in path: "
68
                                        + fwAndamiDriverPath);
69
                
70
                cantabria = (FLyrVect) newLayer("Cantabria.shp", SHP_DRIVER_NAME);
71
                edificaciones = (FLyrVect) newLayer("Edificaciones.shp", SHP_DRIVER_NAME);
72
                
73
        }
74

    
75
        public FLayer newLayer(String fileName, String driverName)
76
                        throws LoadLayerException {
77
                File file = new File(baseDataPath, fileName);
78
                return LayerFactory.createLayer(fileName, driverName, file,
79
                                PROJECTION_DEFAULT);
80
        }
81

    
82
        protected void tearDown() throws Exception {
83
                super.tearDown();
84
                PROJECTION_DEFAULT = null;
85
        }
86

    
87
        public void testSpatialIndexFullExtent() throws ReadDriverException,
88
                        ExpansionFileReadException, VisitorException, LoadLayerException {
89
                
90
                Rectangle2D rect2D = cantabria.getFullExtent();
91
                FBitSet bitset = cantabria.queryByRect(rect2D);
92
                assertTrue(bitset.cardinality() != 0);
93

    
94
                double x = rect2D.getCenterX();
95
                double y = rect2D.getCenterY();
96
                rect2D = new Rectangle2D.Double(x, y, 100d, 100d);
97
                bitset = cantabria.queryByRect(rect2D);
98
                assertTrue(bitset.cardinality() != 0);
99
        }
100

    
101
        public void testPersistentRtreeJsi() throws LoadLayerException,
102
                        ReadDriverException, ExpansionFileReadException,
103
                        SpatialIndexException {
104
                try {
105
                        PersistentRTreeJsi rtree = new PersistentRTreeJsi(baseDataPath
106
                                        .getAbsolutePath()
107
                                        + "/Cantabria", true);
108
                        
109

    
110
                        int numShapes = cantabria.getSource().getShapeCount();
111
                        for (int i = 0; i < numShapes; i++) {
112
                                IGeometry g = cantabria.getSource().getShape(i);
113
                                if (g == null)
114
                                        System.out.println("geometria a null");
115
                                rtree.insert(g.getBounds2D(), i);
116
                        }
117
                        rtree.flush();
118

    
119
                        rtree = null;
120

    
121
                        rtree = new PersistentRTreeJsi(baseDataPath.getAbsolutePath()
122
                                        + "/Cantabria", false);
123

    
124
                        Rectangle2D rect2D = cantabria.getFullExtent();
125
                        List results = rtree.query(rect2D);
126
                        assertTrue(results.size() != 0);
127
                        
128
                } catch (Exception e) {
129
                        e.printStackTrace();
130
                }
131
        }
132
        
133
        public void testPerformanceRtreeJsiQuadtreeGt2() throws LoadLayerException, ExpansionFileReadException, SpatialIndexException, ReadDriverException {
134
                
135
                
136
                String fileName = baseDataPath.getAbsolutePath()
137
                                                        + "/Edificaciones.shp";
138
                long t0 = System.currentTimeMillis();
139
                QuadtreeGt2        spatialIndex = new QuadtreeGt2(fileName,
140
                                                                                                                "NM", 
141
                                                                                edificaciones.getSource().getFullExtent(),
142
                                                                                edificaciones.getSource().getShapeCount(), 
143
                                                                                true);
144
                int numShapes = edificaciones.getSource().getShapeCount();
145
                for (int i = 0; i < numShapes; i++) {
146
                        IGeometry g = edificaciones.getSource().getShape(i);
147
                        spatialIndex.insert(g.getBounds2D(), i);
148
                }
149
                spatialIndex.flush();
150
                long t1 = System.currentTimeMillis();
151
                System.out.println((t1-t0)+ " en generar Edificaciones.qix");
152
                
153
                t0 = System.currentTimeMillis();
154
                PersistentRTreeJsi rtree = new PersistentRTreeJsi(baseDataPath
155
                                .getAbsolutePath()
156
                                + "/Edificaciones", true);
157
                numShapes = edificaciones.getSource().getShapeCount();
158
                for (int i = 0; i < numShapes; i++) {
159
                        IGeometry g = edificaciones.getSource().getShape(i);
160
                        rtree.insert(g.getBounds2D(), i);
161
                }
162
                t1 = System.currentTimeMillis();
163
                System.out.println((t1-t0)+" en generar Edificaciones.rix");
164
                rtree.flush();
165
                
166
                t0 = System.currentTimeMillis();
167
                spatialIndex = new QuadtreeGt2(fileName,
168
                                "NM", 
169
                                edificaciones.getSource().getFullExtent(),
170
                                edificaciones.getSource().getShapeCount(), 
171
                                false);
172
                t1 = System.currentTimeMillis();
173
                System.out.println((t1-t0)+" en cargar Edificaciones.qix");
174

    
175
                t0 = System.currentTimeMillis();
176
                rtree = new PersistentRTreeJsi(baseDataPath
177
                                .getAbsolutePath()
178
                                + "/Edificaciones", false);
179
                t1 = System.currentTimeMillis();
180
                System.out.println((t1-t0)+" en cargar Edificaciones.rix");
181
                
182
                Rectangle2D rect2D = edificaciones.getFullExtent();
183
                
184
                t0 = System.currentTimeMillis();
185
                List results = rtree.query(rect2D);
186
                int nb1 = results.size();
187
                t1 = System.currentTimeMillis();
188
                System.out.println((t1-t0)+" en resolver query con RIX");
189
                System.out.println("encontrados "+nb1);
190
                t0 = System.currentTimeMillis();
191
                results = spatialIndex.query(rect2D);
192
                int nb2 = results.size();
193
                t1 = System.currentTimeMillis();
194
                System.out.println((t1-t0)+" en resolver query con QIX");
195
                System.out.println("encontrados "+nb2);
196
                
197
        }
198
        
199
        public void testNearestRectanglePoint() throws SpatialIndexException, ExpansionFileReadException, ReadDriverException{
200
                long t0 = System.currentTimeMillis();
201
                PersistentRTreeJsi rtree = new PersistentRTreeJsi(baseDataPath
202
                                .getAbsolutePath()
203
                                + "/Edificaciones", false);
204
                long t1 = System.currentTimeMillis();
205
                
206
                System.out.println((t1-t0)+" en cargar Edificaciones.rix");
207
                
208
                Rectangle2D edifFullExtent = edificaciones.getFullExtent();
209
                double xc = edifFullExtent.getCenterX();
210
                double yc = edifFullExtent.getCenterY();
211
                double width = 200;
212
                int numberOfNearest = 20;
213
                Point2D p = new Point2D.Double(xc, yc);
214
                Rectangle2D query = new Rectangle2D.Double(xc, yc, width, width);
215
                
216
                t0 = System.currentTimeMillis();
217
                List solution = rtree.findNNearest(numberOfNearest, p);
218
                t1 = System.currentTimeMillis();
219
                
220
                System.out.println((t1-t0)+" en obtener los 20 mas cercanos al punto");
221
                
222
                assert(solution.size() > 0);
223
                
224
                t0 = System.currentTimeMillis();
225
                solution = rtree.findNNearest(numberOfNearest, query);
226
                t1 = System.currentTimeMillis();
227
                System.out.println((t1-t0)+" en obtener los 20 mas cercanos al rectangulo");
228
                assert(solution.size() > 0);
229
                
230
        }
231

    
232
}