svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalindex / src / org / gvsig / fmap / dal / index / spatial / jsi / JSIPersistentRTree.java @ 25763
History | View | Annotate | Download (6.28 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.Geometry; |
74 |
import org.gvsig.fmap.geom.primitive.Envelope; |
75 |
|
76 |
import com.infomatiq.jsi.Rectangle; |
77 |
import com.infomatiq.jsi.rtree.RTree; |
78 |
|
79 |
/**
|
80 |
* Persistent spatial index which can resolve nearest neighbour queries.
|
81 |
* <br>
|
82 |
*
|
83 |
* To use:
|
84 |
*
|
85 |
* PersistentRTreeJsi sptidx = new PersistentRtreeJsi("/home/kk");
|
86 |
* if(sptidx.exists())
|
87 |
* sptidx.load();
|
88 |
*
|
89 |
*
|
90 |
* sptidx.add(rect, int);
|
91 |
* ...
|
92 |
* sptidx.add(rect2,int2);
|
93 |
* sptidx.flush();
|
94 |
*
|
95 |
* @author azabala
|
96 |
*
|
97 |
*/
|
98 |
public class JSIPersistentRTree extends JSIRTree { |
99 |
|
100 |
public static final String NAME = JSIPersistentRTree.class.getSimpleName(); |
101 |
|
102 |
/**
|
103 |
* Spatial index in memory
|
104 |
*/
|
105 |
private RTree rtree;
|
106 |
/**
|
107 |
* Spatial index file
|
108 |
*/
|
109 |
private File file; |
110 |
|
111 |
private boolean hasChanged = false; |
112 |
/**
|
113 |
* Spatial index file extension
|
114 |
*/
|
115 |
final String rExt = ".rix"; |
116 |
|
117 |
private LinkedHashMap rectangles; |
118 |
|
119 |
/**
|
120 |
* Constructor
|
121 |
* @param fileName path of the spatial index file
|
122 |
* @throws FeatureIndexException
|
123 |
*/
|
124 |
public JSIPersistentRTree() {
|
125 |
rtree = new RTree();
|
126 |
} |
127 |
|
128 |
public void initialize() throws InitializeException { |
129 |
Properties props = new Properties(); |
130 |
rtree.init(props); |
131 |
try {
|
132 |
file = File.createTempFile("RTreeJsi" + getFeatureIndexProviderServices().getTemporaryFileName(), rExt); |
133 |
rectangles = new LinkedHashMap(); |
134 |
load(); |
135 |
} catch (IOException e) { |
136 |
throw new InitializeException(e); |
137 |
} catch (FeatureIndexException e) {
|
138 |
throw new InitializeException(e); |
139 |
} |
140 |
} |
141 |
|
142 |
public void flush(File f) throws FeatureIndexException { |
143 |
try {
|
144 |
if(! hasChanged) {
|
145 |
return;
|
146 |
} |
147 |
RandomAccessFile file = new RandomAccessFile(f, |
148 |
"rw");
|
149 |
FileImageOutputStream output = new FileImageOutputStream(file); |
150 |
output.setByteOrder(ByteOrder.LITTLE_ENDIAN);
|
151 |
int numShapes = rtree.size();
|
152 |
output.writeInt(numShapes); |
153 |
|
154 |
Iterator iterator = rtree.iterator();
|
155 |
int count = 0; |
156 |
while(iterator.hasNext()){
|
157 |
Integer idx = (Integer) iterator.next(); |
158 |
Rectangle nr = (Rectangle) rectangles.get(idx); |
159 |
float xmin = nr.min[0]; |
160 |
float ymin = nr.min[1]; |
161 |
|
162 |
float xmax = nr.max[0]; |
163 |
float ymax = nr.max[1]; |
164 |
|
165 |
output.writeFloat(xmin); |
166 |
output.writeFloat(ymin); |
167 |
output.writeFloat(xmax); |
168 |
output.writeFloat(ymax); |
169 |
|
170 |
output.writeInt(idx.intValue()); |
171 |
count++; |
172 |
} |
173 |
output.flush(); |
174 |
output.close(); |
175 |
file.close(); |
176 |
hasChanged = false;
|
177 |
} catch (FileNotFoundException e) { |
178 |
throw new FeatureIndexException(e); |
179 |
} catch (IOException e) { |
180 |
throw new FeatureIndexException(e); |
181 |
} |
182 |
|
183 |
} |
184 |
|
185 |
public void flush() throws FeatureIndexException { |
186 |
flush(file); |
187 |
} |
188 |
|
189 |
public boolean exists() { |
190 |
return file.exists();
|
191 |
} |
192 |
|
193 |
public void load(File f) throws FeatureIndexException { |
194 |
if (f == null) { |
195 |
throw new IllegalArgumentException("File f cannot be null"); |
196 |
} |
197 |
|
198 |
try {
|
199 |
if(! f.exists()){
|
200 |
return;
|
201 |
} |
202 |
RandomAccessFile file = new RandomAccessFile(f, "r"); |
203 |
FileChannel channel = file.getChannel();
|
204 |
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
205 |
buf.order(ByteOrder.LITTLE_ENDIAN);
|
206 |
int numShapes = buf.getInt();
|
207 |
for(int i = 0; i < numShapes; i++){ |
208 |
float xmin, ymin, xmax, ymax;
|
209 |
int shapeIndex;
|
210 |
xmin = buf.getFloat(); |
211 |
ymin = buf.getFloat(); |
212 |
xmax = buf.getFloat(); |
213 |
ymax = buf.getFloat(); |
214 |
shapeIndex = buf.getInt(); |
215 |
|
216 |
Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax); |
217 |
rtree.add(jsiRect, shapeIndex); |
218 |
} |
219 |
}catch(Exception e){ |
220 |
throw new FeatureIndexException(e); |
221 |
} |
222 |
} |
223 |
|
224 |
public void load() throws FeatureIndexException { |
225 |
load(file); |
226 |
} |
227 |
|
228 |
public void close() { |
229 |
rectangles.clear(); |
230 |
rectangles = null;
|
231 |
} |
232 |
|
233 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
234 |
Envelope env = ((Geometry) value).getEnvelope(); |
235 |
super.insert(env, fref);
|
236 |
rectangles.put(fref.getOID(), toJsiRect(env)); |
237 |
hasChanged = true;
|
238 |
} |
239 |
|
240 |
|
241 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
242 |
super.delete(value, fref);
|
243 |
rectangles.remove(fref.getOID()); |
244 |
hasChanged = true;
|
245 |
} |
246 |
|
247 |
public List findNNearest(int numberOfNearest, Point2D point){ |
248 |
com.infomatiq.jsi.Point jsiPoint = |
249 |
new com.infomatiq.jsi.Point((float)point.getX(),(float)point.getY()); |
250 |
return (List) rtree.nearest(jsiPoint, numberOfNearest); |
251 |
} |
252 |
|
253 |
public File getFile() { |
254 |
return this.file; |
255 |
} |
256 |
} |
257 |
|