Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dalindex / src / org / gvsig / fmap / dal / index / spatial / jsi / PersistentRTreeJsi.java @ 24636

History | View | Annotate | Download (6.18 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.dal.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.dal.exception.InitializeException;
71
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException;
72
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices;
73
import org.gvsig.fmap.geom.primitive.Envelope;
74

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

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

    
99
        public static final String NAME = "PersistentRTreeJsi";
100

    
101
        /**
102
         * Spatial index in memory
103
         */
104
        private RTree rtree;
105
        /**
106
         * Spatial index file
107
         */
108
        private File file;
109

    
110
        private boolean hasChanged = false;
111
        /**
112
         * Spatial index file extension
113
         */
114
        final String rExt = ".rix";
115

    
116
        private LinkedHashMap  rectangles;
117

    
118
        /**
119
         * Constructor
120
         * @param fileName path of the spatial index file
121
         * @throws FeatureIndexException
122
         */
123
        public PersistentRTreeJsi() {
124
                rtree = new RTree();
125
        }
126

    
127
        public void initialize() throws InitializeException {
128
                Properties props = new Properties();
129
                rtree.init(props);
130
                try {
131
                        file = File.createTempFile("RTreeJsi" + getFeatureIndexProviderServices().getTemporaryFileName(), rExt);
132
                        rectangles = new LinkedHashMap();
133
                        load();
134
                } catch (IOException e) {
135
                        throw new InitializeException(e);
136
                } catch (FeatureIndexException e) {
137
                        throw new InitializeException(e);
138
                }
139
        }
140

    
141
        public void flush(File f) throws FeatureIndexException {
142
                try {
143
                        if(! hasChanged) {
144
                                return;
145
                        }
146
                        RandomAccessFile file = new RandomAccessFile(f,
147
                                                                                                                        "rw");
148
                        FileImageOutputStream output = new FileImageOutputStream(file);
149
                        output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
150
                        int numShapes = rtree.size();
151
                        output.writeInt(numShapes);
152

    
153
                        Iterator iterator = rtree.iterator();
154
                        int count = 0;
155
                        while(iterator.hasNext()){
156
                                Integer  idx = (Integer) iterator.next();
157
                                Rectangle nr = (Rectangle) rectangles.get(idx);
158
                                float xmin = nr.min[0];
159
                                float ymin = nr.min[1];
160

    
161
                                float xmax = nr.max[0];
162
                                float ymax = nr.max[1];
163

    
164
                                output.writeFloat(xmin);
165
                                output.writeFloat(ymin);
166
                                output.writeFloat(xmax);
167
                                output.writeFloat(ymax);
168

    
169
                                output.writeInt(idx.intValue());
170
                                count++;
171
                        }
172
                        output.flush();
173
                        output.close();
174
                        file.close();
175
                        hasChanged = false;
176
                } catch (FileNotFoundException e) {
177
                        throw new FeatureIndexException(e);
178
                } catch (IOException e) {
179
                        throw new FeatureIndexException(e);
180
                }
181

    
182
        }
183

    
184
        public void flush() throws FeatureIndexException {
185
                flush(file);
186
        }
187

    
188
        public boolean exists() {
189
                return file.exists();
190
        }
191

    
192
        public void load(File f) throws FeatureIndexException {
193
                if (f == null) {
194
                        throw new IllegalArgumentException("File f cannot be null");
195
                }
196

    
197
                try {
198
                        if(! f.exists()){
199
                                return;
200
                        }
201
                        RandomAccessFile file = new RandomAccessFile(f, "r");
202
                        FileChannel channel = file.getChannel();
203
                        MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
204
                        buf.order(ByteOrder.LITTLE_ENDIAN);
205
                        int numShapes = buf.getInt();
206
                        for(int i = 0; i < numShapes; i++){
207
                                float xmin, ymin, xmax, ymax;
208
                                int shapeIndex;
209
                                xmin = buf.getFloat();
210
                                ymin = buf.getFloat();
211
                                xmax = buf.getFloat();
212
                                ymax = buf.getFloat();
213
                                shapeIndex = buf.getInt();
214

    
215
                                Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax);
216
                                rtree.add(jsiRect, shapeIndex);
217
                        }
218
                }catch(Exception e){
219
                        throw new FeatureIndexException(e);
220
                }
221
        }
222

    
223
        public void load() throws FeatureIndexException {
224
                load(file);
225
        }
226

    
227
        public void close() {
228
                rectangles.clear();
229
                rectangles = null;
230
        }
231

    
232
        public void insert(Object value, FeatureReferenceProviderServices fref) {
233
                super.insert(value, fref);
234
                rectangles.put(fref.getOID(), toJsiRect((Envelope) value));
235
                hasChanged = true;
236
        }
237

    
238

    
239
        public void delete(Object value, FeatureReferenceProviderServices fref) {
240
                super.delete(value, fref);
241
                rectangles.remove(fref.getOID());
242
                hasChanged = true;
243
        }
244

    
245
        public List findNNearest(int numberOfNearest, Point2D point){
246
                com.infomatiq.jsi.Point jsiPoint =
247
                        new com.infomatiq.jsi.Point((float)point.getX(),(float)point.getY());
248
                return (List) rtree.nearest(jsiPoint, numberOfNearest);
249
        }
250

    
251
        public File getFile() {
252
                return this.file;
253
        }
254
}
255