Search in sources :

Example 1 with QuadTree

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;
}
Also used : FileSystemIndexStore(org.geotoolkit.index.quadtree.fs.FileSystemIndexStore) QuadTree(org.geotoolkit.index.quadtree.QuadTree) ShapefileHeader(org.geotoolkit.data.shapefile.shp.ShapefileHeader) DataReader(org.geotoolkit.index.quadtree.DataReader) ShxReader(org.geotoolkit.data.shapefile.shx.ShxReader) Record(org.geotoolkit.data.shapefile.shp.ShapefileReader.Record) Envelope(org.locationtech.jts.geom.Envelope) StoreException(org.geotoolkit.index.quadtree.StoreException) DataStoreException(org.apache.sis.storage.DataStoreException)

Example 2 with QuadTree

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();
    }
}
Also used : DataReader(org.geotoolkit.index.quadtree.DataReader) QuadTree(org.geotoolkit.index.quadtree.QuadTree) AbstractNode(org.geotoolkit.index.quadtree.AbstractNode) CloseableCollection(org.geotoolkit.index.CloseableCollection) Iterator(java.util.Iterator) Envelope(org.locationtech.jts.geom.Envelope) File(java.io.File) DefaultData(org.geotoolkit.index.DefaultData) Test(org.junit.Test)

Example 3 with QuadTree

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;
}
Also used : QuadTree(org.geotoolkit.index.quadtree.QuadTree) IOException(java.io.IOException) StoreException(org.geotoolkit.index.quadtree.StoreException)

Aggregations

QuadTree (org.geotoolkit.index.quadtree.QuadTree)3 DataReader (org.geotoolkit.index.quadtree.DataReader)2 StoreException (org.geotoolkit.index.quadtree.StoreException)2 Envelope (org.locationtech.jts.geom.Envelope)2 File (java.io.File)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 ShapefileHeader (org.geotoolkit.data.shapefile.shp.ShapefileHeader)1 Record (org.geotoolkit.data.shapefile.shp.ShapefileReader.Record)1 ShxReader (org.geotoolkit.data.shapefile.shx.ShxReader)1 CloseableCollection (org.geotoolkit.index.CloseableCollection)1 DefaultData (org.geotoolkit.index.DefaultData)1 AbstractNode (org.geotoolkit.index.quadtree.AbstractNode)1 FileSystemIndexStore (org.geotoolkit.index.quadtree.fs.FileSystemIndexStore)1 Test (org.junit.Test)1