Revision 24033 branches/v2_0_0_prep/libraries/libFMap_spatialindex/src/org/gvsig/fmap/data/index/spatial/spatialindex/RTreeSptLib.java
RTreeSptLib.java | ||
---|---|---|
4 | 4 |
import java.io.FileNotFoundException; |
5 | 5 |
import java.io.IOException; |
6 | 6 |
import java.util.ArrayList; |
7 |
import java.util.Iterator; |
|
8 | 7 |
import java.util.List; |
9 | 8 |
|
10 |
import org.gvsig.fmap.data.ReadException; |
|
11 |
import org.gvsig.fmap.data.feature.Feature; |
|
12 |
import org.gvsig.fmap.data.index.IndexException; |
|
13 |
import org.gvsig.fmap.data.index.IndexParameters; |
|
14 |
import org.gvsig.fmap.data.index.spatial.AbstractIntBasedSpatialIndex; |
|
15 |
import org.gvsig.fmap.data.index.spatial.NearestNeighbourFinder; |
|
16 |
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex; |
|
17 |
import org.gvsig.fmap.geom.Geometry; |
|
9 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
|
10 |
import org.gvsig.fmap.data.feature.FeatureReference; |
|
11 |
import org.gvsig.fmap.data.feature.exceptions.DataIndexException; |
|
12 |
import org.gvsig.fmap.data.feature.spi.index.AbstractFeatureIndexProvider; |
|
13 |
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider; |
|
18 | 14 |
import org.gvsig.fmap.geom.primitive.Envelope; |
19 | 15 |
import org.gvsig.fmap.geom.primitive.Point2D; |
20 | 16 |
|
21 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
|
22 |
|
|
23 | 17 |
import spatialindex.rtree.RTree; |
24 | 18 |
import spatialindex.spatialindex.IData; |
25 | 19 |
import spatialindex.spatialindex.INode; |
... | ... | |
37 | 31 |
* http://u-foria.org/marioh/spatialindex/index.html <br> |
38 | 32 |
* marioh@cs.ucr.edu |
39 | 33 |
* </p> |
40 |
* It has the problem that spatial index file creation is a bit slowly |
|
41 |
* (in comparation with other indexes).
|
|
34 |
* It has the problem that spatial index file creation is a bit slowly (in
|
|
35 |
* comparation with other indexes). |
|
42 | 36 |
*/ |
43 |
public class RTreeSptLib extends AbstractIntBasedSpatialIndex implements PersistentSpatialIndex,
|
|
44 |
NearestNeighbourFinder{
|
|
37 |
public class RTreeSptLib extends AbstractFeatureIndexProvider implements
|
|
38 |
FeatureIndexProvider {
|
|
45 | 39 |
/** |
46 | 40 |
* Page size of associated file |
47 | 41 |
*/ |
... | ... | |
53 | 47 |
*/ |
54 | 48 |
private static final int BUFFER_SIZE = 25000; |
55 | 49 |
RTree rtree; |
56 |
//String rtreeFile;
|
|
50 |
String fileName;
|
|
57 | 51 |
IStorageManager diskfile; |
58 |
/** |
|
59 |
* Constructor |
|
60 |
* @param overwriteFile tells if we must override existing files. |
|
61 |
* If we are goint to create a new spatial index (or if we want to overwrite |
|
62 |
* an existing one) we must to use always 'true'. If we want to load |
|
63 |
* an existing spatial index file, overwrite must be 'false' |
|
64 |
* @param fileName name of the rtree spatial index file |
|
65 |
* @throws IndexException |
|
66 |
*/ |
|
67 |
public RTreeSptLib(IndexParameters params) throws IndexException { |
|
68 |
super(params); |
|
69 |
try { |
|
70 |
//this.rtreeFile = getName(); |
|
71 |
PropertySet ps = new PropertySet(); |
|
72 | 52 |
|
73 |
// overwrite the file if it exists. |
|
74 |
Boolean b = new Boolean(isOverwrite()); |
|
75 |
ps.setProperty("Overwrite", b); |
|
53 |
public RTreeSptLib() { |
|
76 | 54 |
|
77 |
// .idx and .dat extensions will be added. |
|
78 |
ps.setProperty("FileName", getName()); |
|
55 |
} |
|
79 | 56 |
|
80 |
Integer i = new Integer(defaultPageSize); |
|
81 |
ps.setProperty("PageSize", i); |
|
57 |
public void initialize() throws InitializeException { |
|
58 |
try { |
|
59 |
PropertySet ps = new PropertySet(); |
|
60 |
ps.setProperty("Overwrite", new Boolean(false)); |
|
61 |
// .idx and .dat extensions will be added. |
|
62 |
fileName = getFeatureIndexProviderServices().getNewFileName(null, null); |
|
63 |
ps.setProperty("FileName", fileName); |
|
64 |
ps.setProperty("PageSize", new Integer(defaultPageSize)); |
|
82 | 65 |
diskfile = new DiskStorageManager(ps); |
83 | 66 |
load(); |
84 | 67 |
} catch (SecurityException e) { |
85 |
throw new IndexException(e);
|
|
68 |
throw new InitializeException(e);
|
|
86 | 69 |
} catch (FileNotFoundException e) { |
87 |
throw new IndexException(e);
|
|
70 |
throw new InitializeException(e);
|
|
88 | 71 |
} catch (IOException e) { |
89 |
throw new IndexException(e);
|
|
72 |
throw new InitializeException(e);
|
|
90 | 73 |
} |
91 | 74 |
} |
92 |
|
|
93 |
private void initialize() { |
|
94 |
//this.rtreeFile = getName(); |
|
95 |
PropertySet ps = new PropertySet(); |
|
96 | 75 |
|
97 |
// overwrite the file if it exists. |
|
98 |
Boolean b = new Boolean(isOverwrite()); |
|
99 |
ps.setProperty("Overwrite", b); |
|
100 |
|
|
101 |
// .idx and .dat extensions will be added. |
|
102 |
ps.setProperty("FileName", getName()); |
|
103 |
|
|
104 |
Integer i = new Integer(defaultPageSize); |
|
105 |
ps.setProperty("PageSize", i); |
|
106 |
diskfile = new DiskStorageManager(ps); |
|
107 |
load(); |
|
108 |
} |
|
109 |
|
|
110 | 76 |
/** |
111 | 77 |
* If the spatial index file exists and has content |
112 | 78 |
*/ |
113 |
public boolean exists(){ |
|
114 |
return (new File(getName() + ".dat").length() != 0);
|
|
79 |
public boolean exists() {
|
|
80 |
return (new File(fileName + ".dat").length() != 0);
|
|
115 | 81 |
} |
116 | 82 |
|
117 |
|
|
118 |
class RTreeVisitor implements IVisitor{ |
|
83 |
class RTreeVisitor implements IVisitor { |
|
119 | 84 |
ArrayList solution = new ArrayList(); |
85 |
|
|
120 | 86 |
public void visitNode(INode n) { |
121 | 87 |
} |
88 |
|
|
122 | 89 |
public void visitData(IData d) { |
123 | 90 |
solution.add(new Integer(d.getIdentifier())); |
124 | 91 |
} |
125 |
public List getSolution(){ |
|
92 |
|
|
93 |
public List getSolution() { |
|
126 | 94 |
return solution; |
127 | 95 |
} |
128 | 96 |
} |
129 | 97 |
|
130 |
|
|
131 | 98 |
public List query(Envelope env) { |
132 | 99 |
List solution = null; |
133 | 100 |
Region region = createRegion(env); |
134 | 101 |
RTreeVisitor visitor = new RTreeVisitor(); |
135 |
rtree.intersectionQuery(region,visitor); |
|
102 |
rtree.intersectionQuery(region, visitor);
|
|
136 | 103 |
solution = visitor.getSolution(); |
137 | 104 |
return solution; |
138 | 105 |
} |
... | ... | |
141 | 108 |
List solution = null; |
142 | 109 |
Region region = createRegion(env); |
143 | 110 |
RTreeVisitor visitor = new RTreeVisitor(); |
144 |
rtree.containmentQuery(region,visitor); |
|
111 |
rtree.containmentQuery(region, visitor);
|
|
145 | 112 |
solution = visitor.getSolution(); |
146 | 113 |
return solution; |
147 | 114 |
} |
148 | 115 |
|
149 | 116 |
/** |
150 |
* Warn! This RTree implemention doesnt care if 'index' |
|
151 |
* entry has been indexed yet
|
|
117 |
* Warn! This RTree implemention doesnt care if 'index' entry has been
|
|
118 |
* indexed yet |
|
152 | 119 |
*/ |
153 | 120 |
public void insert(Envelope env, int index) { |
154 | 121 |
rtree.insertData(null, createRegion(env), index); |
155 | 122 |
} |
156 | 123 |
|
157 |
private Region createRegion(Envelope env){ |
|
124 |
private Region createRegion(Envelope env) {
|
|
158 | 125 |
return new Region(env.getLowerCorner(), env.getUpperCorner()); |
159 | 126 |
} |
160 | 127 |
|
... | ... | |
164 | 131 |
|
165 | 132 |
/** |
166 | 133 |
* Looks for N indexes nearest to the specified rect. |
134 |
* |
|
167 | 135 |
* @param numberOfNearest |
168 | 136 |
* @param rect |
169 | 137 |
* @return |
... | ... | |
172 | 140 |
List solution = null; |
173 | 141 |
Region region = createRegion(env); |
174 | 142 |
RTreeVisitor visitor = new RTreeVisitor(); |
175 |
rtree.nearestNeighborQuery(numberOfNearest, region,visitor); |
|
143 |
rtree.nearestNeighborQuery(numberOfNearest, region, visitor);
|
|
176 | 144 |
solution = visitor.getSolution(); |
177 | 145 |
return solution; |
178 | 146 |
} |
179 | 147 |
|
180 | 148 |
/** |
181 | 149 |
* Looks for the N indexes nearest to the specified point |
150 |
* |
|
182 | 151 |
* @param numberOfNearest |
183 | 152 |
* @param point |
184 | 153 |
* @return |
185 | 154 |
*/ |
186 | 155 |
public List findNNearest(int numberOfNearest, Point2D point) { |
187 | 156 |
List solution = null; |
188 |
spatialindex.spatialindex.Point sptPoint = new |
|
189 |
spatialindex.spatialindex.Point(new double[]{point.getX(), point.getY()});
|
|
157 |
spatialindex.spatialindex.Point sptPoint = new spatialindex.spatialindex.Point(
|
|
158 |
new double[] { point.getX(), point.getY() });
|
|
190 | 159 |
RTreeVisitor visitor = new RTreeVisitor(); |
191 |
rtree.nearestNeighborQuery(numberOfNearest, sptPoint,visitor); |
|
160 |
rtree.nearestNeighborQuery(numberOfNearest, sptPoint, visitor);
|
|
192 | 161 |
solution = visitor.getSolution(); |
193 | 162 |
return solution; |
194 | 163 |
} |
195 | 164 |
|
196 |
|
|
197 |
|
|
198 |
public void flush(){ |
|
165 |
public void flush() { |
|
199 | 166 |
rtree.flush(); |
200 | 167 |
} |
201 | 168 |
|
202 | 169 |
public void load() { |
203 |
// applies a main memory random buffer on top of the persistent
|
|
170 |
// applies a main memory random buffer on top of the persistent
|
|
204 | 171 |
// storage manager |
205 | 172 |
IBuffer buffer = new RandomEvictionsBuffer(diskfile, BUFFER_SIZE, false); |
206 | 173 |
|
... | ... | |
215 | 182 |
Integer i = new Integer(2); |
216 | 183 |
ps2.setProperty("Dimension", i); |
217 | 184 |
|
218 |
File file = new File(getName() + ".dat");
|
|
219 |
if(file.length() != 0){
|
|
185 |
File file = new File(fileName + ".dat");
|
|
186 |
if (file.length() != 0) {
|
|
220 | 187 |
ps2.setProperty("IndexIdentifier", new Integer(1)); |
221 | 188 |
} |
222 | 189 |
rtree = new RTree(ps2, buffer); |
223 | 190 |
} |
224 |
|
|
225 |
public void load(File f) throws IndexException { |
|
191 |
|
|
192 |
public void load(File f) throws DataIndexException {
|
|
226 | 193 |
load(); |
227 | 194 |
} |
228 |
|
|
229 |
public void flush(File f) throws IndexException { |
|
195 |
|
|
196 |
public void flush(File f) throws DataIndexException {
|
|
230 | 197 |
flush(); |
231 | 198 |
} |
232 | 199 |
|
... | ... | |
234 | 201 |
} |
235 | 202 |
|
236 | 203 |
public File getFile() { |
237 |
return new File(getName() + ".dat");
|
|
204 |
return new File(fileName + ".dat");
|
|
238 | 205 |
} |
239 | 206 |
|
240 |
public void clear() { |
|
241 |
this.rtree = new RTree(); |
|
207 |
public void delete(Object value, FeatureReference fref) { |
|
208 |
// TODO Auto-generated method stub |
|
209 |
|
|
242 | 210 |
} |
211 |
|
|
212 |
public void insert(Object value, FeatureReference fref) { |
|
213 |
// TODO Auto-generated method stub |
|
214 |
|
|
215 |
} |
|
216 |
|
|
217 |
public List match(Object value) { |
|
218 |
// TODO Auto-generated method stub |
|
219 |
return null; |
|
220 |
} |
|
221 |
|
|
222 |
public List match(Object min, Object max) { |
|
223 |
// TODO Auto-generated method stub |
|
224 |
return null; |
|
225 |
} |
|
226 |
|
|
227 |
public List nearest(int n, Object value) throws DataIndexException { |
|
228 |
// TODO Auto-generated method stub |
|
229 |
return null; |
|
230 |
} |
|
243 | 231 |
} |
Also available in: Unified diff