Search in sources :

Example 6 with RTree

use of rtree.RTree in project OsmAnd-tools by osmandapp.

the class AbstractIndexPartCreator method packRtreeFile.

public static RTree packRtreeFile(RTree tree, String nonPackFileName, String packFileName) throws IOException {
    try {
        // $NON-NLS-1$
        assert rtree.Node.MAX < 50 : "It is better for search performance";
        tree.flush();
        File file = new File(packFileName);
        if (file.exists()) {
            file.delete();
        }
        long rootIndex = tree.getFileHdr().getRootIndex();
        if (!nodeIsLastSubTree(tree, rootIndex)) {
            // there is a bug for small files in packing method
            new Pack().packTree(tree, packFileName);
            tree.getFileHdr().getFile().close();
            file = new File(nonPackFileName);
            file.delete();
            return new RTree(packFileName);
        }
    } catch (RTreeException e) {
        // $NON-NLS-1$
        log.error("Error flushing", e);
        throw new IOException(e);
    }
    return tree;
}
Also used : RTreeException(rtree.RTreeException) RTree(rtree.RTree) IOException(java.io.IOException) Pack(rtree.Pack) File(java.io.File)

Example 7 with RTree

use of rtree.RTree in project OsmAnd-tools by osmandapp.

the class ObfFileInMemory method writeMapData.

private void writeMapData(BinaryMapIndexWriter writer, MapZoomPair mapZoomPair, TLongObjectHashMap<BinaryMapDataObject> objects, File fileToWrite, boolean doNotSimplify) throws IOException, RTreeException {
    File nonpackRtree = new File(fileToWrite.getParentFile(), "nonpack" + mapZoomPair.getMinZoom() + "." + fileToWrite.getName() + ".rtree");
    File packRtree = new File(fileToWrite.getParentFile(), "pack" + mapZoomPair.getMinZoom() + "." + fileToWrite.getName() + ".rtree");
    RTree rtree = null;
    try {
        rtree = new RTree(nonpackRtree.getAbsolutePath());
        for (long key : objects.keys()) {
            BinaryMapDataObject obj = objects.get(key);
            int minX = obj.getPoint31XTile(0);
            int maxX = obj.getPoint31XTile(0);
            int maxY = obj.getPoint31YTile(0);
            int minY = obj.getPoint31YTile(0);
            for (int i = 1; i < obj.getPointsLength(); i++) {
                minX = Math.min(minX, obj.getPoint31XTile(i));
                minY = Math.min(minY, obj.getPoint31YTile(i));
                maxX = Math.max(maxX, obj.getPoint31XTile(i));
                maxY = Math.max(maxY, obj.getPoint31YTile(i));
            }
            try {
                rtree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), obj.getId()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        rtree = AbstractIndexPartCreator.packRtreeFile(rtree, nonpackRtree.getAbsolutePath(), packRtree.getAbsolutePath());
        TLongObjectHashMap<BinaryFileReference> treeHeader = new TLongObjectHashMap<BinaryFileReference>();
        long rootIndex = rtree.getFileHdr().getRootIndex();
        rtree.Node root = rtree.getReadNode(rootIndex);
        Rect rootBounds = IndexUploader.calcBounds(root);
        if (rootBounds != null) {
            writer.startWriteMapLevelIndex(mapZoomPair.getMinZoom(), mapZoomPair.getMaxZoom(), rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
            IndexVectorMapCreator.writeBinaryMapTree(root, rootBounds, rtree, writer, treeHeader);
            IndexUploader.writeBinaryMapBlock(root, rootBounds, rtree, writer, treeHeader, objects, mapZoomPair, doNotSimplify);
            writer.endWriteMapLevelIndex();
        }
    } finally {
        if (rtree != null) {
            RandomAccessFile file = rtree.getFileHdr().getFile();
            file.close();
        }
        nonpackRtree.delete();
        packRtree.delete();
        RTree.clearCache();
    }
}
Also used : Rect(rtree.Rect) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) SQLException(java.sql.SQLException) RTreeException(rtree.RTreeException) IOException(java.io.IOException) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) RandomAccessFile(java.io.RandomAccessFile) RTree(rtree.RTree) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LeafElement(rtree.LeafElement)

Example 8 with RTree

use of rtree.RTree in project OsmAnd-tools by osmandapp.

the class ObfFileInMemory method writeRouteData.

private void writeRouteData(BinaryMapIndexWriter writer, TLongObjectHashMap<RouteDataObject> routeObjs, File fileToWrite) throws IOException, RTreeException, SQLException {
    File nonpackRtree = new File(fileToWrite.getParentFile(), "nonpackroute." + fileToWrite.getName() + ".rtree");
    File packRtree = new File(fileToWrite.getParentFile(), "packroute." + fileToWrite.getName() + ".rtree");
    RTree rtree = null;
    try {
        rtree = new RTree(nonpackRtree.getAbsolutePath());
        for (long key : routeObjs.keys()) {
            RouteDataObject obj = routeObjs.get(key);
            int minX = obj.getPoint31XTile(0);
            int maxX = obj.getPoint31XTile(0);
            int maxY = obj.getPoint31YTile(0);
            int minY = obj.getPoint31YTile(0);
            for (int i = 1; i < obj.getPointsLength(); i++) {
                minX = Math.min(minX, obj.getPoint31XTile(i));
                minY = Math.min(minY, obj.getPoint31YTile(i));
                maxX = Math.max(maxX, obj.getPoint31XTile(i));
                maxY = Math.max(maxY, obj.getPoint31YTile(i));
            }
            try {
                rtree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), obj.getId()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        rtree = AbstractIndexPartCreator.packRtreeFile(rtree, nonpackRtree.getAbsolutePath(), packRtree.getAbsolutePath());
        TLongObjectHashMap<BinaryFileReference> treeHeader = new TLongObjectHashMap<BinaryFileReference>();
        long rootIndex = rtree.getFileHdr().getRootIndex();
        rtree.Node root = rtree.getReadNode(rootIndex);
        Rect rootBounds = IndexUploader.calcBounds(root);
        if (rootBounds != null) {
            IndexRouteCreator.writeBinaryRouteTree(root, rootBounds, rtree, writer, treeHeader, false);
            RouteWriteContext wc = new RouteWriteContext(null, treeHeader, null, routeObjs);
            IndexRouteCreator.writeBinaryMapBlock(root, rootBounds, rtree, writer, wc, false);
        }
    } finally {
        if (rtree != null) {
            RandomAccessFile file = rtree.getFileHdr().getFile();
            file.close();
        }
        nonpackRtree.delete();
        packRtree.delete();
        RTree.clearCache();
    }
}
Also used : Rect(rtree.Rect) RouteWriteContext(net.osmand.data.preparation.IndexRouteCreator.RouteWriteContext) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) SQLException(java.sql.SQLException) RTreeException(rtree.RTreeException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RouteDataObject(net.osmand.binary.RouteDataObject) RTree(rtree.RTree) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LeafElement(rtree.LeafElement)

Aggregations

RTree (rtree.RTree)8 RTreeException (rtree.RTreeException)7 File (java.io.File)6 IOException (java.io.IOException)6 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)4 RandomAccessFile (java.io.RandomAccessFile)4 Rect (rtree.Rect)4 PreparedStatement (java.sql.PreparedStatement)3 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)3 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)2 LeafElement (rtree.LeafElement)2 ZipFile (java.util.zip.ZipFile)1 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)1 RouteDataObject (net.osmand.binary.RouteDataObject)1 BinaryMapIndexWriter (net.osmand.data.preparation.BinaryMapIndexWriter)1 RouteWriteContext (net.osmand.data.preparation.IndexRouteCreator.RouteWriteContext)1 MapRulType (net.osmand.osm.MapRenderingTypes.MapRulType)1 Node (rtree.Node)1