Search in sources :

Example 1 with BinaryMapIndexWriter

use of net.osmand.obf.preparation.BinaryMapIndexWriter in project OsmAnd-tools by osmandapp.

the class IndexUploader method copyMapIndex.

private static void copyMapIndex(File roadOnlyFile, MapIndex part, BinaryMapIndexReader index, CodedOutputStream ous, RandomAccessFile raf, RandomAccessFile routf) throws IOException, RTreeException {
    final List<MapRoot> rts = part.getRoots();
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(routf, ous);
    writer.startWriteMapIndex(part.getName());
    boolean first = true;
    for (MapRoot r : rts) {
        if (r.getMaxZoom() <= 10) {
            if (first) {
                throw new UnsupportedOperationException("Can't write top level zoom");
            }
            ous.writeTag(OsmandOdb.OsmAndMapIndex.LEVELS_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
            BinaryMerger.writeInt(ous, r.getLength());
            BinaryMerger.copyBinaryPart(ous, new byte[BUFFER_SIZE], raf, r.getFilePointer(), r.getLength());
            continue;
        }
        final TLongObjectHashMap<BinaryMapDataObject> objects = new TLongObjectHashMap<BinaryMapDataObject>();
        File nonpackRtree = new File(roadOnlyFile.getParentFile(), "nonpack" + r.getMinZoom() + "." + roadOnlyFile.getName() + ".rtree");
        File packRtree = new File(roadOnlyFile.getParentFile(), "pack" + r.getMinZoom() + "." + roadOnlyFile.getName() + ".rtree");
        RTree rtree = null;
        try {
            rtree = new RTree(nonpackRtree.getAbsolutePath());
            final SearchRequest<BinaryMapDataObject> req = buildSearchRequest(r, objects, rtree);
            index.searchMapIndex(req, part);
            if (first) {
                first = false;
                writer.writeMapEncodingRules(part.decodingRules);
            }
            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 = IndexVectorMapCreator.calcBounds(root);
            if (rootBounds != null) {
                writer.startWriteMapLevelIndex(r.getMinZoom(), r.getMaxZoom(), rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
                IndexVectorMapCreator.writeBinaryMapTree(root, rootBounds, rtree, writer, treeHeader);
                IndexVectorMapCreator.writeBinaryMapBlock(root, rootBounds, rtree, writer, treeHeader, objects, r.getMapZoom(), false);
                writer.endWriteMapLevelIndex();
            }
        } finally {
            if (rtree != null) {
                RandomAccessFile file = rtree.getFileHdr().getFile();
                file.close();
            }
            nonpackRtree.delete();
            packRtree.delete();
            RTree.clearCache();
        }
    }
    writer.endWriteMapIndex();
}
Also used : Rect(rtree.Rect) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) BinaryFileReference(net.osmand.obf.preparation.BinaryFileReference) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) RandomAccessFile(java.io.RandomAccessFile) RTree(rtree.RTree) BinaryMapIndexWriter(net.osmand.obf.preparation.BinaryMapIndexWriter) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with BinaryMapIndexWriter

use of net.osmand.obf.preparation.BinaryMapIndexWriter in project OsmAnd-tools by osmandapp.

the class BinaryMerger method combineParts.

public void combineParts(File fileToExtract, List<File> files, List<BinaryMapIndexReader> readers, Set<Integer> combineParts) throws IOException, SQLException {
    boolean combineFiles = files != null;
    BinaryMapIndexReader[] indexes = combineFiles ? new BinaryMapIndexReader[files.size()] : readers.toArray(new BinaryMapIndexReader[readers.size()]);
    RandomAccessFile[] rafs = combineFiles ? new RandomAccessFile[files.size()] : null;
    long dateCreated = 0;
    int version = -1;
    // Go through all files and validate consistency
    int c = 0;
    if (combineFiles) {
        for (File f : files) {
            if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
                System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
                return;
            }
            rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
            indexes[c] = new BinaryMapIndexReader(rafs[c], f);
            dateCreated = Math.max(dateCreated, indexes[c].getDateCreated());
            if (version == -1) {
                version = indexes[c].getVersion();
            } else {
                if (indexes[c].getVersion() != version) {
                    System.err.println("Error : Different input files has different input versions " + indexes[c].getVersion() + " != " + version);
                    return;
                }
            }
            c++;
        }
    } else {
        for (BinaryMapIndexReader index : indexes) {
            dateCreated = Math.max(dateCreated, index.getDateCreated());
            if (version == -1) {
                version = index.getVersion();
            } else {
                if (index.getVersion() != version) {
                    System.err.println("Error : Different input files has different input versions " + index.getVersion() + " != " + version);
                    return;
                }
            }
        }
    }
    // write files
    RandomAccessFile rafToExtract = new RandomAccessFile(fileToExtract, "rw");
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(rafToExtract, dateCreated);
    CodedOutputStream ous = writer.getCodedOutStream();
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    AddressRegion[] addressRegions = new AddressRegion[combineFiles ? files.size() : readers.size()];
    PoiRegion[] poiRegions = new PoiRegion[combineFiles ? files.size() : readers.size()];
    for (int k = 0; k < indexes.length; k++) {
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs != null ? rafs[k] : null;
        for (int i = 0; i < index.getIndexes().size(); i++) {
            BinaryIndexPart part = index.getIndexes().get(i);
            if (combineParts.contains(part.getFieldNumber())) {
                if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER) {
                    addressRegions[k] = (AddressRegion) part;
                } else if (part.getFieldNumber() == OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER) {
                    poiRegions[k] = (PoiRegion) part;
                }
            } else if (raf != null) {
                ous.writeTag(part.getFieldNumber(), WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                writeInt(ous, part.getLength());
                copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
                System.out.println(MessageFormat.format("{2} part {0} is extracted {1} bytes", new Object[] { part.getName(), part.getLength(), part.getPartName() }));
            }
        }
    }
    String nm = fileToExtract.getName();
    int i = nm.indexOf('_');
    if (i > 0) {
        nm = nm.substring(0, i);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER)) {
        combineAddressIndex(nm, writer, addressRegions, indexes);
    }
    if (combineParts.contains(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER)) {
        combinePoiIndex(nm, writer, dateCreated, poiRegions, indexes);
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RandomAccessFile(java.io.RandomAccessFile) BinaryMapIndexWriter(net.osmand.obf.preparation.BinaryMapIndexWriter) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Aggregations

File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 BinaryMapIndexWriter (net.osmand.obf.preparation.BinaryMapIndexWriter)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)1 ZipFile (java.util.zip.ZipFile)1 BinaryIndexPart (net.osmand.binary.BinaryIndexPart)1 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)1 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)1 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)1 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)1 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)1 BinaryFileReference (net.osmand.obf.preparation.BinaryFileReference)1 RTree (rtree.RTree)1 Rect (rtree.Rect)1