Revision 23803 branches/v2_0_0_prep/libraries/libFMap_spatialindex/src/org/gvsig/fmap/data/index/spatial/gt2/QuadtreeGt2.java
QuadtreeGt2.java | ||
---|---|---|
59 | 59 |
package org.gvsig.fmap.data.index.spatial.gt2; |
60 | 60 |
|
61 | 61 |
import java.io.File; |
62 |
import java.io.IOException; |
|
63 |
import java.util.Iterator; |
|
62 | 64 |
import java.util.List; |
63 | 65 |
import java.util.Stack; |
64 | 66 |
|
... | ... | |
67 | 69 |
import org.geotools.index.quadtree.StoreException; |
68 | 70 |
import org.geotools.index.quadtree.fs.FileSystemIndexStore; |
69 | 71 |
import org.geotools.index.quadtree.fs.IndexHeader; |
72 |
import org.gvsig.fmap.data.ReadException; |
|
73 |
import org.gvsig.fmap.data.feature.Feature; |
|
74 |
import org.gvsig.fmap.data.feature.FeatureID; |
|
70 | 75 |
import org.gvsig.fmap.data.index.IndexException; |
76 |
import org.gvsig.fmap.data.index.IndexParameters; |
|
71 | 77 |
import org.gvsig.fmap.data.index.spatial.AbstractIntBasedSpatialIndex; |
72 | 78 |
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex; |
79 |
import org.gvsig.fmap.geom.Geometry; |
|
80 |
import org.gvsig.tools.exception.BaseException; |
|
73 | 81 |
|
74 | 82 |
import com.vividsolutions.jts.geom.Envelope; |
75 | 83 |
|
... | ... | |
90 | 98 |
* @author azabala |
91 | 99 |
* |
92 | 100 |
*/ |
93 |
public class QuadtreeGt2 extends AbstractIntBasedSpatialIndex implements PersistentSpatialIndex { |
|
101 |
public class QuadtreeGt2 extends AbstractIntBasedSpatialIndex implements |
|
102 |
PersistentSpatialIndex { |
|
94 | 103 |
/** |
95 | 104 |
* Geotools quadtree implementation |
96 | 105 |
*/ |
... | ... | |
110 | 119 |
/** |
111 | 120 |
* Bounds of the layer to index |
112 | 121 |
*/ |
113 |
Envelope bounds; |
|
122 |
//Envelope bounds;
|
|
114 | 123 |
/** |
115 | 124 |
* Number of records of the layer to index |
116 | 125 |
*/ |
117 |
int numRecs = 0; |
|
126 |
//int numRecs = 0;
|
|
118 | 127 |
|
119 | 128 |
boolean inMemory = false; |
120 | 129 |
|
... | ... | |
139 | 148 |
public QuadtreeGt2(String quadtreeFile, String byteOrder, |
140 | 149 |
org.gvsig.fmap.geom.primitive.Envelope bounds, int numRecords, |
141 | 150 |
boolean overwrite) throws IndexException { |
142 |
|
|
151 |
super(null); |
|
143 | 152 |
this.quadtreeFile = quadtreeFile + qExt; |
144 | 153 |
this.byteOrder = byteOrder; |
145 |
this.bounds = toJtsEnvelope(bounds); |
|
146 |
this.numRecs = numRecords; |
|
154 |
//this.bounds = toJtsEnvelope(bounds);
|
|
155 |
//this.numRecs = numRecords;
|
|
147 | 156 |
|
148 | 157 |
if (exists()) { |
149 | 158 |
if (!overwrite) { |
... | ... | |
151 | 160 |
return; |
152 | 161 |
} |
153 | 162 |
} |
154 |
quadtree = new QuadTree(numRecs, this.bounds);
|
|
163 |
quadtree = new QuadTree(numRecords, toJtsEnvelope(bounds));
|
|
155 | 164 |
} |
156 | 165 |
|
166 |
public QuadtreeGt2(IndexParameters params) throws IndexException { |
|
167 |
super(params); |
|
168 |
if (getFeatureStore() == null) |
|
169 |
throw new IllegalArgumentException("FeatureStore cannot be null."); |
|
170 |
if (getFeatureType() == null) |
|
171 |
throw new IllegalArgumentException("FeatureType cannot be null."); |
|
172 |
if (getFeatureAttributeDescriptor() == null) |
|
173 |
throw new IllegalArgumentException( |
|
174 |
"FeatureAttributeDescriptor cannot be null."); |
|
175 |
try { |
|
176 |
// from old constructor |
|
177 |
this.byteOrder = "NM"; |
|
178 |
org.gvsig.fmap.geom.primitive.Envelope env = (org.gvsig.fmap.geom.primitive.Envelope) getFeatureStore().getMetadata().get("extent"); |
|
179 |
int numRecs = getFeatureStore().getDataCollection().size(); |
|
180 |
File file = null; |
|
181 |
if (params.getName() == null) { |
|
182 |
file = File.createTempFile(getFeatureStore().getName() + "-" |
|
183 |
+ getFeatureAttributeDescriptor().getName() + "_IDX", |
|
184 |
".qix"); |
|
185 |
} else { |
|
186 |
file = new File(params.getName() + ".qix"); |
|
187 |
} |
|
188 |
this.quadtreeFile = file.getAbsolutePath(); |
|
189 |
quadtree = new QuadTree(numRecs, toJtsEnvelope(env)); |
|
190 |
} catch (IOException e) { |
|
191 |
throw new IndexException(e); |
|
192 |
} catch (ReadException e) { |
|
193 |
throw new IndexException(e); |
|
194 |
} catch (BaseException e) { |
|
195 |
throw new IndexException(e); |
|
196 |
} |
|
197 |
if (exists()) { |
|
198 |
if (!params.isOverwrite()) { |
|
199 |
load(); |
|
200 |
return; |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
} |
|
205 |
|
|
157 | 206 |
/** |
158 | 207 |
* If the spatial index file exists and has content |
159 | 208 |
*/ |
... | ... | |
296 | 345 |
return new File(this.quadtreeFile); |
297 | 346 |
} |
298 | 347 |
|
348 |
/** |
|
349 |
* Clears and fills again the index with refreshed data |
|
350 |
*/ |
|
351 |
public void rebuild() throws IndexException { |
|
352 |
try { |
|
353 |
getFeatureStore().refresh(); |
|
354 |
int numRecs = getFeatureStore().getDataCollection().size(); |
|
355 |
Envelope bounds = toJtsEnvelope((org.gvsig.fmap.geom.primitive.Envelope) getFeatureStore().getMetadata().get("extent")); |
|
356 |
this.quadtree = new QuadTree(numRecs, bounds); |
|
357 |
Iterator it = getFeatureStore().getDataCollection().iterator(); |
|
358 |
while (it.hasNext()) { |
|
359 |
Feature feat = (Feature) it.next(); |
|
360 |
Geometry geom = (Geometry) feat.get(getFeatureAttributeDescriptor().getName()); |
|
361 |
insert(geom.getEnvelope(), feat.getID()); |
|
362 |
} |
|
363 |
} catch (ReadException e) { |
|
364 |
throw new IndexException(e); |
|
365 |
} catch (BaseException e) { |
|
366 |
throw new IndexException(e); |
|
367 |
} |
|
368 |
|
|
369 |
} |
|
370 |
|
|
299 | 371 |
} |
Also available in: Unified diff