Search in sources :

Example 6 with Element

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

the class IndexVectorMapCreator method calcBounds.

public Rect calcBounds(rtree.Node n) {
    Rect r = null;
    Element[] e = n.getAllElements();
    for (int i = 0; i < n.getTotalElements(); i++) {
        Rect re = e[i].getRect();
        if (r == null) {
            try {
                r = new Rect(re.getMinX(), re.getMinY(), re.getMaxX(), re.getMaxY());
            } catch (IllegalValueException ex) {
            }
        } else {
            r.expandToInclude(re);
        }
    }
    return r;
}
Also used : Rect(rtree.Rect) Element(rtree.Element) LeafElement(rtree.LeafElement) IllegalValueException(rtree.IllegalValueException)

Example 7 with Element

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

the class IndexVectorMapCreator method writeBinaryMapBlock.

public void writeBinaryMapBlock(rtree.Node parent, Rect parentBounds, RTree r, BinaryMapIndexWriter writer, PreparedStatement selectData, TLongObjectHashMap<BinaryFileReference> bounds, Map<String, Integer> tempStringTable, LinkedHashMap<MapRulType, String> tempNames, MapZoomPair level) throws IOException, RTreeException, SQLException {
    Element[] e = parent.getAllElements();
    MapDataBlock.Builder dataBlock = null;
    BinaryFileReference ref = bounds.get(parent.getNodeIndex());
    long baseId = 0;
    for (int i = 0; i < parent.getTotalElements(); i++) {
        if (e[i].getElementType() == rtree.Node.LEAF_NODE) {
            long id = e[i].getPtr();
            selectData.setLong(1, id);
            // selectData = mapConnection.prepareStatement("SELECT area, coordinates, innerPolygons, types, additionalTypes, name FROM binary_map_objects WHERE id = ?");
            ResultSet rs = selectData.executeQuery();
            if (rs.next()) {
                long cid = convertGeneratedIdToObfWrite(id);
                if (dataBlock == null) {
                    baseId = cid;
                    dataBlock = writer.createWriteMapDataBlock(baseId);
                    tempStringTable.clear();
                }
                tempNames.clear();
                decodeNames(rs.getString(6), tempNames);
                byte[] types = rs.getBytes(4);
                int[] typeUse = new int[types.length / 2];
                for (int j = 0; j < types.length; j += 2) {
                    int ids = Algorithms.parseSmallIntFromBytes(types, j);
                    typeUse[j / 2] = renderingTypes.getTypeByInternalId(ids).getTargetId();
                }
                byte[] addTypes = rs.getBytes(5);
                int[] addtypeUse = null;
                if (addTypes != null) {
                    addtypeUse = new int[addTypes.length / 2];
                    for (int j = 0; j < addTypes.length; j += 2) {
                        int ids = Algorithms.parseSmallIntFromBytes(addTypes, j);
                        addtypeUse[j / 2] = renderingTypes.getTypeByInternalId(ids).getTargetId();
                    }
                }
                MapData mapData = writer.writeMapData(cid - baseId, parentBounds.getMinX(), parentBounds.getMinY(), rs.getBoolean(1), rs.getBytes(2), rs.getBytes(3), typeUse, addtypeUse, tempNames, null, tempStringTable, dataBlock, level.getMaxZoom() > 15);
                if (mapData != null) {
                    dataBlock.addDataObjects(mapData);
                }
            } else {
                // $NON-NLS-1$
                logMapDataWarn.error("Something goes wrong with id = " + id);
            }
        }
    }
    if (dataBlock != null) {
        writer.writeMapDataBlock(dataBlock, tempStringTable, ref);
    }
    for (int i = 0; i < parent.getTotalElements(); i++) {
        if (e[i].getElementType() != rtree.Node.LEAF_NODE) {
            long ptr = e[i].getPtr();
            rtree.Node ns = r.getReadNode(ptr);
            writeBinaryMapBlock(ns, e[i].getRect(), r, writer, selectData, bounds, tempStringTable, tempNames, level);
        }
    }
}
Also used : MapDataBlock(net.osmand.binary.OsmandOdb.MapDataBlock) Element(rtree.Element) LeafElement(rtree.LeafElement) MapData(net.osmand.binary.OsmandOdb.MapData) ResultSet(java.sql.ResultSet)

Example 8 with Element

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

the class SdTree method seedRec.

/**
 *     @param sdingNode The seeding node from which to copy (source).
 *     @param level The height at which this node falls in the tree.
 */
private // sd
void seedRec(// sd
Node sdingNode, // sd
Node sdNode, // sd
int level) throws Exception {
    if (sdingNode.getElementType() == Node.LEAF_NODE)
        throw new IllegalArgumentException("SdTree.seedRec : Cannot seed a leaf node");
    // make the child nodes before hand so that we know their indices in file
    // sd
    Node[] chNodes = null;
    if (// we do not need to alocate new nodes if we are at slot level
    level != slotLvl)
        // sd
        chNodes = new Node[sdingNode.getTotalElements()];
    Element[] elmts = sdingNode.getAllElements();
    // elements for non-slot levels
    Element[] newElmts;
    if (level != slotLvl)
        // non slots have multiple elements
        newElmts = new Element[sdingNode.getTotalElements()];
    else {
        // slot has only one element
        newElmts = new Element[1];
        // element for slot level
        newElmts[0] = new NonLeafElement(new Rect(), Node.NOT_DEFINED);
    }
    for (int i = 0; i < sdingNode.getTotalElements(); i++) {
        if (level != slotLvl) {
            // we do not seed leaf elements
            newElmts[i] = (NonLeafElement) ((NonLeafElement) elmts[i]).clone();
            chNodes[i] = // sd
            chdNodes.getNode(// sd
            fileHdr.getFile(), // sd
            fileName, // sd
            sdNode.getNodeIndex(), sdingNode.getElementType(), fileHdr);
            // update the child pointers for new node
            newElmts[i].setPtr(chNodes[i].getNodeIndex());
            seedRec(sdingTree.getReadNode(elmts[i].getPtr()), chNodes[i], level + 1);
        } else {
            // this is the slot level
            /*What we do here is that we put only one element into the slot node instead of all the elements.
          This would result in a single element in the node which represents all the seding node elements
          with a null pointer.
        */
            newElmts[0].getRect().expandToInclude(elmts[i].getRect());
        }
    // else
    }
    // for
    // copy the non-slot elements now //sd
    sdNode.insertElement(newElmts, false);
}
Also used : Rect(rtree.Rect) Node(rtree.Node) Element(rtree.Element) NonLeafElement(rtree.NonLeafElement) NonLeafElement(rtree.NonLeafElement)

Example 9 with Element

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

Element (rtree.Element)9 LeafElement (rtree.LeafElement)6 Rect (rtree.Rect)6 IllegalValueException (rtree.IllegalValueException)4 Node (rtree.Node)3 NonLeafElement (rtree.NonLeafElement)3 ResultSet (java.sql.ResultSet)2 TLongArrayList (gnu.trove.list.array.TLongArrayList)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 IdTable (net.osmand.binary.OsmandOdb.IdTable)1 MapData (net.osmand.binary.OsmandOdb.MapData)1 MapDataBlock (net.osmand.binary.OsmandOdb.MapDataBlock)1 RouteDataBlock (net.osmand.binary.OsmandOdb.OsmAndRoutingIndex.RouteDataBlock)1 Builder (net.osmand.binary.OsmandOdb.RestrictionData.Builder)1 RouteData (net.osmand.binary.OsmandOdb.RouteData)1 RoutePointToWrite (net.osmand.data.preparation.BinaryMapIndexWriter.RoutePointToWrite)1 NodeFullException (rtree.NodeFullException)1 NodeReadException (rtree.NodeReadException)1 NodeWriteException (rtree.NodeWriteException)1