Search in sources :

Example 1 with StreetIndex

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

the class BinaryMapIndexWriter method createStreetAndBuildings.

protected StreetIndex createStreetAndBuildings(Street street, int cx, int cy, String postcodeFilter, Map<Long, Set<Street>> mapNodeToStreet, Map<Street, List<Node>> wayNodes, Map<String, Integer> tagRules) throws IOException {
    checkPeekState(CITY_INDEX_INIT);
    StreetIndex.Builder streetBuilder = OsmandOdb.StreetIndex.newBuilder();
    streetBuilder.setName(street.getName());
    if (checkEnNameToWrite(street)) {
        streetBuilder.setNameEn(street.getEnName(false));
    }
    Iterator<Entry<String, String>> it = street.getNamesMap(false).entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, String> next = it.next();
        Integer intg = tagRules.get("name:" + next.getKey());
        if (intg != null) {
            streetBuilder.addAttributeTagIds(intg);
            streetBuilder.addAttributeValues(next.getValue());
        }
    }
    streetBuilder.setId(street.getId());
    int sx = MapUtils.get31TileNumberX(street.getLocation().getLongitude());
    int sy = MapUtils.get31TileNumberY(street.getLocation().getLatitude());
    streetBuilder.setX((sx - cx) >> 7);
    streetBuilder.setY((sy - cy) >> 7);
    street.sortBuildings();
    for (Building b : street.getBuildings()) {
        if (postcodeFilter != null && !postcodeFilter.equalsIgnoreCase(b.getPostcode())) {
            continue;
        }
        OsmandOdb.BuildingIndex.Builder bbuilder = OsmandOdb.BuildingIndex.newBuilder();
        int bx = MapUtils.get31TileNumberX(b.getLocation().getLongitude());
        int by = MapUtils.get31TileNumberY(b.getLocation().getLatitude());
        bbuilder.setX((bx - sx) >> 7);
        bbuilder.setY((by - sy) >> 7);
        String number2 = b.getName2();
        if (!Algorithms.isEmpty(number2)) {
            LatLon loc = b.getLatLon2();
            if (loc == null) {
                bbuilder.setX((bx - sx) >> 7);
                bbuilder.setY((by - sy) >> 7);
            } else {
                int bcx = MapUtils.get31TileNumberX(loc.getLongitude());
                int bcy = MapUtils.get31TileNumberY(loc.getLatitude());
                bbuilder.setX2((bcx - sx) >> 7);
                bbuilder.setY2((bcy - sy) >> 7);
            }
            bbuilder.setName2(number2);
            if (b.getInterpolationType() != null) {
                bbuilder.setInterpolation(b.getInterpolationType().getValue());
            } else if (b.getInterpolationInterval() > 0) {
                bbuilder.setInterpolation(b.getInterpolationInterval());
            } else {
                bbuilder.setInterpolation(1);
            }
        }
        // bbuilder.setId(b.getId());
        bbuilder.setName(b.getName());
        if (b.getPostcode() != null) {
            bbuilder.setPostcode(b.getPostcode());
        }
        it = b.getNamesMap(false).entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, String> next = it.next();
            Integer intg = tagRules.get("name:" + next.getKey());
            if (intg != null) {
                bbuilder.addAttributeTagIds(intg);
                bbuilder.addAttributeValues(next.getValue());
            }
        }
        if (checkEnNameToWrite(b)) {
            bbuilder.setNameEn(b.getEnName(false));
        }
        if (postcodeFilter == null && b.getPostcode() != null) {
            bbuilder.setPostcode(b.getPostcode());
        }
        streetBuilder.addBuildings(bbuilder.build());
    }
    if (wayNodes != null) {
        Set<Street> checkedStreets = new TreeSet<Street>();
        for (Node intersection : wayNodes.get(street)) {
            for (Street streetJ : mapNodeToStreet.get(intersection.getId())) {
                if (checkedStreets.contains(streetJ) || streetJ.getId().longValue() == street.getId().longValue()) {
                    continue;
                }
                checkedStreets.add(streetJ);
                StreetIntersection.Builder builder = OsmandOdb.StreetIntersection.newBuilder();
                int ix = MapUtils.get31TileNumberX(intersection.getLongitude());
                int iy = MapUtils.get31TileNumberY(intersection.getLatitude());
                builder.setIntersectedX((ix - sx) >> 7);
                builder.setIntersectedY((iy - sy) >> 7);
                builder.setName(streetJ.getName());
                if (checkEnNameToWrite(streetJ)) {
                    builder.setNameEn(streetJ.getEnName(false));
                }
                it = streetJ.getNamesMap(false).entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, String> next = it.next();
                    Integer intg = tagRules.get("name:" + next.getKey());
                    if (intg != null) {
                        builder.addAttributeTagIds(intg);
                        builder.addAttributeValues(next.getValue());
                    }
                }
                streetBuilder.addIntersections(builder.build());
            }
        }
    }
    return streetBuilder.build();
}
Also used : Building(net.osmand.data.Building) Node(net.osmand.osm.edit.Node) ByteString(com.google.protobuf.ByteString) LatLon(net.osmand.data.LatLon) StreetIndex(net.osmand.binary.OsmandOdb.StreetIndex) Entry(java.util.Map.Entry) TreeSet(java.util.TreeSet) Street(net.osmand.data.Street) StreetIntersection(net.osmand.binary.OsmandOdb.StreetIntersection)

Example 2 with StreetIndex

use of net.osmand.binary.OsmandOdb.StreetIndex 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)2 TreeSet (java.util.TreeSet)2 StreetIndex (net.osmand.binary.OsmandOdb.StreetIndex)2 Street (net.osmand.data.Street)2 Node (net.osmand.osm.edit.Node)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 CityBlockIndex (net.osmand.binary.OsmandOdb.CityBlockIndex)1 StreetIntersection (net.osmand.binary.OsmandOdb.StreetIntersection)1 Building (net.osmand.data.Building)1 LatLon (net.osmand.data.LatLon)1