Search in sources :

Example 1 with FileIndex

use of net.osmand.binary.OsmandIndex.FileIndex in project Osmand by osmandapp.

the class CachedOsmandIndexes method getReader.

public BinaryMapIndexReader getReader(File f) throws IOException {
    RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r");
    FileIndex found = null;
    if (storedIndex != null) {
        for (int i = 0; i < storedIndex.getFileIndexCount(); i++) {
            FileIndex fi = storedIndex.getFileIndex(i);
            if (f.length() == fi.getSize() && f.getName().equals(fi.getFileName())) {
                // f.lastModified() == fi.getDateModified()
                found = fi;
                break;
            }
        }
    }
    BinaryMapIndexReader reader = null;
    if (found == null) {
        long val = System.currentTimeMillis();
        reader = new BinaryMapIndexReader(mf, f);
        addToCache(reader, f);
        if (log.isDebugEnabled()) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            log.debug("Initializing db " + f.getAbsolutePath() + " " + (System.currentTimeMillis() - val) + "ms");
        }
    } else {
        reader = initFileIndex(found, mf, f);
    }
    return reader;
}
Also used : FileIndex(net.osmand.binary.OsmandIndex.FileIndex) RandomAccessFile(java.io.RandomAccessFile)

Example 2 with FileIndex

use of net.osmand.binary.OsmandIndex.FileIndex 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

FileIndex (net.osmand.binary.OsmandIndex.FileIndex)2 RandomAccessFile (java.io.RandomAccessFile)1 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)1 CitiesBlock (net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock)1 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)1 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)1 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)1 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)1 RouteSubregion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion)1 TransportIndex (net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)1 AddressPart (net.osmand.binary.OsmandIndex.AddressPart)1 CityBlock (net.osmand.binary.OsmandIndex.CityBlock)1 MapLevel (net.osmand.binary.OsmandIndex.MapLevel)1 MapPart (net.osmand.binary.OsmandIndex.MapPart)1 PoiPart (net.osmand.binary.OsmandIndex.PoiPart)1 RoutingPart (net.osmand.binary.OsmandIndex.RoutingPart)1 TransportPart (net.osmand.binary.OsmandIndex.TransportPart)1