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