Revision 24033 branches/v2_0_0_prep/libraries/libFMap_spatialindex/src/org/gvsig/fmap/data/index/spatial/gt2/QuadtreeGt2.java

View differences:

QuadtreeGt2.java
69 69
import org.geotools.index.quadtree.StoreException;
70 70
import org.geotools.index.quadtree.fs.FileSystemIndexStore;
71 71
import org.geotools.index.quadtree.fs.IndexHeader;
72
import org.gvsig.fmap.data.ReadException;
72
import org.gvsig.fmap.data.exceptions.InitializeException;
73
import org.gvsig.fmap.data.exceptions.ReadException;
73 74
import org.gvsig.fmap.data.feature.Feature;
74 75
import org.gvsig.fmap.data.feature.FeatureReference;
75
import org.gvsig.fmap.data.index.IndexException;
76
import org.gvsig.fmap.data.index.IndexParameters;
77
import org.gvsig.fmap.data.index.spatial.AbstractIntBasedSpatialIndex;
78
import org.gvsig.fmap.data.index.spatial.PersistentSpatialIndex;
76
import org.gvsig.fmap.data.feature.FeatureStore;
77
import org.gvsig.fmap.data.feature.exceptions.DataIndexException;
78
import org.gvsig.fmap.data.feature.spi.index.AbstractFeatureIndexProvider;
79
import org.gvsig.fmap.data.feature.spi.index.FeatureIndexProvider;
79 80
import org.gvsig.fmap.geom.Geometry;
80 81
import org.gvsig.tools.exception.BaseException;
81 82

  
......
98 99
 * @author azabala
99 100
 * 
100 101
 */
101
public class QuadtreeGt2 extends AbstractIntBasedSpatialIndex implements
102
		PersistentSpatialIndex {
102
public class QuadtreeGt2 extends AbstractFeatureIndexProvider implements FeatureIndexProvider {
103 103
	/**
104 104
	 * Geotools quadtree implementation
105 105
	 */
......
107 107
	/**
108 108
	 * Persistent storage
109 109
	 */
110
	String quadtreeFile;
110
	String fileName;
111 111
	/**
112 112
	 * Spatial index file extension
113 113
	 */
......
126 126
	//int numRecs = 0;
127 127

  
128 128
	boolean inMemory = false;
129

  
130
	/**
131
	 * Constructor. You must say a qix file path, and if you want to overwrite
132
	 * this file if exists. Also, you must specify how many geometries are going
133
	 * to index, and the bounding box of all the geometries.
134
	 * 
135
	 * 
136
	 * @param quadtreeFile
137
	 *            qix file path
138
	 * @param byteOrder
139
	 *            byte order (bigendian, etc)
140
	 * @param bounds
141
	 *            Envelope of all the geometries to index
142
	 * @param numRecords
143
	 *            num of geometries to index
144
	 * @param overwrite
145
	 *            if we want to overwrite a possible existing qix file
146
	 * @throws IndexException
147
	 */
148
	public QuadtreeGt2(String quadtreeFile, String byteOrder,
149
			org.gvsig.fmap.geom.primitive.Envelope bounds, int numRecords,
150
			boolean overwrite) throws IndexException {
151
		super(null);
152
		this.quadtreeFile = quadtreeFile + qExt;
153
		this.byteOrder = byteOrder;
154
		//this.bounds = toJtsEnvelope(bounds);
155
		//this.numRecs = numRecords;
156

  
157
		if (exists()) {
158
			if (!overwrite) {
159
				load();
160
				return;
161
			}
162
		}
163
		quadtree = new QuadTree(numRecords, toJtsEnvelope(bounds));
129
	
130
	public QuadtreeGt2() {
131
		
164 132
	}
165 133

  
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.");
134
	public void initialize() throws InitializeException {		
175 135
		try {
176
			// from old constructor
177
			this.byteOrder = "NM";
136
			File file = File.createTempFile(getFeatureStore().getName(), ".qix");
137
			this.fileName = file.getAbsolutePath();
178 138
			org.gvsig.fmap.geom.primitive.Envelope env = (org.gvsig.fmap.geom.primitive.Envelope) getFeatureStore().getMetadata().get("extent");
179
			int numRecs = getFeatureStore().getDataSet().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));
139
			int featureCount = (int) getFeatureStore().getFeatureSet().getSize();
140
			this.byteOrder = "NM";
141
			quadtree = new QuadTree(featureCount, toJtsEnvelope(env));
142
			if (exists()) {
143
				load();
144
			}			
190 145
		} catch (IOException e) {
191
			throw new IndexException(e);
192
		} catch (ReadException e) {
193
			throw new IndexException(e);
146
			throw new InitializeException(e);
194 147
		} catch (BaseException e) {
195
			throw new IndexException(e);
148
			throw new InitializeException(e);
196 149
		}
197
		if (exists()) {
198
			if (!params.isOverwrite()) {
199
				load();
200
				return;
201
			}
202
		}
203
		
204
	}
205

  
150
	}	
151
	
206 152
	/**
207 153
	 * If the spatial index file exists and has content
208 154
	 */
209 155
	public boolean exists() {
210
		return (new File(quadtreeFile).length() != 0);
156
		return (new File(fileName).length() != 0);
211 157
	}
212 158

  
213
	public void load() throws IndexException {
159
	public void load() throws DataIndexException {
214 160
		if (quadtree == null) {
215
			load(new File(quadtreeFile));
161
			load(new File(fileName));
216 162
		}
217 163
	}
218 164

  
219
	public void load(File f) throws IndexException {
165
	public void load(File f) throws DataIndexException {
220 166
		try {
221 167
			FileSystemIndexStore store = new FileSystemIndexStore(f);
222 168
			quadtree = store.load();
223
			this.quadtreeFile = f.getAbsolutePath();
169
			this.fileName = f.getAbsolutePath();
224 170
		} catch (StoreException e) {
225
			throw new IndexException(e);
171
			throw new DataIndexException(e);
226 172
		}
227 173
	}
228 174

  
......
279 225
	 */
280 226
	void openQuadTree() throws StoreException {
281 227
		if (quadtree == null) {
282
			File file = new File(quadtreeFile);
228
			File file = new File(this.fileName);
283 229
			// Intento de cargar todo el quadtree en memoria
284 230
			FileSystemIndexStore store = new FileSystemIndexStore(file);
285 231
			quadtree = store.load();
......
288 234

  
289 235
	void openQuadTreeInMemory() throws StoreException {
290 236
		if (quadtree == null) {
291
			File file = new File(quadtreeFile);
237
			File file = new File(fileName);
292 238
			// Intento de cargar todo el quadtree en memoria
293 239
			FileSystemIndexStore store = new FileSystemIndexStore(file);
294 240
			QuadTree filequadtree = store.load();
......
312 258
		}
313 259
	}
314 260

  
315
	public void flush() throws IndexException {
316
		flush(new File(quadtreeFile));
261
	public void flush() throws DataIndexException {
262
		flush(new File(fileName));
317 263
	}
318 264

  
319
	public void flush(File f) throws IndexException {
265
	public void flush(File f) throws DataIndexException {
320 266
		byte order = 0;
321 267
		if ((byteOrder == null) || byteOrder.equalsIgnoreCase("NM")) {
322 268
			order = IndexHeader.NEW_MSB_ORDER;
......
326 272
		FileSystemIndexStore store = new FileSystemIndexStore(f, order);
327 273
		try {
328 274
			store.store(quadtree);
329
			this.quadtreeFile = f.getAbsolutePath();
275
			this.fileName = f.getAbsolutePath();
330 276
		} catch (StoreException e) {
331
			throw new IndexException(e);
277
			throw new DataIndexException(e);
332 278
		}
333 279
	}
334 280

  
......
342 288
	}
343 289

  
344 290
	public File getFile() {
345
		return new File(this.quadtreeFile);
291
		return new File(this.fileName);
346 292
	}
347 293

  
348 294
	/**
349 295
	 * Clears and fills again the index with refreshed data
350 296
	 */
351
	public void rebuild() throws IndexException {
297
	public void rebuild() throws DataIndexException {
352 298
		try {
353 299
			getFeatureStore().refresh();
354
			int numRecs = getFeatureStore().getDataSet().size();
300
			int numRecs = (int) getFeatureStore().getFeatureSet().getSize();
355 301
			Envelope bounds = toJtsEnvelope((org.gvsig.fmap.geom.primitive.Envelope) getFeatureStore().getMetadata().get("extent")); 
356 302
			this.quadtree = new QuadTree(numRecs, bounds);
357
			Iterator it = getFeatureStore().getDataSet().iterator();
303
			Iterator it = getFeatureStore().getFeatureSet().iterator();
358 304
			while (it.hasNext()) {
359 305
				Feature feat = (Feature) it.next();
360
				Geometry geom = (Geometry) feat.get(getFeatureAttributeDescriptor().getName());
306
				Geometry geom = (Geometry) feat.get(getFeatureIndexProviderServices().getAttributeNames()[0]);
361 307
				insert(geom.getEnvelope(), feat.getReference());
362
			}			
308
			}
363 309
		} catch (ReadException e) {
364
			throw new IndexException(e);
310
			throw new DataIndexException(e);
365 311
		} catch (BaseException e) {
366
			throw new IndexException(e);
312
			throw new DataIndexException(e);
367 313
		}
368 314
		
369 315
	}
316
	
317
	private FeatureStore getFeatureStore() {
318
		return getFeatureIndexProviderServices().getFeatureStore();
319
	}
370 320

  
321
	public void delete(Object value, FeatureReference fref) {
322
		// TODO Auto-generated method stub
323
		
324
	}
325

  
326
	public void insert(Object value, FeatureReference fref) {
327
		// TODO Auto-generated method stub
328
		
329
	}
330

  
331
	public List match(Object value) {
332
		// TODO Auto-generated method stub
333
		return null;
334
	}
335

  
336
	public List match(Object min, Object max) {
337
		// TODO Auto-generated method stub
338
		return null;
339
	}
340

  
341
	public List nearest(int n, Object value) throws DataIndexException {
342
		// TODO Auto-generated method stub
343
		return null;
344
	}
345

  
371 346
}

Also available in: Unified diff