svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_spatialindex / src / org / gvsig / fmap / data / index / spatial / jsi / PersistentRTreeJsi.java @ 24033
History | View | Annotate | Download (6.1 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.exceptions.InitializeException; |
71 |
import org.gvsig.fmap.data.feature.FeatureReference; |
72 |
import org.gvsig.fmap.data.feature.exceptions.DataIndexException; |
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 |
/**
|
100 |
* Spatial index in memory
|
101 |
*/
|
102 |
private RTree rtree;
|
103 |
/**
|
104 |
* Spatial index file
|
105 |
*/
|
106 |
private File file; |
107 |
|
108 |
private boolean hasChanged = false; |
109 |
/**
|
110 |
* Spatial index file extension
|
111 |
*/
|
112 |
final String rExt = ".rix"; |
113 |
|
114 |
private LinkedHashMap rectangles; |
115 |
|
116 |
/**
|
117 |
* Constructor
|
118 |
* @param fileName path of the spatial index file
|
119 |
* @throws DataIndexException
|
120 |
*/
|
121 |
public PersistentRTreeJsi() {
|
122 |
rtree = new RTree();
|
123 |
} |
124 |
|
125 |
public void initialize() throws InitializeException { |
126 |
Properties props = new Properties(); |
127 |
rtree.init(props); |
128 |
try {
|
129 |
file = File.createTempFile("RTreeJsi" + getFeatureIndexProviderServices().getTemporaryFileName(), rExt); |
130 |
rectangles = new LinkedHashMap(); |
131 |
load(); |
132 |
} catch (IOException e) { |
133 |
throw new InitializeException(e); |
134 |
} catch (DataIndexException e) {
|
135 |
throw new InitializeException(e); |
136 |
} |
137 |
} |
138 |
|
139 |
public void flush(File f) throws DataIndexException { |
140 |
try {
|
141 |
if(! hasChanged)
|
142 |
return;
|
143 |
RandomAccessFile file = new RandomAccessFile(f, |
144 |
"rw");
|
145 |
FileImageOutputStream output = new FileImageOutputStream(file); |
146 |
output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
|
147 |
int numShapes = rtree.size();
|
148 |
output.writeInt(numShapes); |
149 |
|
150 |
Iterator iterator = rtree.iterator();
|
151 |
int count = 0; |
152 |
while(iterator.hasNext()){
|
153 |
Integer idx = (Integer) iterator.next(); |
154 |
Rectangle nr = (Rectangle) rectangles.get(idx); |
155 |
float xmin = nr.min[0]; |
156 |
float ymin = nr.min[1]; |
157 |
|
158 |
float xmax = nr.max[0]; |
159 |
float ymax = nr.max[1]; |
160 |
|
161 |
output.writeFloat(xmin); |
162 |
output.writeFloat(ymin); |
163 |
output.writeFloat(xmax); |
164 |
output.writeFloat(ymax); |
165 |
|
166 |
output.writeInt(idx.intValue()); |
167 |
count++; |
168 |
} |
169 |
output.flush(); |
170 |
output.close(); |
171 |
file.close(); |
172 |
hasChanged = false;
|
173 |
} catch (FileNotFoundException e) { |
174 |
throw new DataIndexException(e); |
175 |
} catch (IOException e) { |
176 |
throw new DataIndexException(e); |
177 |
} |
178 |
|
179 |
} |
180 |
|
181 |
public void flush() throws DataIndexException { |
182 |
flush(file); |
183 |
} |
184 |
|
185 |
public boolean exists() { |
186 |
return file.exists();
|
187 |
} |
188 |
|
189 |
public void load(File f) throws DataIndexException { |
190 |
if (f == null) throw new IllegalArgumentException("File f cannot be null"); |
191 |
|
192 |
try {
|
193 |
if(! f.exists()){
|
194 |
return;
|
195 |
} |
196 |
RandomAccessFile file = new RandomAccessFile(f, "r"); |
197 |
FileChannel channel = file.getChannel();
|
198 |
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
199 |
buf.order(ByteOrder.LITTLE_ENDIAN);
|
200 |
int numShapes = buf.getInt();
|
201 |
for(int i = 0; i < numShapes; i++){ |
202 |
float xmin, ymin, xmax, ymax;
|
203 |
int shapeIndex;
|
204 |
xmin = buf.getFloat(); |
205 |
ymin = buf.getFloat(); |
206 |
xmax = buf.getFloat(); |
207 |
ymax = buf.getFloat(); |
208 |
shapeIndex = buf.getInt(); |
209 |
|
210 |
Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax); |
211 |
rtree.add(jsiRect, shapeIndex); |
212 |
} |
213 |
}catch(Exception e){ |
214 |
throw new DataIndexException(e); |
215 |
} |
216 |
} |
217 |
|
218 |
public void load() throws DataIndexException { |
219 |
load(file); |
220 |
} |
221 |
|
222 |
public void close() { |
223 |
rectangles.clear(); |
224 |
rectangles = null;
|
225 |
} |
226 |
|
227 |
public void insert(Object value, FeatureReference fref) { |
228 |
super.insert(value, fref);
|
229 |
rectangles.put((Integer) fref.getId(), toJsiRect((Envelope) value));
|
230 |
hasChanged = true;
|
231 |
} |
232 |
|
233 |
|
234 |
public void delete(Object value, FeatureReference fref) { |
235 |
super.delete(value, fref);
|
236 |
rectangles.remove((Integer) fref.getId());
|
237 |
hasChanged = true;
|
238 |
} |
239 |
|
240 |
public List findNNearest(int numberOfNearest, Point2D point){ |
241 |
com.infomatiq.jsi.Point jsiPoint = |
242 |
new com.infomatiq.jsi.Point((float)point.getX(),(float)point.getY()); |
243 |
return (List) rtree.nearest(jsiPoint, numberOfNearest); |
244 |
} |
245 |
|
246 |
public File getFile() { |
247 |
return this.file; |
248 |
} |
249 |
} |
250 |
|