Revision 24033 branches/v2_0_0_prep/libraries/libFMap_spatialindex/src-test/org/gvsig/fmap/data/index/IndexTest.java

View differences:

IndexTest.java
27 27
 
28 28
package org.gvsig.fmap.data.index;
29 29

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

  
34 30
import junit.framework.TestCase;
35 31

  
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Logger;
38
import org.apache.log4j.SimpleLayout;
39
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex;
40
import org.gvsig.fmap.data.index.spatial.SpatialIndex;
41
import org.gvsig.fmap.data.index.spatial.SpatialQueryParameters;
42
import org.gvsig.fmap.data.index.spatial.gt2.QuadTreeGt2Factory;
43
import org.gvsig.fmap.data.index.spatial.jsi.RTreeJsiFactory;
44
import org.gvsig.fmap.data.index.spatial.jts.QuadTreeJtsFactory;
45
import org.gvsig.fmap.data.index.spatial.spatialindex.RTreeSptLibFactory;
46
import org.gvsig.fmap.geom.primitive.Curve2D;
47
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
48
import org.gvsig.fmap.geom.primitive.Envelope;
49
import org.gvsig.fmap.geom.primitive.GeneralPathX;
50

  
51 32
/**
52 33
 * TODO Update these tests according to new design
53 34
 */
54 35
public class IndexTest extends TestCase {
55
	
56
	private static Logger logger = Logger.getLogger("org.gvsig");
57

  
58
	static {
59
		logger.addAppender(new ConsoleAppender(new SimpleLayout()));
60
	}
61
	
36
		
62 37
	protected void setUp() throws Exception {
63
		QuadTreeJtsFactory f1 = new QuadTreeJtsFactory();
64
		QuadTreeGt2Factory f2 = new QuadTreeGt2Factory();
65
		RTreeJsiFactory f3 = new RTreeJsiFactory();
66
		RTreeSptLibFactory f4 = new RTreeSptLibFactory();
67 38
		super.setUp();
68 39
	}
69
	
70
	public void testSpatialIndex() {
71
		
72
		try {
73
			IndexFactory factory = IndexFactory.getFactory(IndexFactory.Factories.GEOMETRY.JTS_QUADTREE);			
74
			List l1 = tryFactory(factory, null);
75
			
76
			factory = IndexFactory.getFactory(IndexFactory.Factories.GEOMETRY.SPATIALINDEX_RTREE);
77
			List l2 = tryFactory(factory, null);
78
			
79
			factory = IndexFactory.getFactory(IndexFactory.Factories.GEOMETRY.JSI_RTREE);
80
			List l3 = tryFactory(factory, null);
81

  
82
			IndexParameters p = new IndexParameters();
83
			//p.setFeatureCount(1000);			
84
			
85
			factory = IndexFactory.getFactory(IndexFactory.Factories.GEOMETRY.GT2_QUADTREE);
86
			List l4 = tryFactory(factory, p);
87
			
88
		} catch (Exception e) {
89
			logger.error("Error, ", e);
90
		}
91
		
92
	}
93
	
94
	private List tryFactory(IndexFactory factory, IndexParameters p) throws IndexException {		
95
		Index index = factory.createIndex(p);
96
		Envelope envelope = fillIndex(index);
97
		
98
		//if (p != null)	p.setEnvelope(envelope);
99
		
100
		if (index instanceof PersistentSpatialIndex) {
101
			logger.debug("*** Persistent Index.***");
102
			PersistentSpatialIndex psi = (PersistentSpatialIndex) index; 
103
			psi.flush();
104
			assertTrue(psi.getFile().exists());
105
			logger.debug(psi.getFile().getAbsolutePath());
106
		}
107
		
108
		Envelope env = new Curve2D(new GeneralPathX(new Rectangle(0,0,100,100))).getEnvelope();
109
		
110
		logger.debug("index.query(env)");
111
		
112
		SpatialQueryParameters sqp = new SpatialQueryParameters();
113
		sqp.setEnvelope(env);		
114
		List result = index.query(sqp); 
115
		logger.debug(result);
116
		return result;
117
	}
118
	
119
	private Envelope fillIndex(Index index) {
120
		if (!(index instanceof SpatialIndex)) {
121
			throw new UnsupportedOperationException("Can't create this type of index.");
122
	    }
123
		
124
		Envelope extent = new DefaultEnvelope(2);
125
		for (int i=0; i<1000; i++) {
126
			Rectangle2D r = new Rectangle(i, -i, i + 10, i * 2 - i);
127
			Envelope env = new Curve2D(new GeneralPathX(r)).getEnvelope();
128
			extent.add(env);
129
			// FIXME index.insert(env, i);
130
			if(i%100 == 0) logger.debug(env.toString());
131
		}		
132
		logger.debug("index done.");
133
		return extent;
134
	}
135
		
136
/*
137
	private FeatureType newFeatureType() {
138

  
139
		FeatureType fType = new DefaultFeatureType();
140

  
141
		try {
142
			FeatureAttributeDescriptor descriptorID = (FeatureAttributeDescriptor) fType.
143
					.createAttributeDescriptor();
144
			descriptorID.loading();
145
			descriptorID.setType(FeatureAttributeDescriptor.INT);
146
			descriptorID.setName("ID");
147
			descriptorID.setDefaultValue(new Integer(0));
148
			descriptorID.stopLoading();
149
			fType.add(descriptorID);
150

  
151
			FeatureAttributeDescriptor descriptorShape = (FeatureAttributeDescriptor) fType
152
					.createAttributeDescriptor();
153
			descriptorShape.loading();
154
			descriptorShape.setType(FeatureAttributeDescriptor.GEOMETRY);
155
			descriptorShape.setName("GEOMETRY");
156
			descriptorShape.setDefaultValue(null);
157
			descriptorShape.stopLoading();
158
			fType.add(descriptorShape);
159
			fType.setDefaultGeometry("GEOMETRY");
160

  
161
			FeatureAttributeDescriptor descriptorEntity = (FeatureAttributeDescriptor) fType
162
					.createAttributeDescriptor();
163
			descriptorEntity.loading();
164
			descriptorEntity.setType(FeatureAttributeDescriptor.STRING);
165
			descriptorEntity.setName("Entity");
166
			descriptorEntity.setDefaultValue("Entity");
167
			descriptorEntity.stopLoading();
168
			fType.add(descriptorEntity);
169

  
170
			FeatureAttributeDescriptor descriptorLayer = (FeatureAttributeDescriptor) fType
171
					.createAttributeDescriptor();
172
			descriptorLayer.loading();
173
			descriptorLayer.setType(FeatureAttributeDescriptor.STRING);
174
			descriptorLayer.setName("Layer");
175
			descriptorLayer.setDefaultValue("default");
176
			descriptorLayer.stopLoading();
177
			fType.add(descriptorLayer);
178

  
179
			FeatureAttributeDescriptor descriptorColor = (FeatureAttributeDescriptor) fType
180
					.createAttributeDescriptor();
181
			descriptorColor.loading();
182
			descriptorColor.setType(FeatureAttributeDescriptor.INT);
183
			descriptorColor.setName("Color");
184
			descriptorColor.setDefaultValue(new Integer(0));
185
			descriptorColor.stopLoading();
186
			fType.add(descriptorColor);
187

  
188
			FeatureAttributeDescriptor descriptorElevation = (FeatureAttributeDescriptor) fType
189
					.createAttributeDescriptor();
190
			descriptorElevation.loading();
191
			descriptorElevation.setType(FeatureAttributeDescriptor.DOUBLE);
192
			descriptorElevation.setName("Elevation");
193
			descriptorElevation.setDefaultValue(new Double(0));
194
			descriptorElevation.stopLoading();
195
			fType.add(descriptorElevation);
196

  
197
			FeatureAttributeDescriptor descriptorThickness = (FeatureAttributeDescriptor) fType
198
					.createAttributeDescriptor();
199
			descriptorThickness.loading();
200
			descriptorThickness.setType(FeatureAttributeDescriptor.DOUBLE);
201
			descriptorThickness.setName("Thickness");
202
			descriptorThickness.setDefaultValue(new Double(0));
203
			descriptorThickness.stopLoading();
204
			fType.add(descriptorThickness);
205

  
206
			FeatureAttributeDescriptor descriptorText = (FeatureAttributeDescriptor) fType
207
					.createAttributeDescriptor();
208
			descriptorText.loading();
209
			descriptorText.setType(FeatureAttributeDescriptor.STRING);
210
			descriptorText.setName("Text");
211
			descriptorText.setDefaultValue("");
212
			descriptorText.stopLoading();
213
			fType.add(descriptorText);
214

  
215
			FeatureAttributeDescriptor descriptorHeightText = (FeatureAttributeDescriptor) fType
216
					.createAttributeDescriptor();
217
			descriptorHeightText.loading();
218
			descriptorHeightText.setType(FeatureAttributeDescriptor.FLOAT);
219
			descriptorHeightText.setName("HeightText");
220
			descriptorHeightText.setDefaultValue(new Float(10));
221
			descriptorHeightText.stopLoading();
222
			fType.add(descriptorHeightText);
223

  
224
			FeatureAttributeDescriptor descriptorRotationText = (FeatureAttributeDescriptor) fType
225
					.createAttributeDescriptor();
226
			descriptorRotationText.loading();
227
			descriptorRotationText
228
					.setType(FeatureAttributeDescriptor.DOUBLE);
229
			descriptorRotationText.setName("Rotation");
230
			descriptorRotationText.setDefaultValue(new Double(10));
231
			descriptorRotationText.stopLoading();
232
			fType.add(descriptorRotationText);
233
			fType.setDefaultGeometry("GEOMETRY");
234
			fType.setGeometryTypes(new int[] { Geometry.TYPES.GEOMETRY });
235
		} catch (IsNotAttributeSettingException e) {
236
			e.printStackTrace();
237
		}
238

  
239
		return fType;
240

  
241
		return null;
242
	}	
243
*/	
244 40
}

Also available in: Unified diff