Search in sources :

Example 11 with RTreeException

use of rtree.RTreeException 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 12 with RTreeException

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

the class IndexCreator method generateIndexes.

public void generateIndexes(File[] readFile, IProgress progress, IOsmStorageFilter addFilter, MapZooms mapZooms, MapRenderingTypesEncoder renderingTypes, Log logMapDataWarn, boolean generateUniqueIds, boolean overwriteIds) throws IOException, SQLException, InterruptedException, XmlPullParserException {
    // }
    if (logMapDataWarn == null) {
        logMapDataWarn = log;
    }
    if (mapZooms == null) {
        mapZooms = MapZooms.getDefault();
    }
    // clear previous results and setting variables
    if (readFile != null && readFile.length > 0 && regionName == null) {
        int i = readFile[0].getName().indexOf('.');
        if (i > -1) {
            regionName = Algorithms.capitalizeFirstLetterAndLowercase(readFile[0].getName().substring(0, i));
        }
    }
    if (renderingTypes == null) {
        renderingTypes = new MapRenderingTypesEncoder(null, regionName);
    }
    this.indexTransportCreator = new IndexTransportCreator();
    this.indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
    this.indexAddressCreator = new IndexAddressCreator(logMapDataWarn);
    this.indexMapCreator = new IndexVectorMapCreator(logMapDataWarn, mapZooms, renderingTypes, zoomWaySmoothness);
    this.indexRouteCreator = new IndexRouteCreator(renderingTypes, logMapDataWarn, generateLowLevel);
    // init address
    indexAddressCreator.initSettings(cityAdminLevel);
    // Main generation method
    try {
        // do not create temp map file and rtree files
        if (recreateOnlyBinaryFile) {
            mapFile = new File(workingDir, getMapFileName());
            File tempDBMapFile = new File(workingDir, getTempMapDBFileName());
            mapConnection = (Connection) getDatabaseConnection(tempDBMapFile.getAbsolutePath(), mapIndexDBDialect);
            mapConnection.setAutoCommit(false);
            try {
                if (indexMap) {
                    indexMapCreator.createRTreeFiles(getRTreeMapIndexPackFileName());
                }
                if (indexRouting) {
                    indexRouteCreator.createRTreeFiles(getRTreeRouteIndexPackFileName());
                }
                if (indexTransport) {
                    indexTransportCreator.createRTreeFile(getRTreeTransportStopsPackFileName());
                }
            } catch (RTreeException e) {
                // $NON-NLS-1$
                log.error("Error flushing", e);
                throw new IOException(e);
            }
        } else {
            // 2. Create index connections and index structure
            createDatabaseIndexesStructure();
            OsmDbAccessor accessor = initDbAccessor(readFile, progress, addFilter, generateUniqueIds, overwriteIds, false);
            // 3. Processing all entries
            // 3.1 write all cities
            writeAllCities(accessor, progress);
            // 3.2 index address relations
            indexRelations(accessor, progress);
            // 3.3 MAIN iterate over all entities
            iterateMainEntities(accessor, progress);
            accessor.closeReadingConnection();
            // do not delete first db connection
            if (accessor.getDbConn() != null) {
                osmDBdialect.commitDatabase(accessor.getDbConn());
                osmDBdialect.closeDatabase(accessor.getDbConn());
            }
            if (deleteOsmDB) {
                osmDBdialect.removeDatabase(dbFile);
            }
            // 3.4 combine all low level ways and simplify them
            if (indexMap || indexRouting) {
                setGeneralProgress(progress, "[90 / 100]");
                if (indexMap) {
                    progress.startTask(Messages.getString("IndexCreator.INDEX_LO_LEVEL_WAYS"), indexMapCreator.getLowLevelWays());
                    indexMapCreator.processingLowLevelWays(progress);
                }
                if (indexRouting) {
                    progress.startTask(Messages.getString("IndexCreator.INDEX_LO_LEVEL_WAYS"), -1);
                    indexRouteCreator.processingLowLevelWays(progress);
                }
            }
            // 4. packing map rtree indexes
            if (indexMap) {
                // $NON-NLS-1$
                setGeneralProgress(progress, "[90 / 100]");
                // $NON-NLS-1$
                progress.startTask(Messages.getString("IndexCreator.PACK_RTREE_MAP"), -1);
                indexMapCreator.packRtreeFiles(getRTreeMapIndexNonPackFileName(), getRTreeMapIndexPackFileName());
            }
            if (indexRouting) {
                indexRouteCreator.packRtreeFiles(getRTreeRouteIndexNonPackFileName(), getRTreeRouteIndexPackFileName());
            }
            if (indexTransport) {
                // $NON-NLS-1$
                setGeneralProgress(progress, "[90 / 100]");
                // $NON-NLS-1$
                progress.startTask(Messages.getString("IndexCreator.PACK_RTREE_TRANSP"), -1);
                indexTransportCreator.packRTree(getRTreeTransportStopsFileName(), getRTreeTransportStopsPackFileName());
            }
        }
        // 5. Writing binary file
        if (indexMap || indexAddress || indexTransport || indexPOI || indexRouting) {
            if (mapFile.exists()) {
                mapFile.delete();
            }
            mapRAFile = new RandomAccessFile(mapFile, "rw");
            BinaryMapIndexWriter writer = new BinaryMapIndexWriter(mapRAFile, lastModifiedDate == null ? System.currentTimeMillis() : lastModifiedDate.longValue());
            if (indexMap) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing map index to binary file...", -1);
                indexMapCreator.writeBinaryMapIndex(writer, regionName);
            }
            if (indexRouting) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing route index to binary file...", -1);
                indexRouteCreator.writeBinaryRouteIndex(mapFile, writer, regionName, generateLowLevel);
            }
            if (indexAddress) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing address index to binary file...", -1);
                indexAddressCreator.writeBinaryAddressIndex(writer, regionName, progress);
            }
            if (indexPOI) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing poi index to binary file...", -1);
                indexPoiCreator.writeBinaryPoiIndex(writer, regionName, progress);
            }
            if (indexTransport) {
                setGeneralProgress(progress, "[95 of 100]");
                progress.startTask("Writing transport index to binary file...", -1);
                indexTransportCreator.writeBinaryTransportIndex(writer, regionName, mapConnection);
            }
            progress.finishTask();
            writer.close();
            mapRAFile.close();
            // $NON-NLS-1$
            log.info("Finish writing binary file");
        }
    } catch (RuntimeException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (SQLException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (IOException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } catch (XmlPullParserException e) {
        // $NON-NLS-1$
        log.error("Log exception", e);
        throw e;
    } finally {
        try {
            indexPoiCreator.commitAndClosePoiFile(lastModifiedDate);
            if (REMOVE_POI_DB) {
                indexPoiCreator.removePoiFile();
            }
            indexAddressCreator.closeAllPreparedStatements();
            indexTransportCreator.commitAndCloseFiles(getRTreeTransportStopsFileName(), getRTreeTransportStopsPackFileName(), deleteDatabaseIndexes);
            indexMapCreator.commitAndCloseFiles(getRTreeMapIndexNonPackFileName(), getRTreeMapIndexPackFileName(), deleteDatabaseIndexes);
            indexRouteCreator.commitAndCloseFiles(getRTreeRouteIndexNonPackFileName(), getRTreeRouteIndexPackFileName(), deleteDatabaseIndexes);
            if (mapConnection != null) {
                mapConnection.commit();
                mapConnection.close();
                mapConnection = null;
                File tempDBFile = new File(workingDir, getTempMapDBFileName());
                if (mapIndexDBDialect.databaseFileExists(tempDBFile) && deleteDatabaseIndexes) {
                    // do not delete it for now
                    mapIndexDBDialect.removeDatabase(tempDBFile);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }
}
Also used : RTreeException(rtree.RTreeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) IndexAddressCreator(net.osmand.data.preparation.address.IndexAddressCreator) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 13 with RTreeException

use of rtree.RTreeException 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 14 with RTreeException

use of rtree.RTreeException 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)

Example 15 with RTreeException

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

the class SdTree method chooseLeaf.

/**
 *     This method is a copy of <code>RTree.chooseLeaf</code> with minor modifications.
 *     In fact there are number of changes , most important is that this method will just not get the new
 *     Node, but also change the parent's (slot node) child pointer.
 *     Remeber that if there are no leaf node associated with a slot selected, this method creates one
 *     returns this new Node after doing the process described above.
 *     but if there is a leaf node present then that node is returned just as in simple rtrees.
 */
private // sd
Node chooseLeaf(// sd
Element elmt, // sd
LongWraper slotIndex) throws RTreeException, IOException {
    /*TODO : we may also have to traverse non seed node, i.e grown nodes.*/
    try {
        // get the root node
        long root = fileHdr.getRootIndex();
        int level = 0;
        // sd
        Node sltNode = chdNodes.getNode(fileHdr.getFile(), fileName, root, fileHdr);
        // repeat till you reach a slot node
        while (sltNode.getElementType() != Node.LEAF_NODE) {
            // (level != slotLvl){
            // get the best fitting rect from the node
            Element nextElmt = sltNode.getLeastEnlargement(elmt);
            if (level == slotLvl) {
                slotIndex.val = sltNode.getNodeIndex();
                if (nextElmt.getPtr() == Node.NOT_DEFINED) {
                    // the first leaf node for this slot node
                    Node rtNode = // sd
                    chdNodes.getNode(// sd
                    fileHdr.getFile(), // sd
                    fileName, // sd
                    sltNode.getNodeIndex(), Node.LEAF_NODE, fileHdr);
                    sltNode.modifyElement(0, rtNode.getNodeIndex());
                    nextElmt.setPtr(rtNode.getNodeIndex());
                    return rtNode;
                }
            }
            // if are here then we are not at a slot that has no childs
            // sd
            sltNode = chdNodes.getNode(fileHdr.getFile(), fileName, nextElmt.getPtr(), fileHdr);
            level++;
        }
        // if we are here then we reached a proper leaf node rather than a slot node
        return sltNode;
    } catch (Exception e) {
        e.printStackTrace();
        throw new RTreeException(e.getMessage());
    }
}
Also used : RTreeException(rtree.RTreeException) Node(rtree.Node) Element(rtree.Element) NonLeafElement(rtree.NonLeafElement) RTreeException(rtree.RTreeException) NodeFullException(rtree.NodeFullException) IOException(java.io.IOException) NodeReadException(rtree.NodeReadException) FileNotFoundException(java.io.FileNotFoundException) IllegalValueException(rtree.IllegalValueException) NodeWriteException(rtree.NodeWriteException)

Aggregations

RTreeException (rtree.RTreeException)15 IOException (java.io.IOException)12 File (java.io.File)8 RTree (rtree.RTree)7 RandomAccessFile (java.io.RandomAccessFile)6 FileNotFoundException (java.io.FileNotFoundException)5 IllegalValueException (rtree.IllegalValueException)5 PreparedStatement (java.sql.PreparedStatement)4 Node (rtree.Node)4 NodeFullException (rtree.NodeFullException)4 NodeReadException (rtree.NodeReadException)4 NodeWriteException (rtree.NodeWriteException)4 Rect (rtree.Rect)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)3 SQLException (java.sql.SQLException)3 LeafElement (rtree.LeafElement)3 Statement (java.sql.Statement)2 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)2 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)2 JSchException (com.jcraft.jsch.JSchException)1