use of org.geotoolkit.index.quadtree.QuadTree in project geotoolkit by Geomatys.
the class ShapeFileIndexer method buildQuadTree.
private int buildQuadTree(final AccessManager locker, final ShapefileReader reader, final Path file, final boolean verbose) throws IOException, StoreException {
byte order = 0;
if ((this.byteOrder == null) || this.byteOrder.equalsIgnoreCase("NM")) {
order = IndexHeader.NEW_MSB_ORDER;
} else if (this.byteOrder.equalsIgnoreCase("NL")) {
order = IndexHeader.NEW_LSB_ORDER;
} else {
throw new StoreException("Asked byte order '" + this.byteOrder + "' must be 'NL' or 'NM'!");
}
final ShxReader shpIndex = locker.getSHXReader(false);
QuadTree tree = null;
int cnt = 0;
int numRecs = shpIndex.getRecordCount();
ShapefileHeader header = reader.getHeader();
Envelope bounds = new Envelope(header.minX(), header.maxX(), header.minY(), header.maxY());
DataReader dr = new IndexDataReader(shpIndex);
tree = new QuadTree(numRecs, max, bounds);
try {
Record rec = null;
while (reader.hasNext()) {
rec = reader.nextRecord();
tree.insert(cnt++, new Envelope(rec.minX, rec.maxX, rec.minY, rec.maxY));
if (verbose && ((cnt % 1000) == 0)) {
System.out.print('.');
}
if (cnt % 100000 == 0)
System.out.print('\n');
}
if (verbose)
System.out.println("done");
FileSystemIndexStore store = new FileSystemIndexStore(file, order);
store.store(tree);
} finally {
tree.close();
}
return cnt;
}
use of org.geotoolkit.index.quadtree.QuadTree in project geotoolkit by Geomatys.
the class FileSystemNodeTest method testNoerror.
/**
* Just test it doesn't raise an error.
* TODO : move tests from shapefile.
*/
@Test
public void testNoerror() throws Exception {
final File file = new File("src/test/resources/org/geotoolkit/index/sample.qix");
final FileSystemIndexStore store = new FileSystemIndexStore(file);
final DataReader reader = new DataReader() {
@Override
public Data read(final int id) throws IOException {
return new DefaultData(DATA_DEFINITION) {
@Override
public String toString() {
return Integer.toString(id);
}
};
}
@Override
public void close() throws IOException {
}
@Override
public void read(int[] ids, Data[] buffer, int size) throws IOException {
for (int i = 0; i < size; i++) {
buffer[i] = read(ids[i]);
}
}
};
final QuadTree tree = store.load();
assertEquals(10, tree.getMaxDepth());
assertEquals(new Envelope(-8.86966023318779, 3.188061808903407, 36.113981340792286, 43.55971524165336), tree.getRoot().getBounds(new Envelope()));
assertEquals(3602, tree.getNumShapes());
final AbstractNode root = tree.getRoot();
for (int i = 0; i < 4; i++) {
root.getSubNode(i);
}
CloseableCollection col = tree.search(reader, new Envelope(-8, 3, 37, 40));
Iterator ite = col.iterator();
while (ite.hasNext()) {
ite.next();
}
}
use of org.geotoolkit.index.quadtree.QuadTree in project geotoolkit by Geomatys.
the class FileSystemIndexStore method load.
/**
* Loads a quadtree stored in a '.qix' file. <b>WARNING:</b> The resulting
* quadtree will be immutable; if you perform an insert, an
* <code>UnsupportedOperationException</code> will be thrown.
*
* @see IndexStore#load(org.geotoolkit.data.shapefile.shp.IndexFile)
*/
@Override
public QuadTree load() throws StoreException {
QuadTree tree = null;
try {
if (QuadTree.LOGGER.isLoggable(Level.FINEST)) {
QuadTree.LOGGER.log(Level.FINEST, "Opening QuadTree {0}", this.file);
}
tree = FileSystemQuadTree.load(file);
QuadTree.LOGGER.finest("QuadTree opened");
} catch (IOException e) {
throw new StoreException(e);
}
return tree;
}
Aggregations