Revision 38486
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIRTree.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 15-may-2006 |
|
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: RTreeJsi.java 13884 2007-09-19 16:26:04Z jaume $ |
|
47 |
* $Log$ |
|
48 |
* Revision 1.5 2007-09-19 16:25:39 jaume |
|
49 |
* ReadExpansionFileException removed from this context and removed unnecessary imports |
|
50 |
* |
|
51 |
* Revision 1.4 2007/06/27 20:17:30 azabala |
|
52 |
* new spatial index (rix) |
|
53 |
* |
|
54 |
* Revision 1.3 2007/03/06 17:08:59 caballero |
|
55 |
* Exceptions |
|
56 |
* |
|
57 |
* Revision 1.2 2006/06/05 16:59:08 azabala |
|
58 |
* implementada busqueda de vecino mas proximo a partir de rectangulos |
|
59 |
* |
|
60 |
* Revision 1.1 2006/05/24 21:58:04 azabala |
|
61 |
* *** empty log message *** |
|
62 |
* |
|
63 |
* |
|
64 |
*/ |
|
65 |
package org.gvsig.fmap.dal.index.spatial.jsi; |
|
66 |
|
|
67 |
import java.util.ArrayList; |
|
68 |
import java.util.Iterator; |
|
69 |
import java.util.List; |
|
70 |
import java.util.Properties; |
|
71 |
|
|
72 |
import com.infomatiq.jsi.IntProcedure; |
|
73 |
import com.infomatiq.jsi.Rectangle; |
|
74 |
import com.infomatiq.jsi.rtree.RTree; |
|
75 |
|
|
76 |
import org.gvsig.fmap.dal.exception.DataException; |
|
77 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
78 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
79 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
80 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
81 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider; |
|
82 |
import org.gvsig.fmap.geom.Geometry; |
|
83 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
84 |
import org.gvsig.fmap.geom.primitive.Point; |
|
85 |
|
|
86 |
/** |
|
87 |
* RTree spatial index implementation based in library |
|
88 |
* JSI (java spatial index). |
|
89 |
* |
|
90 |
* http://jsi.sourceforge.net/ |
|
91 |
* |
|
92 |
* This RTree has better performance that Spatial Index Library |
|
93 |
* RTree, and that JTS'RTree, because |
|
94 |
* it uses the GNU's Trove Collections API. |
|
95 |
* |
|
96 |
* We are doing some probes with it, because it offers |
|
97 |
* a Nearest Neighbour algorithm implementation |
|
98 |
* (useful for Spatial Join geoprocess, for example). |
|
99 |
* |
|
100 |
* It isnt persistent, and We've found some problems |
|
101 |
* with delete operations. |
|
102 |
* |
|
103 |
* |
|
104 |
* |
|
105 |
* |
|
106 |
* @author azabala |
|
107 |
* @author jyarza |
|
108 |
* |
|
109 |
*/ |
|
110 |
public class JSIRTree extends AbstractFeatureIndexProvider implements |
|
111 |
FeatureIndexProvider { |
|
112 |
|
|
113 |
public static final String NAME = JSIRTree.class.getSimpleName(); |
|
114 |
|
|
115 |
protected RTree rtree; |
|
116 |
|
|
117 |
public JSIRTree() { |
|
118 |
} |
|
119 |
|
|
120 |
public void initialize() throws InitializeException { |
|
121 |
this.rtree = createRTree(); |
|
122 |
} |
|
123 |
|
|
124 |
private RTree createRTree() { |
|
125 |
RTree rtree = new RTree(); |
|
126 |
Properties props = new Properties(); |
|
127 |
// props.setProperty("MaxNodeEntries", "500"); |
|
128 |
// props.setProperty("MinNodeEntries", "200"); |
|
129 |
rtree.init(props); |
|
130 |
return rtree; |
|
131 |
} |
|
132 |
|
|
133 |
class ListIntProcedure implements IntProcedure { |
|
134 |
|
|
135 |
ArrayList solution = new ArrayList(); |
|
136 |
|
|
137 |
public boolean execute(int arg0) { |
|
138 |
solution.add(new Integer(arg0)); |
|
139 |
return true; |
|
140 |
} |
|
141 |
|
|
142 |
public List getSolution() { |
|
143 |
return solution; |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
protected List findNNearest(int numberOfNearest, Point point) { |
|
148 |
com.infomatiq.jsi.Point jsiPoint = |
|
149 |
new com.infomatiq.jsi.Point((float) point.getX(), |
|
150 |
(float) point.getY()); |
|
151 |
return (List) rtree.nearest(jsiPoint, numberOfNearest); |
|
152 |
} |
|
153 |
|
|
154 |
public Iterator iterator() { |
|
155 |
return rtree.iterator(); |
|
156 |
} |
|
157 |
|
|
158 |
public int size() { |
|
159 |
return rtree.size(); |
|
160 |
} |
|
161 |
|
|
162 |
protected Rectangle toJsiRect(Envelope env) { |
|
163 |
Point min = env.getLowerCorner(); |
|
164 |
Point max = env.getUpperCorner(); |
|
165 |
|
|
166 |
Rectangle jsiRect = |
|
167 |
new Rectangle((float) min.getX(), (float) min.getY(), |
|
168 |
(float) max.getX(), (float) max.getY()); |
|
169 |
return jsiRect; |
|
170 |
} |
|
171 |
|
|
172 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
173 |
Envelope env = getEnvelope(value); |
|
174 |
|
|
175 |
if (env == null) { |
|
176 |
throw new IllegalArgumentException( |
|
177 |
"value is neither Geometry or Envelope"); |
|
178 |
} |
|
179 |
|
|
180 |
Object oid = fref.getOID(); |
|
181 |
if (!isCompatibleOID(oid)) { |
|
182 |
throw new IllegalArgumentException("OID type not compatible: " |
|
183 |
+ oid.getClass().getName()); |
|
184 |
} |
|
185 |
|
|
186 |
rtree.add(toJsiRect(env), ((Number) oid).intValue()); |
|
187 |
} |
|
188 |
|
|
189 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
190 |
Envelope env = getEnvelope(value); |
|
191 |
|
|
192 |
if (env == null) { |
|
193 |
throw new IllegalArgumentException( |
|
194 |
"value is neither Geometry or Envelope"); |
|
195 |
} |
|
196 |
|
|
197 |
Object oid = fref.getOID(); |
|
198 |
if (!isCompatibleOID(oid)) { |
|
199 |
throw new IllegalArgumentException("OID type not compatible: " |
|
200 |
+ oid.getClass().getName()); |
|
201 |
} |
|
202 |
|
|
203 |
rtree.delete(toJsiRect(env), ((Number) oid).intValue()); |
|
204 |
} |
|
205 |
|
|
206 |
public List match(Object value) throws FeatureIndexException { |
|
207 |
Envelope env = getEnvelope(value); |
|
208 |
|
|
209 |
if (env == null) { |
|
210 |
throw new IllegalArgumentException( |
|
211 |
"value is neither Geometry or Envelope"); |
|
212 |
} |
|
213 |
ListIntProcedure solution = new ListIntProcedure(); |
|
214 |
rtree.intersects(toJsiRect(env), solution); |
|
215 |
return new LongList(solution.getSolution()); |
|
216 |
} |
|
217 |
|
|
218 |
public List nearest(int count, Object value) { |
|
219 |
if (value == null) { |
|
220 |
throw new IllegalArgumentException("value is null"); |
|
221 |
} |
|
222 |
|
|
223 |
if (value instanceof Point) { |
|
224 |
Point p = (Point) value; |
|
225 |
com.infomatiq.jsi.Point jsiPoint = |
|
226 |
new com.infomatiq.jsi.Point((float) p.getDirectPosition() |
|
227 |
.getOrdinate(0), (float) p.getDirectPosition().getOrdinate( |
|
228 |
1)); |
|
229 |
return (List) rtree.nearest(jsiPoint, count); |
|
230 |
} else { |
|
231 |
Envelope env = getEnvelope(value); |
|
232 |
|
|
233 |
if (env == null) { |
|
234 |
throw new IllegalArgumentException( |
|
235 |
"value is neither Geometry or Envelope"); |
|
236 |
} |
|
237 |
return new LongList((List) rtree.nearest(toJsiRect(env), count)); |
|
238 |
} |
|
239 |
} |
|
240 |
|
|
241 |
public boolean isMatchSupported() { |
|
242 |
return true; |
|
243 |
} |
|
244 |
|
|
245 |
public boolean isNearestSupported() { |
|
246 |
return true; |
|
247 |
} |
|
248 |
|
|
249 |
public boolean isNearestToleranceSupported() { |
|
250 |
return false; |
|
251 |
} |
|
252 |
|
|
253 |
public boolean isRangeSupported() { |
|
254 |
return false; |
|
255 |
} |
|
256 |
|
|
257 |
public List nearest(int count, Object value, Object tolerance) |
|
258 |
throws FeatureIndexException { |
|
259 |
throw new UnsupportedOperationException(); |
|
260 |
} |
|
261 |
|
|
262 |
public List range(Object value1, Object value2) |
|
263 |
throws FeatureIndexException { |
|
264 |
throw new UnsupportedOperationException(); |
|
265 |
} |
|
266 |
|
|
267 |
/** |
|
268 |
* Indicates whether the given OID's type is compatible |
|
269 |
* with this provider |
|
270 |
* |
|
271 |
* @param oid |
|
272 |
* |
|
273 |
* @return |
|
274 |
* true if this index provider supports the given oid type |
|
275 |
*/ |
|
276 |
private boolean isCompatibleOID(Object oid) { |
|
277 |
if (!(oid instanceof Number)) { |
|
278 |
return false; |
|
279 |
} |
|
280 |
|
|
281 |
long num = ((Number) oid).longValue(); |
|
282 |
|
|
283 |
if (num > Integer.MAX_VALUE || num < Integer.MIN_VALUE) { |
|
284 |
return false; |
|
285 |
} |
|
286 |
|
|
287 |
return true; |
|
288 |
} |
|
289 |
|
|
290 |
protected Envelope getEnvelope(Object value) { |
|
291 |
Envelope env = null; |
|
292 |
|
|
293 |
if (value instanceof Envelope) { |
|
294 |
env = (Envelope) value; |
|
295 |
} else |
|
296 |
if (value instanceof Geometry) { |
|
297 |
env = ((Geometry) value).getEnvelope(); |
|
298 |
} |
|
299 |
return env; |
|
300 |
} |
|
301 |
|
|
302 |
public void clear() throws DataException { |
|
303 |
rtree = createRTree(); |
|
304 |
} |
|
305 |
} |
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIPersistentRTree.java | ||
---|---|---|
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 file |
|
104 |
*/ |
|
105 |
private File file; |
|
106 |
|
|
107 |
private boolean hasChanged = false; |
|
108 |
/** |
|
109 |
* Spatial index file extension |
|
110 |
*/ |
|
111 |
final String rExt = ".rix"; |
|
112 |
|
|
113 |
private LinkedHashMap rectangles; |
|
114 |
|
|
115 |
public JSIPersistentRTree() { |
|
116 |
super(); |
|
117 |
} |
|
118 |
|
|
119 |
public void initialize() throws InitializeException { |
|
120 |
super.initialize(); |
|
121 |
try { |
|
122 |
file = File.createTempFile("RTreeJsi" + getFeatureIndexProviderServices().getTemporaryFileName(), rExt); |
|
123 |
rectangles = new LinkedHashMap(); |
|
124 |
load(file); |
|
125 |
} catch (IOException e) { |
|
126 |
throw new InitializeException(e); |
|
127 |
} catch (FeatureIndexException e) { |
|
128 |
throw new InitializeException(e); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
public void flush(File f) throws FeatureIndexException { |
|
133 |
try { |
|
134 |
if(! hasChanged) { |
|
135 |
return; |
|
136 |
} |
|
137 |
RandomAccessFile file = new RandomAccessFile(f, |
|
138 |
"rw"); |
|
139 |
FileImageOutputStream output = new FileImageOutputStream(file); |
|
140 |
output.setByteOrder(ByteOrder.LITTLE_ENDIAN); |
|
141 |
int numShapes = rtree.size(); |
|
142 |
output.writeInt(numShapes); |
|
143 |
|
|
144 |
Iterator iterator = rtree.iterator(); |
|
145 |
int count = 0; |
|
146 |
while(iterator.hasNext()){ |
|
147 |
Integer idx = (Integer) iterator.next(); |
|
148 |
Rectangle nr = (Rectangle) rectangles.get(idx); |
|
149 |
float xmin = nr.min[0]; |
|
150 |
float ymin = nr.min[1]; |
|
151 |
|
|
152 |
float xmax = nr.max[0]; |
|
153 |
float ymax = nr.max[1]; |
|
154 |
|
|
155 |
output.writeFloat(xmin); |
|
156 |
output.writeFloat(ymin); |
|
157 |
output.writeFloat(xmax); |
|
158 |
output.writeFloat(ymax); |
|
159 |
|
|
160 |
output.writeInt(idx.intValue()); |
|
161 |
count++; |
|
162 |
} |
|
163 |
output.flush(); |
|
164 |
output.close(); |
|
165 |
file.close(); |
|
166 |
hasChanged = false; |
|
167 |
} catch (FileNotFoundException e) { |
|
168 |
throw new FeatureIndexException(e); |
|
169 |
} catch (IOException e) { |
|
170 |
throw new FeatureIndexException(e); |
|
171 |
} |
|
172 |
|
|
173 |
} |
|
174 |
|
|
175 |
private void load(File f) throws FeatureIndexException { |
|
176 |
if (f == null) { |
|
177 |
throw new IllegalArgumentException("File f cannot be null"); |
|
178 |
} |
|
179 |
|
|
180 |
try { |
|
181 |
if(! f.exists()){ |
|
182 |
return; |
|
183 |
} |
|
184 |
RandomAccessFile file = new RandomAccessFile(f, "r"); |
|
185 |
FileChannel channel = file.getChannel(); |
|
186 |
MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); |
|
187 |
buf.order(ByteOrder.LITTLE_ENDIAN); |
|
188 |
int numShapes = buf.getInt(); |
|
189 |
for(int i = 0; i < numShapes; i++){ |
|
190 |
float xmin, ymin, xmax, ymax; |
|
191 |
int shapeIndex; |
|
192 |
xmin = buf.getFloat(); |
|
193 |
ymin = buf.getFloat(); |
|
194 |
xmax = buf.getFloat(); |
|
195 |
ymax = buf.getFloat(); |
|
196 |
shapeIndex = buf.getInt(); |
|
197 |
|
|
198 |
Rectangle jsiRect = new Rectangle(xmin, ymin, xmax, ymax); |
|
199 |
rtree.add(jsiRect, shapeIndex); |
|
200 |
} |
|
201 |
}catch(Exception e){ |
|
202 |
throw new FeatureIndexException(e); |
|
203 |
} |
|
204 |
} |
|
205 |
|
|
206 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
207 |
super.insert(value, fref); |
|
208 |
Envelope env = getEnvelope(value); |
|
209 |
rectangles.put(fref.getOID(), toJsiRect(env)); |
|
210 |
hasChanged = true; |
|
211 |
} |
|
212 |
|
|
213 |
|
|
214 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
215 |
super.delete(value, fref); |
|
216 |
rectangles.remove(fref.getOID()); |
|
217 |
hasChanged = true; |
|
218 |
} |
|
219 |
|
|
220 |
} |
|
221 |
|
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jsi/JSIIndexLibrary.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {{Company}} {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.index.spatial.jsi; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.DALLibrary; |
|
31 |
import org.gvsig.fmap.dal.DALLocator; |
|
32 |
import org.gvsig.fmap.dal.DataTypes; |
|
33 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
34 |
import org.gvsig.tools.library.AbstractLibrary; |
|
35 |
import org.gvsig.tools.library.LibraryException; |
|
36 |
|
|
37 |
public class JSIIndexLibrary extends AbstractLibrary { |
|
38 |
|
|
39 |
@Override |
|
40 |
public void doRegistration() { |
|
41 |
registerAsServiceOf(DALLibrary.class); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
protected void doInitialize() throws LibraryException { |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
protected void doPostInitialize() throws LibraryException { |
|
50 |
DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager(); |
|
51 |
|
|
52 |
if (!man.getFeatureIndexProviders().contains(JSIPersistentRTree.NAME)) { |
|
53 |
man.registerFeatureIndexProvider(JSIPersistentRTree.NAME, "Persistent RTree index based on JSI", |
|
54 |
JSIPersistentRTree.class, DataTypes.GEOMETRY); |
|
55 |
} |
|
56 |
|
|
57 |
if (!man.getFeatureIndexProviders().contains(JSIRTree.NAME)) { |
|
58 |
man.registerFeatureIndexProvider(JSIRTree.NAME, "RTree index based on JSI", |
|
59 |
JSIRTree.class, DataTypes.GEOMETRY); |
|
60 |
} |
|
61 |
} |
|
62 |
} |
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/spatialindex/SPTLIBRTree.java | ||
---|---|---|
1 |
package org.gvsig.fmap.dal.index.spatial.spatialindex; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileNotFoundException; |
|
5 |
import java.io.IOException; |
|
6 |
import java.util.ArrayList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.gvsig.fmap.dal.exception.DataException; |
|
10 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
11 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
12 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
13 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
14 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider; |
|
15 |
import org.gvsig.fmap.geom.Geometry; |
|
16 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
17 |
import org.gvsig.fmap.geom.primitive.Point; |
|
18 |
|
|
19 |
import spatialindex.rtree.RTree; |
|
20 |
import spatialindex.spatialindex.IData; |
|
21 |
import spatialindex.spatialindex.INode; |
|
22 |
import spatialindex.spatialindex.IVisitor; |
|
23 |
import spatialindex.spatialindex.Region; |
|
24 |
import spatialindex.storagemanager.DiskStorageManager; |
|
25 |
import spatialindex.storagemanager.IBuffer; |
|
26 |
import spatialindex.storagemanager.IStorageManager; |
|
27 |
import spatialindex.storagemanager.PropertySet; |
|
28 |
import spatialindex.storagemanager.RandomEvictionsBuffer; |
|
29 |
|
|
30 |
/** |
|
31 |
* <p> |
|
32 |
* RTree spatial index based in spatial index library: <br> |
|
33 |
* http://u-foria.org/marioh/spatialindex/index.html <br> |
|
34 |
* marioh@cs.ucr.edu |
|
35 |
* </p> |
|
36 |
* It has the problem that spatial index file creation is a bit slowly (in |
|
37 |
* comparation with other indexes). |
|
38 |
*/ |
|
39 |
public class SPTLIBRTree extends AbstractFeatureIndexProvider implements |
|
40 |
FeatureIndexProvider { |
|
41 |
|
|
42 |
public static final String NAME = SPTLIBRTree.class.getSimpleName(); |
|
43 |
/** |
|
44 |
* Page size of associated file |
|
45 |
*/ |
|
46 |
private static final int defaultPageSize = 32 * 1024; |
|
47 |
private static final double defaultFillFactor = 0.85d; |
|
48 |
|
|
49 |
/** |
|
50 |
* Size of memory buffer of the index |
|
51 |
*/ |
|
52 |
private static final int BUFFER_SIZE = 25000; |
|
53 |
RTree rtree; |
|
54 |
String fileName; |
|
55 |
IStorageManager diskfile; |
|
56 |
|
|
57 |
public SPTLIBRTree() { |
|
58 |
|
|
59 |
} |
|
60 |
|
|
61 |
public void initialize() throws InitializeException { |
|
62 |
try { |
|
63 |
PropertySet ps = new PropertySet(); |
|
64 |
ps.setProperty("Overwrite", new Boolean(false)); |
|
65 |
// .idx and .dat extensions will be added. |
|
66 |
fileName = |
|
67 |
getFeatureIndexProviderServices().getNewFileName(null, null); |
|
68 |
ps.setProperty("FileName", fileName); |
|
69 |
ps.setProperty("PageSize", new Integer(defaultPageSize)); |
|
70 |
diskfile = new DiskStorageManager(ps); |
|
71 |
rtree = createRTree(); |
|
72 |
} catch (SecurityException e) { |
|
73 |
throw new InitializeException(e); |
|
74 |
} catch (FileNotFoundException e) { |
|
75 |
throw new InitializeException(e); |
|
76 |
} catch (IOException e) { |
|
77 |
throw new InitializeException(e); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* If the spatial index file exists and has content |
|
83 |
*/ |
|
84 |
public boolean exists() { |
|
85 |
return (new File(fileName + ".dat").length() != 0); |
|
86 |
} |
|
87 |
|
|
88 |
class RTreeVisitor implements IVisitor { |
|
89 |
|
|
90 |
ArrayList solution = new ArrayList(); |
|
91 |
|
|
92 |
public void visitNode(INode n) { |
|
93 |
} |
|
94 |
|
|
95 |
public void visitData(IData d) { |
|
96 |
solution.add(new Integer(d.getIdentifier())); |
|
97 |
} |
|
98 |
|
|
99 |
public List getSolution() { |
|
100 |
return solution; |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
public List containtmentQuery(Envelope env) { |
|
105 |
List solution = null; |
|
106 |
Region region = createRegion(env); |
|
107 |
RTreeVisitor visitor = new RTreeVisitor(); |
|
108 |
rtree.containmentQuery(region, visitor); |
|
109 |
solution = visitor.getSolution(); |
|
110 |
return solution; |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* Warn! This RTree implemention doesnt care if 'index' entry has been |
|
115 |
* indexed yet |
|
116 |
*/ |
|
117 |
public void insert(Envelope env, int index) { |
|
118 |
rtree.insertData(null, createRegion(env), index); |
|
119 |
} |
|
120 |
|
|
121 |
private Region createRegion(Envelope env) { |
|
122 |
double[] x = new double[2]; |
|
123 |
double[] y = new double[2]; |
|
124 |
x[0] = env.getLowerCorner().getX(); |
|
125 |
y[0] = env.getLowerCorner().getY(); |
|
126 |
x[1] = env.getUpperCorner().getX(); |
|
127 |
y[1] = env.getUpperCorner().getY(); |
|
128 |
return new Region(x, y); |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Looks for N indexes nearest to the specified rect. |
|
133 |
* |
|
134 |
* @param numberOfNearest |
|
135 |
* @param rect |
|
136 |
* @return |
|
137 |
*/ |
|
138 |
public List findNNearest(int numberOfNearest, Envelope env) { |
|
139 |
List solution = null; |
|
140 |
Region region = createRegion(env); |
|
141 |
RTreeVisitor visitor = new RTreeVisitor(); |
|
142 |
rtree.nearestNeighborQuery(numberOfNearest, region, visitor); |
|
143 |
solution = visitor.getSolution(); |
|
144 |
return solution; |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* Looks for the N indexes nearest to the specified point |
|
149 |
* |
|
150 |
* @param numberOfNearest |
|
151 |
* @param point |
|
152 |
* @return |
|
153 |
*/ |
|
154 |
public List findNNearest(int numberOfNearest, Point point) { |
|
155 |
List solution = null; |
|
156 |
spatialindex.spatialindex.Point sptPoint = |
|
157 |
new spatialindex.spatialindex.Point(new double[] { point.getX(), |
|
158 |
point.getY() }); |
|
159 |
RTreeVisitor visitor = new RTreeVisitor(); |
|
160 |
rtree.nearestNeighborQuery(numberOfNearest, sptPoint, visitor); |
|
161 |
solution = visitor.getSolution(); |
|
162 |
return solution; |
|
163 |
} |
|
164 |
|
|
165 |
public void flush() { |
|
166 |
rtree.flush(); |
|
167 |
} |
|
168 |
|
|
169 |
private RTree createRTree() { |
|
170 |
// applies a main memory random buffer on top of the persistent |
|
171 |
// storage manager |
|
172 |
IBuffer buffer = |
|
173 |
new RandomEvictionsBuffer(diskfile, BUFFER_SIZE, false); |
|
174 |
|
|
175 |
// Create a new, empty, RTree with dimensionality 2, minimum load |
|
176 |
// 70%, using "file" as |
|
177 |
// the StorageManager and the RSTAR splitting policy. |
|
178 |
PropertySet ps2 = new PropertySet(); |
|
179 |
|
|
180 |
Double f = new Double(defaultFillFactor); |
|
181 |
ps2.setProperty("FillFactor", f); |
|
182 |
|
|
183 |
Integer i = new Integer(2); |
|
184 |
ps2.setProperty("Dimension", i); |
|
185 |
|
|
186 |
File file = new File(fileName + ".dat"); |
|
187 |
if (file.length() != 0) { |
|
188 |
ps2.setProperty("IndexIdentifier", new Integer(1)); |
|
189 |
} |
|
190 |
return new RTree(ps2, buffer); |
|
191 |
} |
|
192 |
|
|
193 |
public void load(File f) throws FeatureIndexException { |
|
194 |
createRTree(); |
|
195 |
} |
|
196 |
|
|
197 |
public void flush(File f) throws FeatureIndexException { |
|
198 |
flush(); |
|
199 |
} |
|
200 |
|
|
201 |
public void close() { |
|
202 |
} |
|
203 |
|
|
204 |
public File getFile() { |
|
205 |
return new File(fileName + ".dat"); |
|
206 |
} |
|
207 |
|
|
208 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
209 |
rtree.deleteData(createRegion(((Geometry) value).getEnvelope()), |
|
210 |
((Integer) fref.getOID()).intValue()); |
|
211 |
} |
|
212 |
|
|
213 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
214 |
Envelope env = null; |
|
215 |
if (value instanceof Envelope) { |
|
216 |
env = (Envelope) value; |
|
217 |
} else |
|
218 |
if (value instanceof Geometry) { |
|
219 |
env = ((Geometry) value).getEnvelope(); |
|
220 |
} |
|
221 |
rtree.insertData(null, createRegion(env), |
|
222 |
((Integer) fref.getOID()).intValue()); |
|
223 |
} |
|
224 |
|
|
225 |
public List match(Object value) throws FeatureIndexException { |
|
226 |
List solution = null; |
|
227 |
Envelope env = null; |
|
228 |
if (value instanceof Envelope) { |
|
229 |
env = (Envelope) value; |
|
230 |
} else |
|
231 |
if (value instanceof Geometry) { |
|
232 |
env = ((Geometry) value).getEnvelope(); |
|
233 |
} |
|
234 |
Region region = createRegion(env); |
|
235 |
RTreeVisitor visitor = new RTreeVisitor(); |
|
236 |
rtree.intersectionQuery(region, visitor); |
|
237 |
solution = visitor.getSolution(); |
|
238 |
return new LongList(solution); |
|
239 |
} |
|
240 |
|
|
241 |
public List match(Object min, Object max) { |
|
242 |
throw new UnsupportedOperationException(); |
|
243 |
} |
|
244 |
|
|
245 |
public List nearest(int count, Object value) throws FeatureIndexException { |
|
246 |
if (value instanceof Envelope) { |
|
247 |
return this.findNNearest(count, (Envelope) value); |
|
248 |
} else |
|
249 |
if (value instanceof Point) { |
|
250 |
return this.findNNearest(count, (Point) value); |
|
251 |
} else |
|
252 |
if (value instanceof Geometry) { |
|
253 |
return this.findNNearest(count, |
|
254 |
((Geometry) value).getEnvelope()); |
|
255 |
} else { |
|
256 |
throw new IllegalArgumentException( |
|
257 |
"value must be either an Envelope or either a Point2D"); |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
public boolean isMatchSupported() { |
|
262 |
return true; |
|
263 |
} |
|
264 |
|
|
265 |
public boolean isNearestSupported() { |
|
266 |
return true; |
|
267 |
} |
|
268 |
|
|
269 |
public boolean isNearestToleranceSupported() { |
|
270 |
return false; |
|
271 |
} |
|
272 |
|
|
273 |
public boolean isRangeSupported() { |
|
274 |
// TODO Auto-generated method stub |
|
275 |
return false; |
|
276 |
} |
|
277 |
|
|
278 |
public List nearest(int count, Object value, Object tolerance) |
|
279 |
throws FeatureIndexException { |
|
280 |
throw new UnsupportedOperationException(); |
|
281 |
} |
|
282 |
|
|
283 |
public List range(Object value1, Object value2) |
|
284 |
throws FeatureIndexException { |
|
285 |
throw new UnsupportedOperationException(); |
|
286 |
} |
|
287 |
|
|
288 |
public void clear() throws DataException { |
|
289 |
rtree = createRTree(); |
|
290 |
} |
|
291 |
} |
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/spatialindex/SPTLIBIndexLibrary.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {{Company}} {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
package org.gvsig.fmap.dal.index.spatial.spatialindex; |
|
29 |
|
|
30 |
import org.gvsig.fmap.dal.DALLibrary; |
|
31 |
import org.gvsig.fmap.dal.DALLocator; |
|
32 |
import org.gvsig.fmap.dal.DataTypes; |
|
33 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
34 |
import org.gvsig.tools.library.AbstractLibrary; |
|
35 |
import org.gvsig.tools.library.LibraryException; |
|
36 |
|
|
37 |
public class SPTLIBIndexLibrary extends AbstractLibrary { |
|
38 |
|
|
39 |
@Override |
|
40 |
public void doRegistration() { |
|
41 |
registerAsServiceOf(DALLibrary.class); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
protected void doInitialize() throws LibraryException { |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
protected void doPostInitialize() throws LibraryException { |
|
50 |
DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager(); |
|
51 |
|
|
52 |
|
|
53 |
if (!man.getFeatureIndexProviders().contains(SPTLIBRTree.NAME)) { |
|
54 |
man.registerFeatureIndexProvider(SPTLIBRTree.NAME, "RTree index based on spatialindex", |
|
55 |
SPTLIBRTree.class, DataTypes.GEOMETRY); |
|
56 |
} |
|
57 |
} |
|
58 |
} |
|
59 |
|
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/gt2/GT2Quadtree.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 16-may-2006 |
|
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: QuadtreeGt2.java 10627 2007-03-06 17:10:21Z caballero $ |
|
47 |
* $Log$ |
|
48 |
* Revision 1.3 2007-03-06 17:08:59 caballero |
|
49 |
* Exceptions |
|
50 |
* |
|
51 |
* Revision 1.2 2006/11/29 19:27:59 azabala |
|
52 |
* bug fixed (caused when we query for a bbox which is greater or equal to a layer bbox) |
|
53 |
* |
|
54 |
* Revision 1.1 2006/05/24 21:58:04 azabala |
|
55 |
* *** empty log message *** |
|
56 |
* |
|
57 |
* |
|
58 |
*/ |
|
59 |
package org.gvsig.fmap.dal.index.spatial.gt2; |
|
60 |
|
|
61 |
import java.io.File; |
|
62 |
import java.io.IOException; |
|
63 |
import java.util.List; |
|
64 |
import java.util.Stack; |
|
65 |
|
|
66 |
import com.vividsolutions.jts.geom.Envelope; |
|
67 |
|
|
68 |
import org.geotools.index.quadtree.Node; |
|
69 |
import org.geotools.index.quadtree.QuadTree; |
|
70 |
import org.geotools.index.quadtree.StoreException; |
|
71 |
import org.geotools.index.quadtree.fs.FileSystemIndexStore; |
|
72 |
import org.geotools.index.quadtree.fs.IndexHeader; |
|
73 |
|
|
74 |
import org.gvsig.fmap.dal.exception.DataException; |
|
75 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
76 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
77 |
import org.gvsig.fmap.dal.feature.exception.FeatureIndexException; |
|
78 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
|
79 |
import org.gvsig.fmap.dal.feature.spi.index.AbstractFeatureIndexProvider; |
|
80 |
import org.gvsig.fmap.dal.feature.spi.index.FeatureIndexProvider; |
|
81 |
import org.gvsig.fmap.geom.Geometry; |
|
82 |
import org.gvsig.fmap.geom.primitive.Point; |
|
83 |
import org.gvsig.tools.exception.BaseException; |
|
84 |
|
|
85 |
/** |
|
86 |
* This Quadtree spatial index implementation is based in a fork of |
|
87 |
* org.geotools.index.quadtree.Quadtree implementation. <br> |
|
88 |
* This implementation offers us: |
|
89 |
* <ol> |
|
90 |
* <li>Persistence of spatial index</li> |
|
91 |
* </ol> |
|
92 |
* We had to fork geotools quadtree for many reasons: |
|
93 |
* <ol> |
|
94 |
* <li>It was strongly dependent of SHP format, so it returned not only a num of |
|
95 |
* rectangle, it also returned byte offset of this rectangle in shp file</li> |
|
96 |
* <li>Query artifact wasnt run well at all</li> |
|
97 |
* </ol> |
|
98 |
* |
|
99 |
* @author azabala |
|
100 |
* |
|
101 |
*/ |
|
102 |
public class GT2Quadtree extends AbstractFeatureIndexProvider implements |
|
103 |
FeatureIndexProvider { |
|
104 |
|
|
105 |
public static final String NAME = GT2Quadtree.class.getSimpleName(); |
|
106 |
/** |
|
107 |
* Geotools quadtree implementation |
|
108 |
*/ |
|
109 |
QuadTree quadtree; |
|
110 |
/** |
|
111 |
* Persistent storage |
|
112 |
*/ |
|
113 |
String fileName; |
|
114 |
/** |
|
115 |
* Spatial index file extension |
|
116 |
*/ |
|
117 |
final String qExt = ".qix"; |
|
118 |
/** |
|
119 |
* qix format has many versions, and allows different byte orders. |
|
120 |
*/ |
|
121 |
String byteOrder; |
|
122 |
/** |
|
123 |
* Bounds of the layer to index |
|
124 |
*/ |
|
125 |
// Envelope bounds; |
|
126 |
/** |
|
127 |
* Number of records of the layer to index |
|
128 |
*/ |
|
129 |
// int numRecs = 0; |
|
130 |
|
|
131 |
boolean inMemory = false; |
|
132 |
|
|
133 |
public GT2Quadtree() { |
|
134 |
|
|
135 |
} |
|
136 |
|
|
137 |
@Override |
|
138 |
public void initialize() throws InitializeException { |
|
139 |
try { |
|
140 |
File file = |
|
141 |
File.createTempFile(getFeatureStore().getName(), ".qix"); |
|
142 |
this.fileName = file.getAbsolutePath(); |
|
143 |
this.byteOrder = "NM"; |
|
144 |
this.quadtree = createQuadTree(); |
|
145 |
if (exists()) { |
|
146 |
load(); |
|
147 |
} |
|
148 |
} catch (IOException e) { |
|
149 |
throw new InitializeException(e); |
|
150 |
} catch (BaseException e) { |
|
151 |
throw new InitializeException(e); |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
/** |
|
156 |
* @return |
|
157 |
* @throws DataException |
|
158 |
*/ |
|
159 |
private QuadTree createQuadTree() throws DataException { |
|
160 |
org.gvsig.fmap.geom.primitive.Envelope env = |
|
161 |
getFeatureStore().getEnvelope(); |
|
162 |
int featureCount = (int) getFeatureStore().getFeatureCount(); |
|
163 |
return new QuadTree(featureCount, toJtsEnvelope(env)); |
|
164 |
} |
|
165 |
|
|
166 |
/** |
|
167 |
* If the spatial index file exists and has content |
|
168 |
*/ |
|
169 |
public boolean exists() { |
|
170 |
return (new File(fileName).length() != 0); |
|
171 |
} |
|
172 |
|
|
173 |
public void load() throws FeatureIndexException { |
|
174 |
if (quadtree == null) { |
|
175 |
load(new File(fileName)); |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
public void load(File f) throws FeatureIndexException { |
|
180 |
try { |
|
181 |
FileSystemIndexStore store = new FileSystemIndexStore(f); |
|
182 |
quadtree = store.load(); |
|
183 |
this.fileName = f.getAbsolutePath(); |
|
184 |
} catch (StoreException e) { |
|
185 |
throw new FeatureIndexException(e); |
|
186 |
} |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* Inserts an object in the index |
|
191 |
*/ |
|
192 |
public void insert(org.gvsig.fmap.geom.primitive.Envelope env, int index) { |
|
193 |
if (env == null) { |
|
194 |
throw new IllegalArgumentException("Envelope cannot be null"); |
|
195 |
} |
|
196 |
Envelope e = toJtsEnvelope(env); |
|
197 |
if (e == null) { |
|
198 |
throw new IllegalStateException( |
|
199 |
"JTS Envelope conversion returns null"); |
|
200 |
} |
|
201 |
System.out.println("recno=" + index); |
|
202 |
if (quadtree == null) { |
|
203 |
throw new IllegalStateException("quadtree is null"); |
|
204 |
} |
|
205 |
try { |
|
206 |
quadtree.insert(index, toJtsEnvelope(env)); |
|
207 |
} catch (StoreException se) { |
|
208 |
// TODO Auto-generated catch block |
|
209 |
se.printStackTrace(); |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
public void delete(org.gvsig.fmap.geom.primitive.Envelope env, int index) { |
|
214 |
if (inMemory) { |
|
215 |
quadtree.delete(toJtsEnvelope(env), index); |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
public Envelope toJtsEnvelope(org.gvsig.fmap.geom.primitive.Envelope env) { |
|
220 |
if (env == null) { |
|
221 |
return null; |
|
222 |
} |
|
223 |
Point min = env.getLowerCorner(); |
|
224 |
Point max = env.getUpperCorner(); |
|
225 |
|
|
226 |
return new Envelope(min.getX(), max.getX(), min.getY(), max.getY()); |
|
227 |
} |
|
228 |
|
|
229 |
/** |
|
230 |
* |
|
231 |
* @throws StoreException |
|
232 |
* @deprecated |
|
233 |
*/ |
|
234 |
@Deprecated |
|
235 |
void openQuadTree() throws StoreException { |
|
236 |
if (quadtree == null) { |
|
237 |
File file = new File(this.fileName); |
|
238 |
// Intento de cargar todo el quadtree en memoria |
|
239 |
FileSystemIndexStore store = new FileSystemIndexStore(file); |
|
240 |
quadtree = store.load(); |
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
void openQuadTreeInMemory() throws StoreException { |
|
245 |
if (quadtree == null) { |
|
246 |
File file = new File(fileName); |
|
247 |
// Intento de cargar todo el quadtree en memoria |
|
248 |
FileSystemIndexStore store = new FileSystemIndexStore(file); |
|
249 |
QuadTree filequadtree = store.load(); |
|
250 |
quadtree = |
|
251 |
new QuadTree(filequadtree.getNumShapes(), |
|
252 |
filequadtree.getMaxDepth(), filequadtree.getRoot() |
|
253 |
.getBounds()); |
|
254 |
Stack nodes = new Stack(); |
|
255 |
nodes.push(filequadtree.getRoot()); |
|
256 |
while (nodes.size() != 0) { |
|
257 |
Node node = (Node) nodes.pop(); |
|
258 |
Envelope nodeEnv = node.getBounds(); |
|
259 |
int[] shapeIds = node.getShapesId(); |
|
260 |
for (int i = 0; i < shapeIds.length; i++) { |
|
261 |
quadtree.insert(shapeIds[i], nodeEnv); |
|
262 |
} |
|
263 |
int numSubnodes = node.getNumSubNodes(); |
|
264 |
for (int i = 0; i < numSubnodes; i++) { |
|
265 |
nodes.push(node.getSubNode(i)); |
|
266 |
} |
|
267 |
}// while |
|
268 |
filequadtree.close(); |
|
269 |
} |
|
270 |
} |
|
271 |
|
|
272 |
public void flush() throws FeatureIndexException { |
|
273 |
flush(new File(fileName)); |
|
274 |
} |
|
275 |
|
|
276 |
public void flush(File f) throws FeatureIndexException { |
|
277 |
byte order = 0; |
|
278 |
if ((byteOrder == null) || byteOrder.equalsIgnoreCase("NM")) { |
|
279 |
order = IndexHeader.NEW_MSB_ORDER; |
|
280 |
} else |
|
281 |
if (byteOrder.equalsIgnoreCase("NL")) { |
|
282 |
order = IndexHeader.NEW_LSB_ORDER; |
|
283 |
} |
|
284 |
FileSystemIndexStore store = new FileSystemIndexStore(f, order); |
|
285 |
try { |
|
286 |
store.store(quadtree); |
|
287 |
this.fileName = f.getAbsolutePath(); |
|
288 |
} catch (StoreException e) { |
|
289 |
throw new FeatureIndexException(e); |
|
290 |
} |
|
291 |
} |
|
292 |
|
|
293 |
public File getFile() { |
|
294 |
return new File(this.fileName); |
|
295 |
} |
|
296 |
|
|
297 |
private FeatureStore getFeatureStore() { |
|
298 |
return getFeatureIndexProviderServices() |
|
299 |
.getFeatureStoreProviderServices().getFeatureStore(); |
|
300 |
} |
|
301 |
|
|
302 |
public void delete(Object value, FeatureReferenceProviderServices fref) { |
|
303 |
|
|
304 |
if (!isCompatibleOID(fref.getOID())) { |
|
305 |
throw new IllegalArgumentException( |
|
306 |
"OID not compatible. Must be an instance of Number within the Integer range."); |
|
307 |
} |
|
308 |
|
|
309 |
delete(((org.gvsig.fmap.geom.Geometry) value).getEnvelope(), |
|
310 |
((Number) fref.getOID()).intValue()); |
|
311 |
} |
|
312 |
|
|
313 |
public void insert(Object value, FeatureReferenceProviderServices fref) { |
|
314 |
|
|
315 |
if (!isCompatibleOID(fref.getOID())) { |
|
316 |
throw new IllegalArgumentException( |
|
317 |
"OID not compatible. Must be an instance of Number within the Integer range."); |
|
318 |
} |
|
319 |
|
|
320 |
insert(((org.gvsig.fmap.geom.Geometry) value).getEnvelope(), |
|
321 |
((Number) fref.getOID()).intValue()); |
|
322 |
} |
|
323 |
|
|
324 |
public List match(Object value) throws FeatureIndexException { |
|
325 |
if (quadtree == null) { |
|
326 |
throw new IllegalStateException("This quadtree is null."); |
|
327 |
} |
|
328 |
if (value == null) { |
|
329 |
throw new IllegalArgumentException("Envelope cannot be null."); |
|
330 |
} |
|
331 |
if (!(value instanceof org.gvsig.fmap.geom.primitive.Envelope)) { |
|
332 |
throw new IllegalArgumentException("Not an envelope."); |
|
333 |
} |
|
334 |
org.gvsig.fmap.geom.primitive.Envelope env = null; |
|
335 |
if (value instanceof org.gvsig.fmap.geom.primitive.Envelope) { |
|
336 |
env = (org.gvsig.fmap.geom.primitive.Envelope) value; |
|
337 |
} else |
|
338 |
if (value instanceof Geometry) { |
|
339 |
env = ((Geometry) value).getEnvelope(); |
|
340 |
} |
|
341 |
return new LongList(quadtree.query(toJtsEnvelope(env))); |
|
342 |
} |
|
343 |
|
|
344 |
public List nearest(int count, Object value) throws FeatureIndexException { |
|
345 |
throw new UnsupportedOperationException(); |
|
346 |
} |
|
347 |
|
|
348 |
public boolean isMatchSupported() { |
|
349 |
return true; |
|
350 |
} |
|
351 |
|
|
352 |
public boolean isNearestSupported() { |
|
353 |
return false; |
|
354 |
} |
|
355 |
|
|
356 |
public boolean isNearestToleranceSupported() { |
|
357 |
return false; |
|
358 |
} |
|
359 |
|
|
360 |
public boolean isRangeSupported() { |
|
361 |
return false; |
|
362 |
} |
|
363 |
|
|
364 |
public List nearest(int count, Object value, Object tolerance) |
|
365 |
throws FeatureIndexException { |
|
366 |
throw new UnsupportedOperationException(); |
|
367 |
} |
|
368 |
|
|
369 |
public List range(Object value1, Object value2) |
|
370 |
throws FeatureIndexException { |
|
371 |
throw new UnsupportedOperationException(); |
|
372 |
} |
|
373 |
|
|
374 |
/** |
|
375 |
* Indicates whether the given OID's type is compatible |
|
376 |
* with this provider |
|
377 |
* |
|
378 |
* @param oid |
|
379 |
* |
|
380 |
* @return |
|
381 |
* true if this index provider supports the given oid type |
|
382 |
*/ |
|
383 |
private boolean isCompatibleOID(Object oid) { |
|
384 |
if (!(oid instanceof Number)) { |
|
385 |
return false; |
|
386 |
} |
|
387 |
|
|
388 |
long num = ((Number) oid).longValue(); |
|
389 |
|
|
390 |
if ((num > Integer.MAX_VALUE) || (num < Integer.MIN_VALUE)) { |
|
391 |
return false; |
|
392 |
} |
|
393 |
|
|
394 |
return true; |
|
395 |
} |
|
396 |
|
|
397 |
public void clear() throws DataException { |
|
398 |
this.quadtree = createQuadTree(); |
|
399 |
} |
|
400 |
|
|
401 |
} |
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/gt2/Gt2IndexLibrary.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
|
|
23 |
/* |
|
24 |
* AUTHORS (In addition to CIT): |
|
25 |
* 2008 {{Company}} {{Task}} |
|
26 |
*/ |
|
27 |
|
|
28 |
|
|
29 |
package org.gvsig.fmap.dal.index.spatial.gt2; |
|
30 |
|
|
31 |
import org.gvsig.fmap.dal.DALLibrary; |
|
32 |
import org.gvsig.fmap.dal.DALLocator; |
|
33 |
import org.gvsig.fmap.dal.DataTypes; |
|
34 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
35 |
import org.gvsig.tools.library.AbstractLibrary; |
|
36 |
import org.gvsig.tools.library.LibraryException; |
|
37 |
|
|
38 |
public class Gt2IndexLibrary extends AbstractLibrary { |
|
39 |
|
|
40 |
@Override |
|
41 |
public void doRegistration() { |
|
42 |
registerAsServiceOf(DALLibrary.class); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
protected void doInitialize() throws LibraryException { |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
protected void doPostInitialize() throws LibraryException { |
|
51 |
DataManagerProviderServices man = (DataManagerProviderServices) DALLocator.getDataManager(); |
|
52 |
|
|
53 |
if (!man.getFeatureIndexProviders().contains(GT2Quadtree.NAME)) { |
|
54 |
man.registerFeatureIndexProvider(GT2Quadtree.NAME, "Quadtree index based on Geotools 2", |
|
55 |
GT2Quadtree.class, DataTypes.GEOMETRY); |
|
56 |
} |
|
57 |
} |
|
58 |
} |
tags/v2_0_0_Build_2049/libraries/libFMap_dalindex/src/org/gvsig/fmap/dal/index/spatial/jts/JTSQuadtree.java | ||
---|---|---|
1 |
/* |
|
2 |
* Created on 28-abr-2006 |
|
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: QuadtreeJts.java 11288 2007-04-19 17:32:50Z azabala $ |
|
47 |
* $Log$ |
|
48 |
* Revision 1.2 2007-04-19 17:32:50 azabala |
|
49 |
* new constructor (fmap spatial index from an existing jts spatial index) |
|
50 |
* |
|
51 |
* Revision 1.1 2006/05/01 18:38:41 azabala |
|
52 |
* primera version en cvs del api de indices espaciales |
|
53 |
* |
|
54 |
* |
|
55 |
*/ |
|
56 |
package org.gvsig.fmap.dal.index.spatial.jts; |
|
57 |
|
|
58 |
import java.util.List; |
|
59 |
|
|
60 |
import com.vividsolutions.jts.geom.Envelope; |
|
61 |
import com.vividsolutions.jts.index.quadtree.Quadtree; |
|
62 |
|
|
63 |
import org.gvsig.fmap.dal.exception.DataException; |
Also available in: Unified diff