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