Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src / org / gvsig / fmap / data / index / spatial / jsi / RTreeJsi.java @ 24197

History | View | Annotate | Download (6.96 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.exceptions.InitializeException;
73
import org.gvsig.fmap.data.feature.FeatureReference;
74
import org.gvsig.fmap.data.feature.exceptions.FeatureIndexException;
75
import org.gvsig.fmap.data.feature.spi.index.AbstractFeatureIndexProvider;
76
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider;
77
import org.gvsig.fmap.geom.primitive.Envelope;
78
import org.gvsig.fmap.geom.primitive.Point;
79
import org.gvsig.fmap.geom.primitive.Point2D;
80

    
81
import com.infomatiq.jsi.IntProcedure;
82
import com.infomatiq.jsi.Rectangle;
83
import com.infomatiq.jsi.rtree.RTree;
84

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

    
165
        public void insert(Object value, FeatureReference fref) {
166
                if (value == null) throw new IllegalArgumentException("value is null");
167
                if (!(value instanceof Envelope)) {
168
                        throw new IllegalArgumentException("Received " + value.getClass().getName() + " but expected org.gvsig.fmap.geom.primitive.Envelope.");                        
169
                }
170
                if (!(fref.getId() instanceof Integer)) throw new IllegalArgumentException("Reference Id is of type " + fref.getId().getClass().getName() + " but expected an Integer.");
171
                rtree.add(toJsiRect((Envelope) value), ((Integer) fref.getId()).intValue());
172
        }
173

    
174
        public void delete(Object value, FeatureReference fref) {
175
                if (value == null) throw new IllegalArgumentException("value is null");
176
                if (!(value instanceof Envelope)) {
177
                        throw new IllegalArgumentException("Received " + value.getClass().getName() + " but expected org.gvsig.fmap.geom.primitive.Envelope.");                        
178
                }                
179
                rtree.delete(toJsiRect((Envelope) value), ((Integer) fref.getId()).intValue());
180
        }
181
        
182
        public List match(Object value) throws FeatureIndexException {
183
                if (value == null) throw new IllegalArgumentException("value is null");
184
                if (!(value instanceof Envelope)) {
185
                        throw new IllegalArgumentException("Received " + value.getClass().getName() + " but expected org.gvsig.fmap.geom.primitive.Envelope.");                        
186
                }                
187
                ListIntProcedure solution = new ListIntProcedure();
188
                rtree.intersects(toJsiRect((Envelope) value), solution);
189
                return solution.getSolution();
190
        }        
191
        
192
        public List match(Object min, Object max) {
193
                throw new UnsupportedOperationException();
194
        }        
195

    
196
        public List nearest(int count, Object value) {
197
                if (value == null) throw new IllegalArgumentException("value is null");
198
                if (value instanceof Envelope) {
199
                        return (List) rtree.nearest(toJsiRect((Envelope) value), count);
200
                        
201
                } else if (value instanceof Point) {
202
                        Point p = (Point) value;
203
                        com.infomatiq.jsi.Point jsiPoint =
204
                                new com.infomatiq.jsi.Point((float) p.getDirectPosition().getOrdinate(0), (float) p.getDirectPosition().getOrdinate(1));
205
                        return (List) rtree.nearest(jsiPoint, count);                                                
206
                }
207
                throw new IllegalArgumentException("Received " + value.getClass().getName() + " but expected org.gvsig.fmap.geom.primitive.Envelope.");                                
208
        }
209

    
210

    
211
        public boolean isMatchSupported() {
212
                return true;
213
        }
214

    
215
        public boolean isNearestSupported() {
216
                return true;
217
        }
218

    
219
        public boolean isNearestToleranceSupported() {
220
                return false;
221
        }
222

    
223
        public boolean isRangeSupported() {
224
                return false;
225
        }
226

    
227
        public List nearest(int count, Object value, double tolerance)
228
                        throws FeatureIndexException {
229
                throw new UnsupportedOperationException();
230
        }
231

    
232
        public List range(Object value1, Object value2) throws FeatureIndexException {
233
                throw new UnsupportedOperationException();
234
        }
235

    
236
}
237