Search in sources :

Example 1 with AddressRegion

use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project OsmAnd-tools by osmandapp.

the class IndexUploader method extractRoadOnlyFile.

public static void extractRoadOnlyFile(File mainFile, File roadOnlyFile) throws IOException, RTreeException {
    RandomAccessFile raf = new RandomAccessFile(mainFile, "r");
    BinaryMapIndexReader index = new BinaryMapIndexReader(raf, mainFile);
    final RandomAccessFile routf = new RandomAccessFile(roadOnlyFile, "rw");
    routf.setLength(0);
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

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

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

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            routf.write(b, off, len);
        }
    });
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, index.getVersion());
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, index.getDateCreated());
    for (int i = 0; i < index.getIndexes().size(); i++) {
        BinaryIndexPart part = index.getIndexes().get(i);
        if (part instanceof MapIndex) {
            // skip map part
            copyMapIndex(roadOnlyFile, (MapIndex) part, index, ous, routf);
            continue;
        } else if (part instanceof AddressRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof TransportIndex) {
            ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof PoiRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof RouteRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else {
            throw new UnsupportedOperationException();
        }
        BinaryMerger.writeInt(ous, part.getLength());
        BinaryMerger.copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, index.getVersion());
    ous.flush();
    routf.close();
    raf.close();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 2 with AddressRegion

use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion 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)

Example 3 with AddressRegion

use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.

the class BinaryInspector method printFileInformation.

public void printFileInformation(RandomAccessFile r, File file) throws IOException {
    String filename = file.getName();
    try {
        BinaryMapIndexReader index = new BinaryMapIndexReader(r, file);
        int i = 1;
        println("Binary index " + filename + " version = " + index.getVersion() + " edition = " + new Date(index.getDateCreated()));
        for (BinaryIndexPart p : index.getIndexes()) {
            String partname = "";
            if (p instanceof MapIndex) {
                partname = "Map";
            } else if (p instanceof TransportIndex) {
                partname = "Transport";
            } else if (p instanceof RouteRegion) {
                partname = "Routing";
            } else if (p instanceof PoiRegion) {
                partname = "Poi";
            } else if (p instanceof AddressRegion) {
                partname = "Address";
            }
            String name = p.getName() == null ? "" : p.getName();
            println(MessageFormat.format("{0} {1} data {3} - {2,number,#} bytes", new Object[] { i, partname, p.getLength(), name }));
            if (p instanceof TransportIndex) {
                TransportIndex ti = ((TransportIndex) p);
                int sh = (31 - BinaryMapIndexReader.TRANSPORT_STOP_ZOOM);
                println("\tBounds " + formatBounds(ti.getLeft() << sh, ti.getRight() << sh, ti.getTop() << sh, ti.getBottom() << sh));
                if ((vInfo != null && vInfo.isVtransport())) {
                    printTransportDetailInfo(vInfo, index, (TransportIndex) p);
                }
            } else if (p instanceof RouteRegion) {
                RouteRegion ri = ((RouteRegion) p);
                println("\tBounds " + formatLatBounds(ri.getLeftLongitude(), ri.getRightLongitude(), ri.getTopLatitude(), ri.getBottomLatitude()));
                if ((vInfo != null && vInfo.isVrouting())) {
                    printRouteDetailInfo(index, (RouteRegion) p);
                }
            } else if (p instanceof MapIndex) {
                MapIndex m = ((MapIndex) p);
                int j = 1;
                for (MapRoot mi : m.getRoots()) {
                    println(MessageFormat.format("\t{4}.{5} Map level minZoom = {0}, maxZoom = {1}, size = {2,number,#} bytes \n\t\tBounds {3}", new Object[] { mi.getMinZoom(), mi.getMaxZoom(), mi.getLength(), formatBounds(mi.getLeft(), mi.getRight(), mi.getTop(), mi.getBottom()), i, j++ }));
                }
                if ((vInfo != null && vInfo.isVmap())) {
                    printMapDetailInfo(index, m);
                }
            } else if (p instanceof PoiRegion && (vInfo != null && vInfo.isVpoi())) {
                printPOIDetailInfo(vInfo, index, (PoiRegion) p);
            } else if (p instanceof TransportIndex && (vInfo != null && vInfo.isVtransport())) {
            } else if (p instanceof AddressRegion) {
                List<CitiesBlock> cities = ((AddressRegion) p).cities;
                for (CitiesBlock c : cities) {
                    println("\t" + i + "." + c.type + " Address part size=" + c.length + " bytes");
                }
                if (vInfo != null && vInfo.isVaddress()) {
                    printAddressDetailedInfo(vInfo, index, (AddressRegion) p);
                }
            }
            i++;
        }
    } catch (IOException e) {
        System.err.println("File doesn't have valid structure : " + filename + " " + e.getMessage());
        throw e;
    }
}
Also used : MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) IOException(java.io.IOException) Date(java.util.Date) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapObject(net.osmand.data.MapObject) TIntArrayList(gnu.trove.list.array.TIntArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) CitiesBlock(net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 4 with AddressRegion

use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.

the class BinaryMapIndexReader method searchAddressDataByName.

public List<MapObject> searchAddressDataByName(SearchRequest<MapObject> req, List<Integer> typeFilter) throws IOException {
    for (AddressRegion reg : addressIndexes) {
        if (reg.indexNameOffset != -1) {
            codedIS.seek(reg.indexNameOffset);
            int len = readInt();
            int old = codedIS.pushLimit(len);
            addressAdapter.searchAddressDataByName(reg, req, typeFilter);
            codedIS.popLimit(old);
        }
    }
    return req.getSearchResults();
}
Also used : AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)

Example 5 with AddressRegion

use of net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion in project Osmand by osmandapp.

the class CachedOsmandIndexes method addToCache.

public void addToCache(BinaryMapIndexReader reader, File f) {
    hasChanged = true;
    if (storedIndexBuilder == null) {
        storedIndexBuilder = OsmandIndex.OsmAndStoredIndex.newBuilder();
        storedIndexBuilder.setVersion(VERSION);
        storedIndexBuilder.setDateCreated(System.currentTimeMillis());
        if (storedIndex != null) {
            for (FileIndex ex : storedIndex.getFileIndexList()) {
                storedIndexBuilder.addFileIndex(ex);
            }
        }
    }
    FileIndex.Builder fileIndex = OsmandIndex.FileIndex.newBuilder();
    long d = reader.getDateCreated();
    fileIndex.setDateModified(d == 0 ? f.lastModified() : d);
    fileIndex.setSize(f.length());
    fileIndex.setVersion(reader.getVersion());
    fileIndex.setFileName(f.getName());
    for (MapIndex index : reader.getMapIndexes()) {
        MapPart.Builder map = OsmandIndex.MapPart.newBuilder();
        map.setSize(index.getLength());
        map.setOffset(index.getFilePointer());
        if (index.getName() != null) {
            map.setName(index.getName());
        }
        for (MapRoot mr : index.getRoots()) {
            MapLevel.Builder lev = OsmandIndex.MapLevel.newBuilder();
            lev.setSize(mr.length);
            lev.setOffset(mr.filePointer);
            lev.setLeft(mr.left);
            lev.setRight(mr.right);
            lev.setTop(mr.top);
            lev.setBottom(mr.bottom);
            lev.setMinzoom(mr.minZoom);
            lev.setMaxzoom(mr.maxZoom);
            map.addLevels(lev);
        }
        fileIndex.addMapIndex(map);
    }
    for (AddressRegion index : reader.getAddressIndexes()) {
        AddressPart.Builder addr = OsmandIndex.AddressPart.newBuilder();
        addr.setSize(index.getLength());
        addr.setOffset(index.getFilePointer());
        if (index.getName() != null) {
            addr.setName(index.getName());
        }
        if (index.getEnName() != null) {
            addr.setNameEn(index.getEnName());
        }
        addr.setIndexNameOffset(index.getIndexNameOffset());
        for (CitiesBlock mr : index.getCities()) {
            CityBlock.Builder cblock = OsmandIndex.CityBlock.newBuilder();
            cblock.setSize(mr.length);
            cblock.setOffset(mr.filePointer);
            cblock.setType(mr.type);
            addr.addCities(cblock);
        }
        for (String s : index.getAttributeTagsTable()) {
            addr.addAdditionalTags(s);
        }
        fileIndex.addAddressIndex(addr);
    }
    for (PoiRegion index : reader.getPoiIndexes()) {
        PoiPart.Builder poi = OsmandIndex.PoiPart.newBuilder();
        poi.setSize(index.getLength());
        poi.setOffset(index.getFilePointer());
        if (index.getName() != null) {
            poi.setName(index.getName());
        }
        poi.setLeft(index.left31);
        poi.setRight(index.right31);
        poi.setTop(index.top31);
        poi.setBottom(index.bottom31);
        fileIndex.addPoiIndex(poi.build());
    }
    for (TransportIndex index : reader.getTransportIndexes()) {
        TransportPart.Builder transport = OsmandIndex.TransportPart.newBuilder();
        transport.setSize(index.getLength());
        transport.setOffset(index.getFilePointer());
        if (index.getName() != null) {
            transport.setName(index.getName());
        }
        transport.setLeft(index.getLeft());
        transport.setRight(index.getRight());
        transport.setTop(index.getTop());
        transport.setBottom(index.getBottom());
        transport.setStopsTableLength(index.stopsFileLength);
        transport.setStopsTableOffset(index.stopsFileOffset);
        transport.setStringTableLength(index.stringTable.length);
        transport.setStringTableOffset(index.stringTable.fileOffset);
        fileIndex.addTransportIndex(transport);
    }
    for (RouteRegion index : reader.getRoutingIndexes()) {
        RoutingPart.Builder routing = OsmandIndex.RoutingPart.newBuilder();
        routing.setSize(index.getLength());
        routing.setOffset(index.getFilePointer());
        if (index.getName() != null) {
            routing.setName(index.getName());
        }
        for (RouteSubregion sub : index.getSubregions()) {
            addRouteSubregion(routing, sub, false);
        }
        for (RouteSubregion sub : index.getBaseSubregions()) {
            addRouteSubregion(routing, sub, true);
        }
        fileIndex.addRoutingIndex(routing);
    }
    storedIndexBuilder.addFileIndex(fileIndex);
}
Also used : FileIndex(net.osmand.binary.OsmandIndex.FileIndex) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapPart(net.osmand.binary.OsmandIndex.MapPart) CitiesBlock(net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock) MapLevel(net.osmand.binary.OsmandIndex.MapLevel) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion) RouteSubregion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion) CityBlock(net.osmand.binary.OsmandIndex.CityBlock) RoutingPart(net.osmand.binary.OsmandIndex.RoutingPart) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) TransportPart(net.osmand.binary.OsmandIndex.TransportPart) PoiPart(net.osmand.binary.OsmandIndex.PoiPart) AddressPart(net.osmand.binary.OsmandIndex.AddressPart) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)

Aggregations

AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)12 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)7 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)6 TransportIndex (net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)6 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 CitiesBlock (net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock)4 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)4 MapObject (net.osmand.data.MapObject)4 CodedOutputStream (com.google.protobuf.CodedOutputStream)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 RandomAccessFile (java.io.RandomAccessFile)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 City (net.osmand.data.City)3 TLongArrayList (gnu.trove.list.array.TLongArrayList)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 List (java.util.List)2 BinaryIndexPart (net.osmand.binary.BinaryIndexPart)2