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);
}
}
}
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());
}
}
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();
}
}
}
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
}
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();
}
}
Aggregations