Search in sources :

Example 1 with Node

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

the class IndexUploader method writeBinaryMapBlock.

public static void writeBinaryMapBlock(Node parent, Rect parentBounds, RTree r, BinaryMapIndexWriter writer, TLongObjectHashMap<BinaryFileReference> bounds, TLongObjectHashMap<BinaryMapDataObject> objects, MapZooms.MapZoomPair pair, boolean doNotSimplify) throws IOException, RTreeException {
    rtree.Element[] e = parent.getAllElements();
    MapDataBlock.Builder dataBlock = null;
    BinaryFileReference ref = bounds.get(parent.getNodeIndex());
    long baseId = 0;
    Map<String, Integer> tempStringTable = new LinkedHashMap<String, Integer>();
    for (int i = 0; i < parent.getTotalElements(); i++) {
        if (e[i].getElementType() == rtree.Node.LEAF_NODE) {
            long id = e[i].getPtr();
            if (objects.containsKey(id)) {
                long cid = id;
                BinaryMapDataObject mdo = objects.get(id);
                if (dataBlock == null) {
                    baseId = cid;
                    dataBlock = writer.createWriteMapDataBlock(baseId);
                    tempStringTable.clear();
                }
                int[] typeUse = mdo.getTypes();
                int[] addtypeUse = mdo.getAdditionalTypes();
                byte[] coordinates = new byte[8 * mdo.getPointsLength()];
                for (int t = 0; t < mdo.getPointsLength(); t++) {
                    Algorithms.putIntToBytes(coordinates, 8 * t, mdo.getPoint31XTile(t));
                    Algorithms.putIntToBytes(coordinates, 8 * t + 4, mdo.getPoint31YTile(t));
                }
                byte[] innerPolygonTypes = new byte[0];
                int[][] pip = mdo.getPolygonInnerCoordinates();
                if (pip != null && pip.length > 0) {
                    ByteArrayOutputStream bous = new ByteArrayOutputStream();
                    for (int s = 0; s < pip.length; s++) {
                        int[] st = pip[s];
                        for (int t = 0; t < st.length; t++) {
                            Algorithms.writeInt(bous, st[t]);
                        }
                        Algorithms.writeInt(bous, 0);
                        Algorithms.writeInt(bous, 0);
                    }
                    innerPolygonTypes = bous.toByteArray();
                }
                MapData mapData = writer.writeMapData(cid - baseId, parentBounds.getMinX(), parentBounds.getMinY(), mdo.isArea(), coordinates, innerPolygonTypes, typeUse, addtypeUse, null, mdo.getOrderedObjectNames(), tempStringTable, dataBlock, !doNotSimplify && pair.getMaxZoom() > 15);
                if (mapData != null) {
                    dataBlock.addDataObjects(mapData);
                }
            } else {
                // $NON-NLS-1$
                log.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, bounds, objects, pair, doNotSimplify);
        }
    }
}
Also used : MapDataBlock(net.osmand.binary.OsmandOdb.MapDataBlock) LeafElement(rtree.LeafElement) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryFileReference(net.osmand.data.preparation.BinaryFileReference) LinkedHashMap(java.util.LinkedHashMap) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) MapData(net.osmand.binary.OsmandOdb.MapData) Node(rtree.Node)

Example 2 with Node

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

the class SdTree method seed.

/**
 *     Start seeding - take the root node, copy it to this tree, keep on copying until the slot level.
 *     This method overwrites the root irrespective of its existance or nonexistence.
 */
private void seed() throws RTreeException {
    try {
        long sdingRoot = sdingTree.getFileHdr().getRootIndex();
        // somehow remove all the nodes of this tree from the cache and since we have a write lock
        // nobody can get this tree's nodes on to the buufer if we don't
        Node sdingNode = sdingTree.getReadNode(sdingRoot);
        seedRec(sdingNode, // sd
        chdNodes.getNode(// sd
        fileHdr.getFile(), // sd
        fileName, // sd
        Node.NOT_DEFINED, sdingNode.getElementType(), fileHdr), 0);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RTreeException(e.getMessage());
    }
}
Also used : RTreeException(rtree.RTreeException) Node(rtree.Node) RTreeException(rtree.RTreeException) NodeFullException(rtree.NodeFullException) IOException(java.io.IOException) NodeReadException(rtree.NodeReadException) FileNotFoundException(java.io.FileNotFoundException) IllegalValueException(rtree.IllegalValueException) NodeWriteException(rtree.NodeWriteException)

Example 3 with Node

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

the class SdTree method growLeaf.

public void growLeaf(Element elmt) throws RTreeException {
    if (slotLvl == Node.NOT_DEFINED) {
        try {
            insert(elmt);
        } catch (Exception e) {
            throw new RTreeException(e.getMessage());
        }
    } else {
        fileHdr.lockWrite();
        try {
            long root = fileHdr.getRootIndex();
            // Long slotIndex = null;
            LongWraper slotIndex = new LongWraper();
            // sd
            Node node = this.chooseLeaf(elmt, slotIndex);
            if (slotIndex == null)
                throw new NullPointerException();
            long nodeParent = node.getParent();
            Node[] newNodes = new Node[2];
            try {
                // if another insert is possible
                node.insertElement(elmt);
                newNodes[0] = node;
                newNodes[1] = null;
            } catch (NodeFullException e) {
                // if another insert is not possible
                newNodes = node.splitNode(elmt, slotIndex.val);
            }
            Node newRoot = adjustTree(newNodes, slotIndex.val);
            // if we got a new root node then we have to set the slot's child to point to this new root
            if (newRoot != null) {
                // sd
                Node slot = chdNodes.getNode(fileHdr.getFile(), fileName, newRoot.getParent(), fileHdr);
                slot.modifyElement(0, newRoot.getNodeIndex());
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RTreeException(e.getMessage());
        } finally {
            fileHdr.unlock();
        }
    }
}
Also used : RTreeException(rtree.RTreeException) Node(rtree.Node) NodeFullException(rtree.NodeFullException) RTreeException(rtree.RTreeException) NodeFullException(rtree.NodeFullException) IOException(java.io.IOException) NodeReadException(rtree.NodeReadException) FileNotFoundException(java.io.FileNotFoundException) IllegalValueException(rtree.IllegalValueException) NodeWriteException(rtree.NodeWriteException)

Example 4 with Node

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

the class SdTree method cleanUpRec.

/**
 * This method adjusts all the seed node MBRs to the grown subtrees. It also delets the slot node and
 * makes the root node of the underneath substree as the slot node.
 */
private // sd
Rect cleanUpRec(// sd
Node node, // sd
int level) throws NodeWriteException, FileNotFoundException, IllegalValueException, IOException, NodeReadException, RTreeException {
    Element[] elmts = node.getAllElements();
    if (level == slotLvl) {
        // if level is the slot
        if (elmts[0].getPtr() == Node.NOT_DEFINED) {
            // this slot was never supplied a child node
            node.deleteNode();
            // a null rect
            return new Rect();
        } else {
            // a slot that does have child node
            // remove this slot node and make the parent element point to the child of this slot node
            // sd
            Node parentNode = chdNodes.getNode(fileHdr.getFile(), fileName, node.getParent(), fileHdr);
            int index = parentNode.getElementIndex(node.getNodeIndex());
            parentNode.modifyElement(index, elmts[0].getPtr());
            // sd
            Node subRoot = chdNodes.getNode(fileHdr.getFile(), fileName, elmts[0].getPtr(), fileHdr);
            subRoot.setParent(node.getParent());
            node.deleteNode();
            return (subRoot.getNodeMBR());
        }
    // else
    } else {
        // it is not slot node but a seed node
        // remebeer we may have a situation where we do not get any Rect from down below this node...we delete
        // this node as well.
        Rect rect = new Rect();
        for (int i = node.getTotalElements() - 1; i > -1; i--) {
            // for each element in this seed node
            // sd
            Node chNode = chdNodes.getNode(fileHdr.getFile(), fileName, elmts[i].getPtr(), fileHdr);
            Rect chRect = cleanUpRec(chNode, level + 1);
            // get child node's rect
            rect.expandToInclude(chRect);
            if (chRect.isNull()) {
                // situation where child node does not have grown subtrees underneath
                node.deleteElement(i, false);
            } else {
                // we do have a child Rect
                node.modifyElement(i, chRect);
            }
        // else
        }
        // for
        if (rect.isNull()) {
            // situation where there are no grown subtrees underneath this node
            node.deleteNode();
        }
        return rect;
    }
// else
}
Also used : Rect(rtree.Rect) Element(rtree.Element) NonLeafElement(rtree.NonLeafElement) Node(rtree.Node)

Example 5 with Node

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

the class SdTree method cleanUp.

/**
 * The clean up pahse is the last method that should be called after all the data have been grown.
 * This method basically adjusts all the slot nodes after all the insertions are made
 */
public void cleanUp() throws RTreeException {
    try {
        fileHdr.lockWrite();
        if (slotLvl == Node.NOT_DEFINED)
            return;
        long root = fileHdr.getRootIndex();
        // sd
        Node node = chdNodes.getNode(fileHdr.getFile(), fileName, root, fileHdr);
        cleanUpRec(node, 0);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RTreeException(e.getMessage());
    } finally {
        fileHdr.unlock();
    }
}
Also used : RTreeException(rtree.RTreeException) Node(rtree.Node) RTreeException(rtree.RTreeException) NodeFullException(rtree.NodeFullException) IOException(java.io.IOException) NodeReadException(rtree.NodeReadException) FileNotFoundException(java.io.FileNotFoundException) IllegalValueException(rtree.IllegalValueException) NodeWriteException(rtree.NodeWriteException)

Aggregations

Node (rtree.Node)7 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 IllegalValueException (rtree.IllegalValueException)4 NodeFullException (rtree.NodeFullException)4 NodeReadException (rtree.NodeReadException)4 NodeWriteException (rtree.NodeWriteException)4 RTreeException (rtree.RTreeException)4 Element (rtree.Element)3 NonLeafElement (rtree.NonLeafElement)3 Rect (rtree.Rect)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 LinkedHashMap (java.util.LinkedHashMap)1 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)1 MapData (net.osmand.binary.OsmandOdb.MapData)1 MapDataBlock (net.osmand.binary.OsmandOdb.MapDataBlock)1 BinaryFileReference (net.osmand.data.preparation.BinaryFileReference)1 Element (org.w3c.dom.Element)1 LeafElement (rtree.LeafElement)1