Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src-test / org / gvsig / fmap / data / index / SpatialIndexTest.java @ 23290

History | View | Annotate | Download (8.86 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 {{Company}}   {{Task}}
26
*/
27
 
28
package org.gvsig.fmap.data.index;
29

    
30
import java.awt.Rectangle;
31
import java.awt.geom.Rectangle2D;
32
import java.util.List;
33

    
34
import junit.framework.TestCase;
35

    
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Logger;
38
import org.apache.log4j.SimpleLayout;
39
import org.gvsig.fmap.data.DataManager;
40
import org.gvsig.fmap.data.feature.AttributeDescriptor;
41
import org.gvsig.fmap.data.feature.DefaultFeatureType;
42
import org.gvsig.fmap.data.feature.Feature;
43
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
44
import org.gvsig.fmap.data.feature.FeatureCollection;
45
import org.gvsig.fmap.data.feature.FeatureType;
46
import org.gvsig.fmap.data.feature.IsNotAttributeSettingException;
47
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex;
48
import org.gvsig.fmap.data.index.spatial.SpatialIndex;
49
import org.gvsig.fmap.data.index.spatial.SpatialIndexFactory;
50
import org.gvsig.fmap.data.index.spatial.SpatialIndexParameters;
51
import org.gvsig.fmap.data.index.spatial.SpatialQueryParameters;
52
import org.gvsig.fmap.data.index.spatial.gt2.QuadTreeGt2Factory;
53
import org.gvsig.fmap.data.index.spatial.jsi.RTreeJsiFactory;
54
import org.gvsig.fmap.data.index.spatial.jts.QuadTreeJtsFactory;
55
import org.gvsig.fmap.data.index.spatial.spatialindex.RTreeSptLibFactory;
56
import org.gvsig.fmap.geom.Geometry;
57
import org.gvsig.fmap.geom.primitive.Curve2D;
58
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61

    
62
public class SpatialIndexTest extends TestCase {
63
        
64
        private static Logger logger = Logger.getLogger("org.gvsig");
65

    
66
        static {
67
                logger.addAppender(new ConsoleAppender(new SimpleLayout()));
68
        }
69
        
70
        protected void setUp() throws Exception {
71
                QuadTreeJtsFactory f1 = new QuadTreeJtsFactory();
72
                QuadTreeGt2Factory f2 = new QuadTreeGt2Factory();
73
                RTreeJsiFactory f3 = new RTreeJsiFactory();
74
                RTreeSptLibFactory f4 = new RTreeSptLibFactory();
75
                super.setUp();
76
        }
77
        
78
        public void testSpatialIndex() {
79
                
80
                try {
81
                        SpatialIndexFactory factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.JTS_QUADTREE);                        
82
                        List l1 = tryFactory(factory, null);
83
                        
84
                        factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.SPATIALINDEX_RTREE);
85
                        List l2 = tryFactory(factory, null);
86
                        
87
                        factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.JSI_RTREE);
88
                        List l3 = tryFactory(factory, null);
89

    
90
                        SpatialIndexParameters p = new SpatialIndexParameters();
91
                        p.setFeatureCount(1000);                        
92
                        
93
                        factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.GT2_QUADTREE);
94
                        List l4 = tryFactory(factory, p);
95
                        
96
                } catch (Exception e) {
97
                        logger.error("Error, ", e);
98
                }
99
                
100
        }
101
        
102
        private List tryFactory(SpatialIndexFactory factory, SpatialIndexParameters p) throws IndexException {                
103
                SpatialIndex index = factory.createSpatialIndex(p);
104
                Envelope envelope = fillIndex(index);
105
                
106
                if (p != null)        p.setEnvelope(envelope);
107
                
108
                if (index instanceof PersistentSpatialIndex) {
109
                        logger.debug("*** Persistent Index.***");
110
                        PersistentSpatialIndex psi = (PersistentSpatialIndex) index; 
111
                        psi.flush();
112
                        assertTrue(psi.getFile().exists());
113
                        logger.debug(psi.getFile().getAbsolutePath());
114
                }
115
                
116

    
117
                
118
                Envelope env = new Curve2D(new GeneralPathX(new Rectangle(0,0,100,100))).getEnvelope();
119
                
120
                logger.debug("index.query(env)");
121
                
122
                SpatialQueryParameters sqp = new SpatialQueryParameters();
123
                sqp.setEnvelope(env);                
124
                List result = index.query(sqp); 
125
                logger.debug(result);
126
                return result;
127
        }
128
        
129
        private Envelope fillIndex(SpatialIndex index) {
130
                Envelope extent = new DefaultEnvelope(2);
131
                for (int i=0; i<1000; i++) {
132
                        Rectangle2D r = new Rectangle(i, -i, i + 10, i * 2 - i);
133
                        Envelope env = new Curve2D(new GeneralPathX(r)).getEnvelope();
134
                        extent.add(env);
135
                        // FIXME index.insert(env, i);
136
                        if(i%100 == 0) logger.debug(env.toString());
137
                }                
138
                logger.debug("index done.");
139
                return extent;
140
        }
141
        
142
        private FeatureCollection newFeatureCollection() {
143
                DataManager man = DataManager.getManager();
144
                return null;                
145
        }
146
        
147
        private FeatureType newFeatureType() {
148
                DefaultFeatureType fType = new DefaultFeatureType();
149

    
150
                try {
151
                        AttributeDescriptor descriptorID = (AttributeDescriptor) fType
152
                                        .createAttributeDescriptor();
153
                        descriptorID.loading();
154
                        descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
155
                        descriptorID.setName("ID");
156
                        descriptorID.setDefaultValue(new Integer(0));
157
                        descriptorID.stopLoading();
158
                        fType.add(descriptorID);
159

    
160
                        AttributeDescriptor descriptorShape = (AttributeDescriptor) fType
161
                                        .createAttributeDescriptor();
162
                        descriptorShape.loading();
163
                        descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
164
                        descriptorShape.setName("GEOMETRY");
165
                        descriptorShape.setDefaultValue(null);
166
                        descriptorShape.stopLoading();
167
                        fType.add(descriptorShape);
168
                        fType.setDefaultGeometry("GEOMETRY");
169

    
170
                        AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType
171
                                        .createAttributeDescriptor();
172
                        descriptorEntity.loading();
173
                        descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
174
                        descriptorEntity.setName("Entity");
175
                        descriptorEntity.setDefaultValue("Entity");
176
                        descriptorEntity.stopLoading();
177
                        fType.add(descriptorEntity);
178

    
179
                        AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType
180
                                        .createAttributeDescriptor();
181
                        descriptorLayer.loading();
182
                        descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
183
                        descriptorLayer.setName("Layer");
184
                        descriptorLayer.setDefaultValue("default");
185
                        descriptorLayer.stopLoading();
186
                        fType.add(descriptorLayer);
187

    
188
                        AttributeDescriptor descriptorColor = (AttributeDescriptor) fType
189
                                        .createAttributeDescriptor();
190
                        descriptorColor.loading();
191
                        descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
192
                        descriptorColor.setName("Color");
193
                        descriptorColor.setDefaultValue(new Integer(0));
194
                        descriptorColor.stopLoading();
195
                        fType.add(descriptorColor);
196

    
197
                        AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType
198
                                        .createAttributeDescriptor();
199
                        descriptorElevation.loading();
200
                        descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
201
                        descriptorElevation.setName("Elevation");
202
                        descriptorElevation.setDefaultValue(new Double(0));
203
                        descriptorElevation.stopLoading();
204
                        fType.add(descriptorElevation);
205

    
206
                        AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType
207
                                        .createAttributeDescriptor();
208
                        descriptorThickness.loading();
209
                        descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
210
                        descriptorThickness.setName("Thickness");
211
                        descriptorThickness.setDefaultValue(new Double(0));
212
                        descriptorThickness.stopLoading();
213
                        fType.add(descriptorThickness);
214

    
215
                        AttributeDescriptor descriptorText = (AttributeDescriptor) fType
216
                                        .createAttributeDescriptor();
217
                        descriptorText.loading();
218
                        descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
219
                        descriptorText.setName("Text");
220
                        descriptorText.setDefaultValue("");
221
                        descriptorText.stopLoading();
222
                        fType.add(descriptorText);
223

    
224
                        AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType
225
                                        .createAttributeDescriptor();
226
                        descriptorHeightText.loading();
227
                        descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
228
                        descriptorHeightText.setName("HeightText");
229
                        descriptorHeightText.setDefaultValue(new Float(10));
230
                        descriptorHeightText.stopLoading();
231
                        fType.add(descriptorHeightText);
232

    
233
                        AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType
234
                                        .createAttributeDescriptor();
235
                        descriptorRotationText.loading();
236
                        descriptorRotationText
237
                                        .setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
238
                        descriptorRotationText.setName("Rotation");
239
                        descriptorRotationText.setDefaultValue(new Double(10));
240
                        descriptorRotationText.stopLoading();
241
                        fType.add(descriptorRotationText);
242
                        fType.setDefaultGeometry("GEOMETRY");
243
                        fType.setGeometryTypes(new int[] { Geometry.TYPES.GEOMETRY });
244
                } catch (IsNotAttributeSettingException e) {
245
                        e.printStackTrace();
246
                }
247

    
248
                return fType;
249

    
250
        }        
251
        
252
}