Search in sources :

Example 6 with PoiRegion

use of net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion in project Osmand by osmandapp.

the class BinaryMapIndexReader method searchPoiCategoriesByName.

public Map<PoiCategory, List<String>> searchPoiCategoriesByName(String query, Map<PoiCategory, List<String>> map) throws IOException {
    if (query == null || query.length() == 0) {
        throw new IllegalArgumentException();
    }
    Collator collator = OsmAndCollator.primaryCollator();
    for (PoiRegion poiIndex : poiIndexes) {
        poiAdapter.initCategories(poiIndex);
        for (int i = 0; i < poiIndex.categories.size(); i++) {
            String cat = poiIndex.categories.get(i);
            PoiCategory catType = poiIndex.categoriesType.get(i);
            if (CollatorStringMatcher.cmatches(collator, cat, query, StringMatcherMode.CHECK_STARTS_FROM_SPACE)) {
                map.put(catType, null);
            } else {
                List<String> subcats = poiIndex.subcategories.get(i);
                for (int j = 0; j < subcats.size(); j++) {
                    if (CollatorStringMatcher.cmatches(collator, subcats.get(j), query, StringMatcherMode.CHECK_STARTS_FROM_SPACE)) {
                        if (!map.containsKey(catType)) {
                            map.put(catType, new ArrayList<String>());
                        }
                        List<String> list = map.get(catType);
                        if (list != null) {
                            list.add(subcats.get(j));
                        }
                    }
                }
            }
        }
    }
    return map;
}
Also used : PoiCategory(net.osmand.osm.PoiCategory) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion) OsmAndCollator(net.osmand.OsmAndCollator) Collator(net.osmand.Collator)

Example 7 with PoiRegion

use of net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion 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)

Example 8 with PoiRegion

use of net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion in project Osmand by osmandapp.

the class CachedOsmandIndexes method initFileIndex.

private BinaryMapIndexReader initFileIndex(FileIndex found, RandomAccessFile mf, File f) throws IOException {
    BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f, false);
    reader.version = found.getVersion();
    reader.dateCreated = found.getDateModified();
    for (MapPart index : found.getMapIndexList()) {
        MapIndex mi = new MapIndex();
        mi.length = (int) index.getSize();
        mi.filePointer = (int) index.getOffset();
        mi.name = index.getName();
        for (MapLevel mr : index.getLevelsList()) {
            MapRoot root = new MapRoot();
            root.length = (int) mr.getSize();
            root.filePointer = (int) mr.getOffset();
            root.left = mr.getLeft();
            root.right = mr.getRight();
            root.top = mr.getTop();
            root.bottom = mr.getBottom();
            root.minZoom = mr.getMinzoom();
            root.maxZoom = mr.getMaxzoom();
            mi.roots.add(root);
        }
        reader.mapIndexes.add(mi);
        reader.indexes.add(mi);
        reader.basemap = reader.basemap || mi.isBaseMap();
    }
    for (AddressPart index : found.getAddressIndexList()) {
        AddressRegion mi = new AddressRegion();
        mi.length = (int) index.getSize();
        mi.filePointer = (int) index.getOffset();
        mi.name = index.getName();
        mi.enName = index.getNameEn();
        mi.indexNameOffset = index.getIndexNameOffset();
        for (CityBlock mr : index.getCitiesList()) {
            CitiesBlock cblock = new CitiesBlock();
            cblock.length = (int) mr.getSize();
            cblock.filePointer = (int) mr.getOffset();
            cblock.type = mr.getType();
            mi.cities.add(cblock);
        }
        for (String s : index.getAdditionalTagsList()) {
            mi.attributeTagsTable.add(s);
        }
        reader.addressIndexes.add(mi);
        reader.indexes.add(mi);
    }
    for (PoiPart index : found.getPoiIndexList()) {
        PoiRegion mi = new PoiRegion();
        mi.length = (int) index.getSize();
        mi.filePointer = (int) index.getOffset();
        mi.name = index.getName();
        mi.left31 = index.getLeft();
        mi.right31 = index.getRight();
        mi.top31 = index.getTop();
        mi.bottom31 = index.getBottom();
        reader.poiIndexes.add(mi);
        reader.indexes.add(mi);
    }
    for (TransportPart index : found.getTransportIndexList()) {
        TransportIndex mi = new TransportIndex();
        mi.length = (int) index.getSize();
        mi.filePointer = (int) index.getOffset();
        mi.name = index.getName();
        mi.left = index.getLeft();
        mi.right = index.getRight();
        mi.top = index.getTop();
        mi.bottom = index.getBottom();
        mi.stopsFileLength = index.getStopsTableLength();
        mi.stopsFileOffset = index.getStopsTableOffset();
        mi.stringTable = new IndexStringTable();
        mi.stringTable.fileOffset = index.getStringTableOffset();
        mi.stringTable.length = index.getStringTableLength();
        reader.transportIndexes.add(mi);
        reader.indexes.add(mi);
    }
    for (RoutingPart index : found.getRoutingIndexList()) {
        RouteRegion mi = new RouteRegion();
        mi.length = (int) index.getSize();
        mi.filePointer = (int) index.getOffset();
        mi.name = index.getName();
        for (RoutingSubregion mr : index.getSubregionsList()) {
            RouteSubregion sub = new RouteSubregion(mi);
            sub.length = (int) mr.getSize();
            sub.filePointer = (int) mr.getOffset();
            sub.left = mr.getLeft();
            sub.right = mr.getRight();
            sub.top = mr.getTop();
            sub.bottom = mr.getBottom();
            sub.shiftToData = mr.getShifToData();
            if (mr.getBasemap()) {
                mi.basesubregions.add(sub);
            } else {
                mi.subregions.add(sub);
            }
        }
        reader.routingIndexes.add(mi);
        reader.indexes.add(mi);
    }
    return reader;
}
Also used : RouteSubregion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion) CityBlock(net.osmand.binary.OsmandIndex.CityBlock) RoutingPart(net.osmand.binary.OsmandIndex.RoutingPart) RoutingSubregion(net.osmand.binary.OsmandIndex.RoutingSubregion) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) TransportPart(net.osmand.binary.OsmandIndex.TransportPart) IndexStringTable(net.osmand.binary.BinaryMapTransportReaderAdapter.IndexStringTable) PoiPart(net.osmand.binary.OsmandIndex.PoiPart) AddressPart(net.osmand.binary.OsmandIndex.AddressPart) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapPart(net.osmand.binary.OsmandIndex.MapPart) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) CitiesBlock(net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock) MapLevel(net.osmand.binary.OsmandIndex.MapLevel) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 9 with PoiRegion

use of net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion in project Osmand by osmandapp.

the class BinaryInspector method combineParts.

@SuppressWarnings("unchecked")
public static List<Float> combineParts(File fileToExtract, Map<File, String> partsToExtractFrom) throws IOException {
    BinaryMapIndexReader[] indexes = new BinaryMapIndexReader[partsToExtractFrom.size()];
    RandomAccessFile[] rafs = new RandomAccessFile[partsToExtractFrom.size()];
    LinkedHashSet<Float>[] partsSet = new LinkedHashSet[partsToExtractFrom.size()];
    int c = 0;
    Set<String> addressNames = new LinkedHashSet<String>();
    int version = -1;
    // Go through all files and validate conistency
    for (File f : partsToExtractFrom.keySet()) {
        if (f.getAbsolutePath().equals(fileToExtract.getAbsolutePath())) {
            System.err.println("Error : Input file is equal to output file " + f.getAbsolutePath());
            return null;
        }
        rafs[c] = new RandomAccessFile(f.getAbsolutePath(), "r");
        indexes[c] = new BinaryMapIndexReader(rafs[c], f);
        partsSet[c] = new LinkedHashSet<Float>();
        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 null;
            }
        }
        LinkedHashSet<Float> temp = new LinkedHashSet<Float>();
        String pattern = partsToExtractFrom.get(f);
        boolean minus = true;
        for (int i = 0; i < indexes[c].getIndexes().size(); i++) {
            partsSet[c].add(i + 1f);
            BinaryIndexPart part = indexes[c].getIndexes().get(i);
            if (part instanceof MapIndex) {
                List<MapRoot> roots = ((MapIndex) part).getRoots();
                int rsize = roots.size();
                for (int j = 0; j < rsize; j++) {
                    partsSet[c].add((i + 1f) + (j + 1) / 10f);
                }
            }
        }
        if (pattern != null) {
            minus = pattern.startsWith("-");
            String[] split = pattern.substring(1).split(",");
            for (String s : split) {
                temp.add(Float.valueOf(s));
            }
        }
        Iterator<Float> p = partsSet[c].iterator();
        while (p.hasNext()) {
            Float part = p.next();
            if (minus) {
                if (temp.contains(part)) {
                    p.remove();
                }
            } else {
                if (!temp.contains(part)) {
                    p.remove();
                }
            }
        }
        c++;
    }
    // write files
    FileOutputStream fout = new FileOutputStream(fileToExtract);
    CodedOutputStream ous = CodedOutputStream.newInstance(fout, BUFFER_SIZE);
    List<Float> list = new ArrayList<Float>();
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, System.currentTimeMillis());
    for (int k = 0; k < indexes.length; k++) {
        LinkedHashSet<Float> partSet = partsSet[k];
        BinaryMapIndexReader index = indexes[k];
        RandomAccessFile raf = rafs[k];
        for (int i = 0; i < index.getIndexes().size(); i++) {
            if (!partSet.contains(Float.valueOf(i + 1f))) {
                continue;
            }
            list.add(i + 1f);
            BinaryIndexPart part = index.getIndexes().get(i);
            String map;
            if (part instanceof MapIndex) {
                ous.writeTag(OsmandOdb.OsmAndStructure.MAPINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Map";
            } else if (part instanceof AddressRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Address";
                if (addressNames.contains(part.getName())) {
                    System.err.println("Error : going to merge 2 addresses with same names. Skip " + part.getName());
                    continue;
                }
                addressNames.add(part.getName());
            } else if (part instanceof TransportIndex) {
                ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Transport";
            } else if (part instanceof PoiRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "POI";
            } else if (part instanceof RouteRegion) {
                ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
                map = "Routing";
            } else {
                throw new UnsupportedOperationException();
            }
            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(), map }));
        }
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
    fout.close();
    return list;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion) CodedOutputStream(com.google.protobuf.CodedOutputStream) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) RandomAccessFile(java.io.RandomAccessFile) FileOutputStream(java.io.FileOutputStream) MapObject(net.osmand.data.MapObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 10 with PoiRegion

use of net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion in project Osmand by osmandapp.

the class BinaryMapIndexReader method searchPoiByName.

public List<Amenity> searchPoiByName(SearchRequest<Amenity> req) throws IOException {
    if (req.nameQuery == null || req.nameQuery.length() == 0) {
        throw new IllegalArgumentException();
    }
    for (PoiRegion poiIndex : poiIndexes) {
        poiAdapter.initCategories(poiIndex);
        codedIS.seek(poiIndex.filePointer);
        int old = codedIS.pushLimit(poiIndex.length);
        poiAdapter.searchPoiByName(poiIndex, req);
        codedIS.popLimit(old);
    }
    return req.getSearchResults();
}
Also used : PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Aggregations

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