Search in sources :

Example 1 with BinaryMapIndexWriter

use of net.osmand.data.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 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) {
        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);
            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 = calcBounds(root);
            if (rootBounds != null) {
                if (first) {
                    writer.writeMapEncodingRules(part.decodingRules);
                    first = false;
                }
                writer.startWriteMapLevelIndex(r.getMinZoom(), r.getMaxZoom(), rootBounds.getMinX(), rootBounds.getMaxX(), rootBounds.getMinY(), rootBounds.getMaxY());
                IndexVectorMapCreator.writeBinaryMapTree(root, rootBounds, rtree, writer, treeHeader);
                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.data.preparation.BinaryFileReference) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) RandomAccessFile(java.io.RandomAccessFile) Node(rtree.Node) RTree(rtree.RTree) BinaryMapIndexWriter(net.osmand.data.preparation.BinaryMapIndexWriter) RandomAccessFile(java.io.RandomAccessFile) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with BinaryMapIndexWriter

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

the class ObfFileInMemory method writeFile.

public void writeFile(File targetFile, boolean doNotSimplifyObjects) throws IOException, RTreeException, SQLException {
    boolean gzip = targetFile.getName().endsWith(".gz");
    File nonGzip = targetFile;
    if (gzip) {
        nonGzip = new File(targetFile.getParentFile(), targetFile.getName().substring(0, targetFile.getName().length() - 3));
    }
    final RandomAccessFile raf = new RandomAccessFile(nonGzip, "rw");
    // write files
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

        @Override
        public void write(int b) throws IOException {
            raf.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            raf.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            raf.write(b, off, len);
        }
    });
    timestamp = timestamp == 0 ? System.currentTimeMillis() : timestamp;
    int version = IndexConstants.BINARY_MAP_VERSION;
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, timestamp);
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(raf, ous);
    String defName = targetFile.getName().substring(0, targetFile.getName().indexOf('.'));
    if (mapObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteMapIndex(Algorithms.capitalizeFirstLetter(name));
        writer.writeMapEncodingRules(mapIndex.decodingRules);
        Iterator<Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> it = mapObjects.entrySet().iterator();
        while (it.hasNext()) {
            Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> n = it.next();
            writeMapData(writer, n.getKey(), n.getValue(), targetFile, doNotSimplifyObjects);
        }
        writer.endWriteMapIndex();
    }
    if (routeObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteRouteIndex(name);
        writer.writeRouteRawEncodingRules(routeIndex.routeEncodingRules);
        writeRouteData(writer, routeObjects, targetFile);
        writer.endWriteRouteIndex();
    }
    if (poiObjects.size() > 0) {
        String name = "";
        boolean overwriteIds = false;
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
        final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
        File poiFile = new File(targetFile.getParentFile(), IndexCreator.getPoiFileName(name));
        indexPoiCreator.createDatabaseStructure(poiFile);
        for (Map<String, Amenity> mp : poiObjects.valueCollection()) {
            for (Amenity a : mp.values()) {
                indexPoiCreator.insertAmenityIntoPoi(a);
            }
        }
        indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
        indexPoiCreator.commitAndClosePoiFile(System.currentTimeMillis());
        indexPoiCreator.removePoiFile();
    }
    // TODO Write Transport
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
    raf.close();
    if (gzip) {
        nonGzip.setLastModified(timestamp);
        FileInputStream fis = new FileInputStream(nonGzip);
        GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(targetFile));
        Algorithms.streamCopy(fis, gzout);
        fis.close();
        gzout.close();
        nonGzip.delete();
    }
    targetFile.setLastModified(timestamp);
}
Also used : Amenity(net.osmand.data.Amenity) CodedOutputStream(com.google.protobuf.CodedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) IOException(java.io.IOException) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) FileInputStream(java.io.FileInputStream) Entry(java.util.Map.Entry) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexWriter(net.osmand.data.preparation.BinaryMapIndexWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 3 with BinaryMapIndexWriter

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

the class BinaryMerger method combineParts.

public void combineParts(File fileToExtract, List<File> files, Set<Integer> combineParts) throws IOException, SQLException {
    BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[files.size()];
    RandomAccessFile[] rafs = new RandomAccessFile[files.size()];
    long dateCreated = 0;
    int version = -1;
    // Go through all files and validate consistency
    int c = 0;
    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++;
    }
    // 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[files.size()];
    PoiRegion[] poiRegions = new PoiRegion[files.size()];
    for (int k = 0; k < indexes.length; k++) {
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs[k];
        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 {
                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) MapObject(net.osmand.data.MapObject) BinaryMapIndexWriter(net.osmand.data.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)3 RandomAccessFile (java.io.RandomAccessFile)3 BinaryMapIndexWriter (net.osmand.data.preparation.BinaryMapIndexWriter)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Entry (java.util.Map.Entry)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ZipFile (java.util.zip.ZipFile)1 BinaryIndexPart (net.osmand.binary.BinaryIndexPart)1 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)1 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)1 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)1 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)1 MapZoomPair (net.osmand.binary.MapZooms.MapZoomPair)1 Amenity (net.osmand.data.Amenity)1