Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src / org / gvsig / fmap / data / index / spatial / jsi / RTreeJsi.java @ 23290

History | View | Annotate | Download (4.64 KB)

1
/*
2
 * Created on 15-may-2006
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: RTreeJsi.java 13884 2007-09-19 16:26:04Z jaume $
47
* $Log$
48
* Revision 1.5  2007-09-19 16:25:39  jaume
49
* ReadExpansionFileException removed from this context and removed unnecessary imports
50
*
51
* Revision 1.4  2007/06/27 20:17:30  azabala
52
* new spatial index (rix)
53
*
54
* Revision 1.3  2007/03/06 17:08:59  caballero
55
* Exceptions
56
*
57
* Revision 1.2  2006/06/05 16:59:08  azabala
58
* implementada busqueda de vecino mas proximo a partir de rectangulos
59
*
60
* Revision 1.1  2006/05/24 21:58:04  azabala
61
* *** empty log message ***
62
*
63
*
64
*/
65
package org.gvsig.fmap.data.index.spatial.jsi;
66

    
67
import java.util.ArrayList;
68
import java.util.Iterator;
69
import java.util.List;
70
import java.util.Properties;
71

    
72
import org.gvsig.fmap.data.index.IndexException;
73
import org.gvsig.fmap.data.index.spatial.AbstractIntBasedSpatialIndex;
74
import org.gvsig.fmap.data.index.spatial.NearestNeighbourFinder;
75
import org.gvsig.fmap.data.index.spatial.SpatialIndex;
76
import org.gvsig.fmap.geom.primitive.Envelope;
77
import org.gvsig.fmap.geom.primitive.Point2D;
78

    
79
import com.infomatiq.jsi.IntProcedure;
80
import com.infomatiq.jsi.Rectangle;
81
import com.infomatiq.jsi.rtree.RTree;
82

    
83
/**
84
 * RTree spatial index implementation based in library
85
 * JSI (java spatial index).
86
 * 
87
 * http://jsi.sourceforge.net/
88
 * 
89
 * This RTree has better performance that Spatial Index Library
90
 * RTree, and that JTS'RTree, because
91
 * it uses the GNU's Trove Collections API.
92
 * 
93
 * We are doing some probes with it, because it offers
94
 * a Nearest Neighbour algorithm implementation 
95
 * (useful for Spatial Join geoprocess, for example).
96
 * 
97
 * It isnt persistent, and We've found some problems
98
 * with delete operations.
99
 * 
100
 * 
101
 * 
102
 * 
103
 * @author azabala
104
 *
105
 */
106
public class RTreeJsi extends AbstractIntBasedSpatialIndex implements SpatialIndex, NearestNeighbourFinder {
107
        private RTree rtree;
108
        
109
        public RTreeJsi(){
110
                rtree = new RTree();
111
        }
112
        
113
        public void create(){
114
                Properties props = new Properties();
115
//                props.setProperty("MaxNodeEntries", "500");
116
//                props.setProperty("MinNodeEntries", "200");
117
                rtree.init(props);
118
        }
119
        
120
        class ListIntProcedure implements IntProcedure{
121
                ArrayList solution = new ArrayList();
122
                
123
                public boolean execute(int arg0) {
124
                        solution.add(new Integer(arg0));
125
                        return true;
126
                }
127
                
128
                public List getSolution(){
129
                        return solution;
130
                }
131
        }
132
        
133
        public List findNNearest(int numberOfNearest, Point2D point){
134
                com.infomatiq.jsi.Point jsiPoint =
135
                        new com.infomatiq.jsi.Point((float)point.getX(), (float)point.getY());
136
                return (List) rtree.nearest(jsiPoint, numberOfNearest);
137
        }
138
        
139
        //FIXME Add this method to spatial index interface
140
        public Iterator iterator(){
141
                return rtree.iterator();
142
        }
143
        
144
        public int size(){
145
                return rtree.size();
146
        }        
147
        
148
        public void delete(Envelope env, int index) {                
149
                rtree.delete(toJsiRect(env), index);                
150
        }
151

    
152
        public void insert(Envelope env, int index) {
153
                rtree.add(toJsiRect(env), index);                
154
        }
155

    
156
        public List query(Envelope env) throws IndexException {
157
                ListIntProcedure solution = new ListIntProcedure();
158
                rtree.intersects(toJsiRect(env), solution);
159
                return solution.getSolution();
160
        }
161

    
162
        public List findNNearest(int numberOfNearest, Envelope env){
163
                return (List) rtree.nearest(toJsiRect(env), numberOfNearest);
164
        }        
165
        
166
        protected Rectangle toJsiRect(Envelope env){
167
                double[] min = env.getLowerCorner();
168
                double[] max = env.getUpperCorner();
169
                
170
                Rectangle jsiRect = new Rectangle((float)min[0],
171
                                (float)min[1],
172
                                (float)max[0],
173
                                (float)max[1]);
174
                return jsiRect;
175
        }
176

    
177
        
178
        
179
        
180
        
181
}
182