Search in sources :

Example 6 with RTreeException

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

the class IndexTransportCreator method writeBinaryTransportIndex.

public void writeBinaryTransportIndex(BinaryMapIndexWriter writer, String regionName, Connection mapConnection) throws IOException, SQLException {
    try {
        closePreparedStatements(transRouteStat, transRouteStopsStat, transStopsStat, transRouteGeometryStat);
        mapConnection.commit();
        transportStopsTree.flush();
        // allow gc to collect it
        visitedStops = null;
        PreparedStatement selectTransportRouteData = mapConnection.prepareStatement(// $NON-NLS-1$
        "SELECT id, dist, name, name_en, ref, operator, type, color FROM transport_route");
        PreparedStatement selectTransportData = mapConnection.prepareStatement(// $NON-NLS-1$
        "SELECT S.stop, " + // $NON-NLS-1$
        "  A.latitude,  A.longitude, A.name, A.name_en " + // $NON-NLS-1$
        "FROM transport_route_stop S INNER JOIN transport_stop A ON A.id = S.stop WHERE S.route = ? ORDER BY S.ord asc");
        PreparedStatement selectTransportRouteGeometry = mapConnection.prepareStatement("SELECT S.geometry " + // $NON-NLS-1$
        "FROM transport_route_geometry S WHERE S.route = ?");
        writer.startWriteTransportIndex(regionName);
        writer.startWriteTransportRoutes();
        // expect that memory would be enough
        Map<String, Integer> stringTable = createStringTableForTransport();
        Map<Long, Long> transportRoutes = new LinkedHashMap<Long, Long>();
        ResultSet rs = selectTransportRouteData.executeQuery();
        List<TransportStop> directStops = new ArrayList<>();
        List<TransportStop> reverseStops = new ArrayList<>();
        List<byte[]> directGeometry = new ArrayList<>();
        while (rs.next()) {
            long idRoute = rs.getLong(1);
            int dist = rs.getInt(2);
            String routeName = rs.getString(3);
            String routeEnName = rs.getString(4);
            if (routeEnName != null && routeEnName.equals(Junidecode.unidecode(routeName))) {
                routeEnName = null;
            }
            String ref = rs.getString(5);
            String operator = rs.getString(6);
            String type = rs.getString(7);
            String color = rs.getString(8);
            selectTransportData.setLong(1, idRoute);
            ResultSet rset = selectTransportData.executeQuery();
            reverseStops.clear();
            directStops.clear();
            directGeometry.clear();
            while (rset.next()) {
                long idStop = rset.getInt(1);
                String stopName = rset.getString(4);
                String stopEnName = rset.getString(5);
                if (stopEnName != null && stopEnName.equals(Junidecode.unidecode(stopName))) {
                    stopEnName = null;
                }
                TransportStop st = new TransportStop();
                st.setId(idStop);
                st.setName(stopName);
                st.setLocation(rset.getDouble(2), rset.getDouble(3));
                if (stopEnName != null) {
                    st.setEnName(stopEnName);
                }
                directStops.add(st);
            }
            selectTransportRouteGeometry.setLong(1, idRoute);
            rset = selectTransportRouteGeometry.executeQuery();
            while (rset.next()) {
                byte[] bytes = rset.getBytes(1);
                directGeometry.add(bytes);
            }
            writer.writeTransportRoute(idRoute, routeName, routeEnName, ref, operator, type, dist, color, directStops, directGeometry, stringTable, transportRoutes);
        }
        rs.close();
        selectTransportRouteData.close();
        selectTransportData.close();
        writer.endWriteTransportRoutes();
        PreparedStatement selectTransportStop = mapConnection.prepareStatement(// $NON-NLS-1$
        "SELECT A.id,  A.latitude,  A.longitude, A.name, A.name_en FROM transport_stop A where A.id = ?");
        PreparedStatement selectTransportRouteStop = mapConnection.prepareStatement(// $NON-NLS-1$
        "SELECT DISTINCT S.route FROM transport_route_stop S join transport_route R  on R.id = S.route WHERE S.stop = ? ORDER BY R.type, R.ref ");
        long rootIndex = transportStopsTree.getFileHdr().getRootIndex();
        rtree.Node root = transportStopsTree.getReadNode(rootIndex);
        Rect rootBounds = calcBounds(root);
        if (rootBounds != null) {
            writer.startTransportTreeElement(rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
            writeBinaryTransportTree(root, transportStopsTree, writer, selectTransportStop, selectTransportRouteStop, transportRoutes, stringTable);
            writer.endWriteTransportTreeElement();
        }
        selectTransportStop.close();
        selectTransportRouteStop.close();
        writer.writeTransportStringTable(stringTable);
        writer.endWriteTransportIndex();
        writer.flush();
    } catch (RTreeException e) {
        throw new IllegalStateException(e);
    }
}
Also used : Rect(rtree.Rect) RTreeException(rtree.RTreeException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) LinkedHashMap(java.util.LinkedHashMap) ResultSet(java.sql.ResultSet) TransportStop(net.osmand.data.TransportStop)

Example 7 with RTreeException

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

the class IndexTransportCreator method createDatabaseStructure.

public void createDatabaseStructure(Connection conn, DBDialect dialect, String rtreeStopsFileName) throws SQLException, IOException {
    Statement stat = conn.createStatement();
    stat.executeUpdate("create table transport_route (id bigint primary key, type varchar(1024), operator varchar(1024)," + "ref varchar(1024), name varchar(1024), name_en varchar(1024), dist int, color varchar(1024))");
    stat.executeUpdate("create index transport_route_id on transport_route (id)");
    stat.executeUpdate("create table transport_route_stop (stop bigint, route bigint, ord int, primary key (route, ord))");
    stat.executeUpdate("create index transport_route_stop_stop on transport_route_stop (stop)");
    stat.executeUpdate("create index transport_route_stop_route on transport_route_stop (route)");
    stat.executeUpdate("create table transport_route_geometry (geometry bytes, route bigint)");
    stat.executeUpdate("create index transport_route_geometry_route on transport_route_geometry (route)");
    stat.executeUpdate("create table transport_stop (id bigint primary key, latitude double, longitude double, name varchar(1024), name_en varchar(1024))");
    stat.executeUpdate("create index transport_stop_id on transport_stop (id)");
    stat.executeUpdate("create index transport_stop_location on transport_stop (latitude, longitude)");
    // if(dialect == DBDialect.SQLITE){
    // stat.execute("PRAGMA user_version = " + IndexConstants.TRANSPORT_TABLE_VERSION); //$NON-NLS-1$
    // }
    stat.close();
    try {
        File file = new File(rtreeStopsFileName);
        if (file.exists()) {
            file.delete();
        }
        transportStopsTree = new RTree(file.getAbsolutePath());
    } catch (RTreeException e) {
        throw new IOException(e);
    }
    transRouteStat = conn.prepareStatement("insert into transport_route(id, type, operator, ref, name, name_en, dist, color) values(?, ?, ?, ?, ?, ?, ?, ?)");
    transRouteStopsStat = conn.prepareStatement("insert into transport_route_stop(route, stop, ord) values(?, ?, ?)");
    transStopsStat = conn.prepareStatement("insert into transport_stop(id, latitude, longitude, name, name_en) values(?, ?, ?, ?, ?)");
    transRouteGeometryStat = conn.prepareStatement("insert into transport_route_geometry(route, geometry) values(?, ?)");
    pStatements.put(transRouteStat, 0);
    pStatements.put(transRouteStopsStat, 0);
    pStatements.put(transStopsStat, 0);
    pStatements.put(transRouteGeometryStat, 0);
}
Also used : RTreeException(rtree.RTreeException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) RTree(rtree.RTree) IOException(java.io.IOException) File(java.io.File)

Example 8 with RTreeException

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

the class IndexVectorMapCreator method writeBinaryMapIndex.

public void writeBinaryMapIndex(BinaryMapIndexWriter writer, String regionName) throws IOException, SQLException {
    closePreparedStatements(mapBinaryStat, mapLowLevelBinaryStat);
    mapConnection.commit();
    try {
        writer.startWriteMapIndex(regionName);
        // write map encoding rules
        writer.writeMapEncodingRules(renderingTypes.getEncodingRuleTypes());
        PreparedStatement selectData = mapConnection.prepareStatement("SELECT area, coordinates, innerPolygons, types, additionalTypes, name FROM binary_map_objects WHERE id = ?");
        // write map levels and map index
        TLongObjectHashMap<BinaryFileReference> treeHeader = new TLongObjectHashMap<BinaryFileReference>();
        for (int i = 0; i < mapZooms.size(); i++) {
            RTree rtree = mapTree[i];
            long rootIndex = rtree.getFileHdr().getRootIndex();
            rtree.Node root = rtree.getReadNode(rootIndex);
            Rect rootBounds = calcBounds(root);
            if (rootBounds != null) {
                writer.startWriteMapLevelIndex(mapZooms.getLevel(i).getMinZoom(), mapZooms.getLevel(i).getMaxZoom(), rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
                writeBinaryMapTree(root, rootBounds, rtree, writer, treeHeader);
                writeBinaryMapBlock(root, rootBounds, rtree, writer, selectData, treeHeader, new LinkedHashMap<String, Integer>(), new LinkedHashMap<MapRulType, String>(), mapZooms.getLevel(i));
                writer.endWriteMapLevelIndex();
            }
        }
        selectData.close();
        writer.endWriteMapIndex();
        writer.flush();
    } catch (RTreeException e) {
        throw new IllegalStateException(e);
    }
}
Also used : Rect(rtree.Rect) RTreeException(rtree.RTreeException) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) PreparedStatement(java.sql.PreparedStatement) MapRulType(net.osmand.osm.MapRenderingTypes.MapRulType) RTree(rtree.RTree)

Example 9 with RTreeException

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

the class IndexVectorMapCreator method createDatabaseStructure.

public void createDatabaseStructure(Connection mapConnection, DBDialect dialect, String rtreeMapIndexNonPackFileName) throws SQLException, IOException {
    createMapIndexStructure(mapConnection);
    this.mapConnection = mapConnection;
    mapBinaryStat = createStatementMapBinaryInsert(mapConnection);
    mapLowLevelBinaryStat = createStatementLowLevelMapBinaryInsert(mapConnection);
    try {
        mapTree = new RTree[mapZooms.size()];
        for (int i = 0; i < mapZooms.size(); i++) {
            File file = new File(rtreeMapIndexNonPackFileName + i);
            if (file.exists()) {
                file.delete();
            }
            mapTree[i] = new RTree(rtreeMapIndexNonPackFileName + i);
        // very slow
        // mapTree[i].getFileHdr().setBufferPolicy(true);
        }
    } catch (RTreeException e) {
        throw new IOException(e);
    }
    pStatements.put(mapBinaryStat, 0);
    pStatements.put(mapLowLevelBinaryStat, 0);
}
Also used : RTreeException(rtree.RTreeException) RTree(rtree.RTree) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 10 with RTreeException

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

the class IndexUploader method checkfileAndGetDescription.

private String checkfileAndGetDescription(File mainFile) throws OneFileException, IOException, RTreeException {
    String fileName = mainFile.getName();
    boolean srtmFile = mainFile.getName().contains(".srtm");
    boolean roadFile = mainFile.getName().contains(".road");
    boolean wikiFile = mainFile.getName().contains(".wiki");
    boolean tourFile = fileName.endsWith(IndexConstants.TOUR_INDEX_EXT) || fileName.endsWith(IndexConstants.TOUR_INDEX_EXT_ZIP);
    boolean worldFile = fileName.toLowerCase().contains("basemap") || fileName.toLowerCase().contains("world");
    boolean regionFile = !srtmFile && !roadFile && !wikiFile && !tourFile && !worldFile;
    if (srtmFile != this.srtmProcess) {
        return null;
    }
    if (tourFile != this.tourProcess) {
        return null;
    }
    if (roadFile != this.roadProcess) {
        return null;
    }
    if (wikiFile != this.wikiProcess) {
        return null;
    }
    if (regionFile) {
        extractRoadOnlyFile(mainFile, new File(mainFile.getParentFile(), fileName.replace(IndexConstants.BINARY_MAP_INDEX_EXT, IndexConstants.BINARY_ROAD_MAP_INDEX_EXT)));
    }
    if (tourFile) {
        File fl = new File(mainFile, "inventory.xml");
        if (!fl.exists()) {
            System.err.println("inventory.xml doesn't exist " + fl.getAbsolutePath());
            return null;
        }
        try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fl);
            return ((Element) doc.getElementsByTagName("shortDescription").item(0)).getTextContent();
        } catch (Exception e) {
            throw new OneFileException("Not supported file format " + fileName, e);
        }
    } else if (fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT) || fileName.endsWith(IndexConstants.BINARY_MAP_INDEX_EXT_ZIP)) {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(mainFile, "r");
            BinaryMapIndexReader reader = new BinaryMapIndexReader(raf, mainFile);
            if (reader.getVersion() != IndexConstants.BINARY_MAP_VERSION) {
                throw new OneFileException("Uploader version is not compatible " + reader.getVersion() + " to current " + IndexConstants.BINARY_MAP_VERSION);
            }
            String summary = getDescription(reader, fileName);
            reader.close();
            mainFile.setLastModified(reader.getDateCreated());
            return summary;
        } catch (IOException e) {
            if (raf != null) {
                try {
                    raf.close();
                } catch (IOException e1) {
                }
            }
            throw new OneFileException("Reader could not read the index: " + e.getMessage());
        }
    } else {
        throw new OneFileException("Not supported file format " + fileName);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) LeafElement(rtree.LeafElement) Element(org.w3c.dom.Element) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File) ZipException(java.util.zip.ZipException) FileNotFoundException(java.io.FileNotFoundException) JSchException(com.jcraft.jsch.JSchException) IllegalValueException(rtree.IllegalValueException) RTreeException(rtree.RTreeException) IOException(java.io.IOException)

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