Search in sources :

Example 1 with LeafElement

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

the class IndexRouteCreator method addWayToIndex.

private void addWayToIndex(long id, List<Node> nodes, PreparedStatement insertStat, RTree rTree, TIntArrayList outTypes, TLongObjectHashMap<TIntArrayList> pointTypes, TLongObjectHashMap<TIntObjectHashMap<String>> pointNamesRaw, Map<MapRoutingTypes.MapRouteType, String> names) throws SQLException {
    boolean init = false;
    int minX = Integer.MAX_VALUE;
    int maxX = 0;
    int minY = Integer.MAX_VALUE;
    int maxY = 0;
    ByteArrayOutputStream bcoordinates = new ByteArrayOutputStream();
    ByteArrayOutputStream bpointIds = new ByteArrayOutputStream();
    ByteArrayOutputStream bpointTypes = new ByteArrayOutputStream();
    ByteArrayOutputStream btypes = new ByteArrayOutputStream();
    List<MapPointName> pointNamesEmp = new ArrayList<MapPointName>();
    try {
        for (int j = 0; j < outTypes.size(); j++) {
            Algorithms.writeSmallInt(btypes, outTypes.get(j));
        }
        int pointIndex = 0;
        for (Node n : nodes) {
            if (n != null) {
                // write id
                Algorithms.writeLongInt(bpointIds, n.getId());
                // write point type
                TIntArrayList types = pointTypes.get(n.getId());
                if (types != null) {
                    for (int j = 0; j < types.size(); j++) {
                        Algorithms.writeSmallInt(bpointTypes, types.get(j));
                    }
                }
                TIntObjectHashMap<String> namesP = pointNamesRaw.get(n.getId());
                if (namesP != null) {
                    TIntObjectIterator<String> it = namesP.iterator();
                    while (it.hasNext()) {
                        it.advance();
                        MapPointName obj = new MapPointName(it.key(), pointIndex, it.value());
                        pointNamesEmp.add(obj);
                    }
                }
                Algorithms.writeSmallInt(bpointTypes, 0);
                // write coordinates
                int y = MapUtils.get31TileNumberY(n.getLatitude());
                int x = MapUtils.get31TileNumberX(n.getLongitude());
                minX = Math.min(minX, x);
                maxX = Math.max(maxX, x);
                minY = Math.min(minY, y);
                maxY = Math.max(maxY, y);
                init = true;
                Algorithms.writeInt(bcoordinates, x);
                Algorithms.writeInt(bcoordinates, y);
                pointIndex++;
            }
        }
    } catch (IOException est) {
        throw new IllegalStateException(est);
    }
    if (init) {
        // conn.prepareStatement("insert into route_objects(id, types, pointTypes, pointIds, pointCoordinates, name) values(?, ?, ?, ?, ?, ?, ?)");
        insertStat.setLong(1, id);
        insertStat.setBytes(2, btypes.toByteArray());
        insertStat.setBytes(3, bpointTypes.toByteArray());
        insertStat.setBytes(4, bpointIds.toByteArray());
        insertStat.setBytes(5, bcoordinates.toByteArray());
        insertStat.setString(6, encodeNames(names));
        insertStat.setString(7, encodeListNames(pointNamesEmp));
        addBatch(insertStat, false);
        try {
            rTree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), id));
        } catch (RTreeInsertException e1) {
            throw new IllegalArgumentException(e1);
        } catch (IllegalValueException e1) {
            throw new IllegalArgumentException(e1);
        }
    }
}
Also used : Rect(rtree.Rect) Node(net.osmand.osm.edit.Node) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) IllegalValueException(rtree.IllegalValueException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList) RTreeInsertException(rtree.RTreeInsertException) MapPointName(net.osmand.osm.MapRoutingTypes.MapPointName) LeafElement(rtree.LeafElement)

Example 2 with LeafElement

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

the class IndexRouteCreator method appendMissingRoadsForBaseMap.

private void appendMissingRoadsForBaseMap(Connection conn, BinaryMapIndexReader reader) throws IOException, SQLException {
    TLongObjectHashMap<RouteDataObject> map = new CheckRoadConnectivity().collectDisconnectedRoads(reader);
    // to add
    PreparedStatement ps = conn.prepareStatement(COPY_BASE);
    for (RouteDataObject rdo : map.valueCollection()) {
        // addWayToIndex(id, nodes, insertStat, rTree)
        int minX = Integer.MAX_VALUE;
        int maxX = 0;
        int minY = Integer.MAX_VALUE;
        int maxY = 0;
        long id = rdo.getId();
        for (int i = 0; i < rdo.getPointsLength(); i++) {
            int x = rdo.getPoint31XTile(i);
            int y = rdo.getPoint31YTile(i);
            minX = Math.min(minX, x);
            maxX = Math.max(maxX, x);
            minY = Math.min(minY, y);
            maxY = Math.max(maxY, y);
            long point = (x << 31) + y;
            registerBaseIntersectionPoint(point, false, id, i, i);
        }
        ps.setLong(1, id);
        ps.execute();
        try {
            baserouteTree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), id));
        } catch (RTreeInsertException e1) {
            throw new IllegalArgumentException(e1);
        } catch (IllegalValueException e1) {
            throw new IllegalArgumentException(e1);
        }
    }
    ps.close();
}
Also used : RTreeInsertException(rtree.RTreeInsertException) Rect(rtree.Rect) CheckRoadConnectivity(net.osmand.osm.util.CheckRoadConnectivity) IllegalValueException(rtree.IllegalValueException) RouteDataObject(net.osmand.binary.RouteDataObject) PreparedStatement(java.sql.PreparedStatement) LeafElement(rtree.LeafElement)

Example 3 with LeafElement

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

the class IndexTransportCreator method writeRouteStops.

private void writeRouteStops(TransportRoute r, List<TransportStop> stops) throws SQLException {
    int i = 0;
    for (TransportStop s : stops) {
        if (!visitedStops.contains(s.getId())) {
            transStopsStat.setLong(1, s.getId());
            transStopsStat.setDouble(2, s.getLocation().getLatitude());
            transStopsStat.setDouble(3, s.getLocation().getLongitude());
            transStopsStat.setString(4, s.getName());
            transStopsStat.setString(5, s.getEnName(false));
            int x = (int) MapUtils.getTileNumberX(24, s.getLocation().getLongitude());
            int y = (int) MapUtils.getTileNumberY(24, s.getLocation().getLatitude());
            addBatch(transStopsStat);
            try {
                transportStopsTree.insert(new LeafElement(new Rect(x, y, x, y), s.getId()));
            } catch (RTreeInsertException e) {
                throw new IllegalArgumentException(e);
            } catch (IllegalValueException e) {
                throw new IllegalArgumentException(e);
            }
            visitedStops.add(s.getId());
        }
        transRouteStopsStat.setLong(1, r.getId());
        transRouteStopsStat.setLong(2, s.getId());
        transRouteStopsStat.setInt(3, i++);
        addBatch(transRouteStopsStat);
    }
}
Also used : RTreeInsertException(rtree.RTreeInsertException) Rect(rtree.Rect) IllegalValueException(rtree.IllegalValueException) TransportStop(net.osmand.data.TransportStop) LeafElement(rtree.LeafElement)

Example 4 with LeafElement

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

the class IndexVectorMapCreator method insertBinaryMapRenderObjectIndex.

private void insertBinaryMapRenderObjectIndex(RTree mapTree, Collection<Node> nodes, List<List<Node>> innerWays, Map<MapRulType, String> names, long id, boolean area, TIntArrayList types, TIntArrayList addTypes, boolean commit) throws SQLException {
    boolean init = false;
    int minX = Integer.MAX_VALUE;
    int maxX = 0;
    int minY = Integer.MAX_VALUE;
    int maxY = 0;
    ByteArrayOutputStream bcoordinates = new ByteArrayOutputStream();
    ByteArrayOutputStream binnercoord = new ByteArrayOutputStream();
    ByteArrayOutputStream btypes = new ByteArrayOutputStream();
    ByteArrayOutputStream badditionalTypes = new ByteArrayOutputStream();
    try {
        for (int j = 0; j < types.size(); j++) {
            Algorithms.writeSmallInt(btypes, types.get(j));
        }
        for (int j = 0; j < addTypes.size(); j++) {
            Algorithms.writeSmallInt(badditionalTypes, addTypes.get(j));
        }
        for (Node n : nodes) {
            if (n != null) {
                int y = MapUtils.get31TileNumberY(n.getLatitude());
                int x = MapUtils.get31TileNumberX(n.getLongitude());
                minX = Math.min(minX, x);
                maxX = Math.max(maxX, x);
                minY = Math.min(minY, y);
                maxY = Math.max(maxY, y);
                init = true;
                Algorithms.writeInt(bcoordinates, x);
                Algorithms.writeInt(bcoordinates, y);
            }
        }
        if (innerWays != null) {
            for (List<Node> ws : innerWays) {
                boolean exist = false;
                if (ws != null) {
                    for (Node n : ws) {
                        if (n != null) {
                            exist = true;
                            int y = MapUtils.get31TileNumberY(n.getLatitude());
                            int x = MapUtils.get31TileNumberX(n.getLongitude());
                            Algorithms.writeInt(binnercoord, x);
                            Algorithms.writeInt(binnercoord, y);
                        }
                    }
                }
                if (exist) {
                    Algorithms.writeInt(binnercoord, 0);
                    Algorithms.writeInt(binnercoord, 0);
                }
            }
        }
    } catch (IOException es) {
        throw new IllegalStateException(es);
    }
    if (init) {
        // conn.prepareStatement("insert into binary_map_objects(id, area, coordinates, innerPolygons, types, additionalTypes, name) values(?, ?, ?, ?, ?, ?, ?)");
        mapBinaryStat.setLong(1, id);
        mapBinaryStat.setBoolean(2, area);
        mapBinaryStat.setBytes(3, bcoordinates.toByteArray());
        mapBinaryStat.setBytes(4, binnercoord.toByteArray());
        mapBinaryStat.setBytes(5, btypes.toByteArray());
        mapBinaryStat.setBytes(6, badditionalTypes.toByteArray());
        mapBinaryStat.setString(7, encodeNames(names));
        addBatch(mapBinaryStat, commit);
        try {
            mapTree.insert(new LeafElement(new Rect(minX, minY, maxX, maxY), id));
        } catch (RTreeInsertException e1) {
            throw new IllegalArgumentException(e1);
        } catch (IllegalValueException e1) {
            throw new IllegalArgumentException(e1);
        }
    }
}
Also used : RTreeInsertException(rtree.RTreeInsertException) Rect(rtree.Rect) Node(net.osmand.osm.edit.Node) IllegalValueException(rtree.IllegalValueException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LeafElement(rtree.LeafElement)

Example 5 with LeafElement

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

Aggregations

LeafElement (rtree.LeafElement)6 Rect (rtree.Rect)6 IOException (java.io.IOException)4 IllegalValueException (rtree.IllegalValueException)4 RTreeInsertException (rtree.RTreeInsertException)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 SQLException (java.sql.SQLException)2 RouteDataObject (net.osmand.binary.RouteDataObject)2 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)2 Node (net.osmand.osm.edit.Node)2 RTree (rtree.RTree)2 RTreeException (rtree.RTreeException)2 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)1