Revision 23197

View differences:

branches/v2_0_0_prep/libraries/libFMap_spatialindex/src-test/org/gvsig/fmap/data/index/SpatialIndexTest.java
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.index.gt2.QuadTreeGt2Factory;
40
import org.gvsig.fmap.data.index.jsi.RTreeJsiFactory;
41
import org.gvsig.fmap.data.index.jts.QuadTreeJtsFactory;
42
import org.gvsig.fmap.data.index.spatialindex.RTreeSptLibFactory;
43
import org.gvsig.fmap.geom.primitive.Curve2D;
44
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
45
import org.gvsig.fmap.geom.primitive.Envelope;
46
import org.gvsig.fmap.geom.primitive.GeneralPathX;
47

  
48
public class SpatialIndexTest extends TestCase {
49
	
50
	private static Logger logger = Logger.getLogger("org.gvsig");
51

  
52
	static {
53
		logger.addAppender(new ConsoleAppender(new SimpleLayout()));
54
	}
55
	
56
	protected void setUp() throws Exception {
57
		QuadTreeJtsFactory f1 = new QuadTreeJtsFactory();
58
		QuadTreeGt2Factory f2 = new QuadTreeGt2Factory();
59
		RTreeJsiFactory f3 = new RTreeJsiFactory();
60
		RTreeSptLibFactory f4 = new RTreeSptLibFactory();
61
		super.setUp();
62
	}
63
	
64
	public void testSpatialIndex() {
65
		
66
		try {
67
			SpatialIndexFactory factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.JTS_QUADTREE);			
68
			List l1 = tryFactory(factory, null);
69
			
70
			factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.SPATIALINDEX_RTREE);
71
			List l2 = tryFactory(factory, null);
72
			
73
			factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.JSI_RTREE);
74
			List l3 = tryFactory(factory, null);
75

  
76
			SpatialIndexParameters p = new SpatialIndexParameters();
77
			p.setFeatureCount(1000);			
78
			
79
			factory = SpatialIndexFactory.getFactory(SpatialIndexFactory.Factories.GT2_QUADTREE);
80
			List l4 = tryFactory(factory, p);
81
			
82
		} catch (Exception e) {
83
			logger.error("Error, ", e);
84
		}
85
		
86
	}
87
	
88
	private List tryFactory(SpatialIndexFactory factory, SpatialIndexParameters p) throws IndexException {		
89
		SpatialIndex index = factory.createSpatialIndex(p);
90
		Envelope envelope = fillIndex(index);
91
		
92
		if (p != null)	p.setEnvelope(envelope);
93
		
94
		if (index instanceof PersistentSpatialIndex) {
95
			logger.debug("*** Persistent Index.***");
96
			PersistentSpatialIndex psi = (PersistentSpatialIndex) index; 
97
			psi.flush();
98
			assertTrue(psi.getFile().exists());
99
			logger.debug(psi.getFile().getAbsolutePath());
100
		}
101
		
102

  
103
		
104
		Envelope env = new Curve2D(new GeneralPathX(new Rectangle(0,0,100,100))).getEnvelope();
105
		
106
		logger.debug("index.query(env)");
107
		
108
		SpatialQueryParameters sqp = new SpatialQueryParameters();
109
		sqp.setEnvelope(env);		
110
		List result = index.query(sqp); 
111
		logger.debug(result);
112
		return result;
113
	}
114
	
115
	private Envelope fillIndex(SpatialIndex index) {
116
		Envelope extent = new DefaultEnvelope(2);
117
		for (int i=0; i<1000; i++) {
118
			Rectangle2D r = new Rectangle(i, -i, i + 10, i * 2 - i);
119
			Envelope env = new Curve2D(new GeneralPathX(r)).getEnvelope();
120
			extent.add(env);
121
			// FIXME index.insert(env, i);
122
			if(i%100 == 0) logger.debug(env.toString());
123
		}		
124
		logger.debug("index done.");
125
		return extent;
126
	}
127
	
128
	
129
}

Also available in: Unified diff