Statistics
| Revision:

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

History | View | Annotate | Download (5.8 KB)

1
/*
2
 * Created on 13-jun-2007
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: PersistentRTreeJsi.java 12380 2007-06-27 20:17:30Z azabala $
47
* $Log$
48
* Revision 1.1  2007-06-27 20:17:30  azabala
49
* new spatial index (rix)
50
*
51
*
52
*/
53
package org.gvsig.fmap.data.index.spatial.jsi;
54

    
55
import java.awt.geom.Point2D;
56
import java.io.File;
57
import java.io.FileNotFoundException;
58
import java.io.IOException;
59
import java.io.RandomAccessFile;
60
import java.nio.ByteOrder;
61
import java.nio.MappedByteBuffer;
62
import java.nio.channels.FileChannel;
63
import java.util.Iterator;
64
import java.util.LinkedHashMap;
65
import java.util.List;
66
import java.util.Properties;
67

    
68
import javax.imageio.stream.FileImageOutputStream;
69

    
70
import org.gvsig.fmap.data.index.IndexException;
71
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex;
72
import org.gvsig.fmap.geom.primitive.Envelope;
73

    
74
import com.infomatiq.jsi.Rectangle;
75
import com.infomatiq.jsi.rtree.RTree;
76

    
77
/**
78
 * Persistent spatial index which can resolve nearest neighbour queries.
79
 * <br>
80
 * 
81
 * To use:
82
 * 
83
 * PersistentRTreeJsi sptidx = new PersistentRtreeJsi("/home/kk");
84
 * if(sptidx.exists())
85
 *  sptidx.load();
86
 *  
87
 *  
88
 *  sptidx.add(rect, int);
89
 *  ...
90
 *  sptidx.add(rect2,int2);
91
 *  sptidx.flush();
92
 * 
93
 * @author azabala
94
 *
95
 */
96
public class PersistentRTreeJsi extends RTreeJsi implements PersistentSpatialIndex {
97

    
98
        /**
99
         * Spatial index in memory
100
         */
101
        private RTree rtree;
102
        /**
103
         * Spatial index file
104
         */
105
        private File rtreeFile;
106
        
107
        private boolean hasChange = false;
108
        /**
109
         * Spatial index file extension
110
         */
111
        final String rExt = ".rix";
112
        
113
        private LinkedHashMap  rectangles;
114
        
115
        /**
116
         * Constructor
117
         * @param file path of the spatial index file
118
         * @throws IndexException 
119
         */
120
        public PersistentRTreeJsi(String file, boolean overwrite) throws IndexException {
121
                rtree = new RTree();
122
                Properties props = new Properties();
123
                rtree.init(props);
124
                rtreeFile = new File(file + rExt);
125
                rectangles = new LinkedHashMap();
126
                if(! overwrite)
127
                        load();
128
        }
129
        
130
        public void flush(File f) throws IndexException {
131
                try {
132
                        if(! hasChange)
133
                                return;
134
                        RandomAccessFile file = new RandomAccessFile(f,
135
                                                                                                                        "rw");
136
                        FileImageOutputStream output = new FileImageOutputStream(file);
137
                        output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
138
                        int numShapes = rtree.size();
139
                        output.writeInt(numShapes);
140
                        
141
                        Iterator iterator = rtree.iterator();
142
                        int count = 0;
143
                        while(iterator.hasNext()){
144
                                Integer  idx = (Integer) iterator.next();
145
                                Rectangle nr = (Rectangle) rectangles.get(idx);
146
                                float xmin = nr.min[0];
147
                                float ymin = nr.min[1];
148
                                
149
                                float xmax = nr.max[0];
150
                                float ymax = nr.max[1];
151
                                
152
                                output.writeFloat(xmin);
153
                                output.writeFloat(ymin);
154
                                output.writeFloat(xmax);
155
                                output.writeFloat(ymax);
156
                                
157
                                output.writeInt(idx.intValue());
158
                                count++;
159
                        }
160
                        output.flush();
161
                        output.close();
162
                        file.close();
163
                        hasChange = false;
164
                } catch (FileNotFoundException e) {
165
                        throw new IndexException(e);
166
                } catch (IOException e) {
167
                        throw new IndexException(e);
168
                }
169
                
170
        }
171
        
172
        public void flush() throws IndexException {
173
                flush(rtreeFile);
174
        }
175

    
176
        public boolean exists() {
177
                return rtreeFile.exists();
178
        }
179

    
180
        public void load(File f) throws IndexException {
181
                if (f == null) throw new IllegalArgumentException("File f cannot be null");
182
                
183
                try {
184
                        if(! f.exists()){
185
                                return;
186
                        }
187
                        RandomAccessFile file = new RandomAccessFile(f, "r");
188
                        FileChannel channel = file.getChannel();
189
                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
190
                        buf.order(ByteOrder.LITTLE_ENDIAN);
191
                        int numShapes = buf.getInt();
192
                        for(int i = 0; i < numShapes; i++){
193
                                float xmin, ymin, xmax, ymax;
194
                                int shapeIndex;
195
                                xmin = buf.getFloat();
196
                                ymin = buf.getFloat();
197
                                xmax = buf.getFloat();
198
                                ymax = buf.getFloat();
199
                                shapeIndex = buf.getInt();
200
                                
201
                                Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax);
202
                                rtree.add(jsiRect, shapeIndex);
203
                        }
204
                }catch(Exception e){
205
                        throw new IndexException(e);
206
                }                
207
        }
208
        
209
        public void load() throws IndexException {
210
                load(rtreeFile);
211
        }
212

    
213
        public void close() {
214
                rectangles.clear();
215
                rectangles = null;
216
        }
217
        
218
        public void insert(Envelope env, int index) {
219
                super.insert(env, index);
220
                rectangles.put(new Integer(index), toJsiRect(env));
221
                hasChange = true;
222
        }
223

    
224
        
225
        public void delete(Envelope env, int index) {
226
                super.delete(env, index);
227
                rectangles.remove(new Integer(index));
228
                hasChange = true;
229
        }
230
        
231
        public List findNNearest(int numberOfNearest, Point2D point){
232
                com.infomatiq.jsi.Point jsiPoint =
233
                        new com.infomatiq.jsi.Point((float)point.getX(),(float)point.getY());
234
                return (List) rtree.nearest(jsiPoint, numberOfNearest);
235
        }
236
        
237
        public File getFile() {
238
                return this.rtreeFile;
239
        }
240
}
241