Search in sources :

Example 1 with CityBlockIndex

use of net.osmand.binary.OsmandOdb.CityBlockIndex in project OsmAnd-tools by osmandapp.

the class BinaryMapIndexWriter method writeCityIndex.

public void writeCityIndex(City cityOrPostcode, List<Street> streets, Map<Street, List<Node>> wayNodes, BinaryFileReference ref, Map<String, Integer> tagRules) throws IOException {
    checkPeekState(CITY_INDEX_INIT);
    codedOutStream.writeTag(CitiesIndex.BLOCKS_FIELD_NUMBER, FieldType.MESSAGE.getWireType());
    codedOutStream.flush();
    long startMessage = getFilePointer();
    long startCityBlock = ref.getStartPointer();
    ref.writeReference(raf, startMessage);
    CityBlockIndex.Builder cityInd = OsmandOdb.CityBlockIndex.newBuilder();
    cityInd.setShiftToCityIndex((int) (startMessage - startCityBlock));
    long currentPointer = startMessage + 4 + CodedOutputStream.computeTagSize(CityBlockIndex.SHIFTTOCITYINDEX_FIELD_NUMBER);
    int cx = MapUtils.get31TileNumberX(cityOrPostcode.getLocation().getLongitude());
    int cy = MapUtils.get31TileNumberY(cityOrPostcode.getLocation().getLatitude());
    Map<Long, Set<Street>> mapNodeToStreet = new LinkedHashMap<Long, Set<Street>>();
    if (wayNodes != null) {
        for (int i = 0; i < streets.size(); i++) {
            for (Node n : wayNodes.get(streets.get(i))) {
                if (!mapNodeToStreet.containsKey(n.getId())) {
                    mapNodeToStreet.put(n.getId(), new LinkedHashSet<Street>(3));
                }
                mapNodeToStreet.get(n.getId()).add(streets.get(i));
            }
        }
    }
    String postcodeFilter = cityOrPostcode.isPostcode() ? cityOrPostcode.getName() : null;
    for (Street s : streets) {
        StreetIndex streetInd = createStreetAndBuildings(s, cx, cy, postcodeFilter, mapNodeToStreet, wayNodes, tagRules);
        currentPointer += CodedOutputStream.computeTagSize(CityBlockIndex.STREETS_FIELD_NUMBER);
        if (currentPointer > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("File offset > 2 GB.");
        }
        s.setFileOffset((int) currentPointer);
        currentPointer += CodedOutputStream.computeMessageSizeNoTag(streetInd);
        cityInd.addStreets(streetInd);
    }
    CityBlockIndex block = cityInd.build();
    int size = CodedOutputStream.computeRawVarint32Size(block.getSerializedSize());
    codedOutStream.writeMessageNoTag(block);
    for (Street s : streets) {
        s.setFileOffset(s.getFileOffset() + size);
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Node(net.osmand.osm.edit.Node) ByteString(com.google.protobuf.ByteString) LinkedHashMap(java.util.LinkedHashMap) StreetIndex(net.osmand.binary.OsmandOdb.StreetIndex) CityBlockIndex(net.osmand.binary.OsmandOdb.CityBlockIndex) Street(net.osmand.data.Street)

Aggregations

ByteString (com.google.protobuf.ByteString)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 CityBlockIndex (net.osmand.binary.OsmandOdb.CityBlockIndex)1 StreetIndex (net.osmand.binary.OsmandOdb.StreetIndex)1 Street (net.osmand.data.Street)1 Node (net.osmand.osm.edit.Node)1