Search in sources :

Example 1 with MapZoomPair

use of net.osmand.binary.MapZooms.MapZoomPair in project OsmAnd-tools by osmandapp.

the class BasemapProcessor method writeBasemapFile.

public void writeBasemapFile(BinaryMapIndexWriter writer, String regionName) throws IOException {
    writer.startWriteMapIndex(regionName);
    // write map encoding rules
    writer.writeMapEncodingRules(renderingTypes.getEncodingRuleTypes());
    int i = 0;
    for (MapZoomPair p : mapZooms.getLevels()) {
        // write map levels and map index
        writer.startWriteMapLevelIndex(p.getMinZoom(), p.getMaxZoom(), 0, (1 << 31) - 1, 0, (1 << 31) - 1);
        Map<SimplisticQuadTree, BinaryFileReference> refs = new LinkedHashMap<BasemapProcessor.SimplisticQuadTree, BinaryFileReference>();
        writeBinaryMapTree(quadTrees[i], writer, refs, p);
        // without data blocks
        writeBinaryMapBlock(quadTrees[i], writer, refs, p);
        writer.endWriteMapLevelIndex();
        i++;
    }
    writer.endWriteMapIndex();
    writer.flush();
}
Also used : MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with MapZoomPair

use of net.osmand.binary.MapZooms.MapZoomPair in project OsmAnd-tools by osmandapp.

the class BasemapProcessor method processEntity.

public void processEntity(Entity e) {
    if (e instanceof Way) {
        if ("reverse_coastline".equals(((Way) e).getModifiableTags().get("natural"))) {
            ((Way) e).putTag("natural", "coastline");
            Collections.reverse(((Way) e).getNodes());
            ((Way) e).getNodeIds().reverse();
        }
    }
    long refId = -Math.abs(e.getId());
    for (int level = 0; level < mapZooms.getLevels().size(); level++) {
        boolean mostDetailed = level == 0;
        MapZoomPair zoomPair = mapZooms.getLevel(level);
        int zoomToEncode = mostDetailed ? Math.max(MOST_DETAILED_APPROXIMATION, zoomPair.getMinZoom() + 1) : zoomPair.getMaxZoom();
        if (mostDetailed && zoomPair.getMaxZoom() < 10) {
            throw new IllegalStateException("Zoom pair is not detailed " + zoomPair);
        }
        renderingTypes.encodeEntityWithType(e, zoomToEncode, typeUse, addtypeUse, namesUse, tempNameUse);
        if (typeUse.isEmpty()) {
            continue;
        }
        if (e instanceof Relation) {
            Relation r = (Relation) e;
            Iterator<RelationMember> it = r.getMembers().iterator();
            List<Node> outer = null;
            List<List<Node>> inner = new ArrayList<List<Node>>();
            while (it.hasNext()) {
                RelationMember n = it.next();
                if (n.getRole().equals("outer")) {
                    if (outer != null) {
                        throw new IllegalStateException("2 outer lines for relation = " + e.getId());
                    }
                    outer = ((Way) n.getEntity()).getNodes();
                } else if (n.getRole().equals("inner")) {
                    inner.add(((Way) n.getEntity()).getNodes());
                }
            }
            if (OsmMapUtils.polygonAreaPixels(outer, zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                continue;
            }
            addObject(refId, level, zoomPair, zoomToEncode, outer, inner);
        } else if (e instanceof Way) {
            if (((Way) e).getNodes().size() < 2) {
                continue;
            }
            double dist = OsmMapUtils.getDistance(((Way) e).getFirstNode(), ((Way) e).getLastNode());
            boolean polygon = dist < 100;
            if ("coastline".equals(e.getTag("natural"))) {
                if (polygon && !mostDetailed) {
                    if (OsmMapUtils.polygonAreaPixels(((Way) e).getNodes(), zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                }
                splitContinuousWay(((Way) e).getNodes(), typeUse.toArray(), !addtypeUse.isEmpty() ? addtypeUse.toArray() : null, zoomPair, zoomToEncode, quadTrees[level], refId);
            } else {
                List<Node> ns = ((Way) e).getNodes();
                if (!polygon) {
                    QuadRect qr = ((Way) e).getLatLonBBox();
                    if (qr == null) {
                        continue;
                    }
                    double mult = 1 / MapUtils.getPowZoom(Math.max(31 - (zoomToEncode + 8), 0));
                    int rx = MapUtils.get31TileNumberX(qr.right);
                    int lx = MapUtils.get31TileNumberX(qr.left);
                    int by = MapUtils.get31TileNumberY(qr.bottom);
                    int ty = MapUtils.get31TileNumberY(qr.top);
                    if (mult * (rx - lx) < PIXELS_THRESHOLD_AREA && mult * (by - ty) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                } else {
                    if (OsmMapUtils.polygonAreaPixels(ns, zoomToEncode) < PIXELS_THRESHOLD_AREA) {
                        continue;
                    }
                }
                addObject(refId, level, zoomPair, zoomToEncode, ns, null);
            }
        } else {
            int z = getViewZoom(zoomPair.getMinZoom(), zoomToEncode);
            int tilex = (int) MapUtils.getTileNumberX(z, ((Node) e).getLongitude());
            int tiley = (int) MapUtils.getTileNumberY(z, ((Node) e).getLatitude());
            addRawData(Collections.singletonList((Node) e), null, typeUse.toArray(), !addtypeUse.isEmpty() ? addtypeUse.toArray() : null, zoomPair, quadTrees[level], z, tilex, tiley, namesUse.isEmpty() ? null : new LinkedHashMap<MapRulType, String>(namesUse), refId);
        }
    }
}
Also used : Node(net.osmand.osm.edit.Node) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) QuadRect(net.osmand.data.QuadRect) Way(net.osmand.osm.edit.Way) MapRulType(net.osmand.osm.MapRenderingTypes.MapRulType) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) Relation(net.osmand.osm.edit.Relation) RelationMember(net.osmand.osm.edit.Relation.RelationMember) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 3 with MapZoomPair

use of net.osmand.binary.MapZooms.MapZoomPair in project OsmAnd-tools by osmandapp.

the class ObfDiffGenerator method compareMapData.

private void compareMapData(ObfFileInMemory fStart, ObfFileInMemory fEnd, boolean print, Set<EntityId> modifiedObjIds) {
    fStart.filterAllZoomsBelow(13);
    fEnd.filterAllZoomsBelow(13);
    MapIndex mi = fEnd.getMapIndex();
    int deleteId;
    Integer rl = mi.getRule(OSMAND_CHANGE_TAG, OSMAND_CHANGE_VALUE);
    if (rl != null) {
        deleteId = rl;
    } else {
        deleteId = mi.decodingRules.size() + 1;
        mi.initMapEncodingRule(0, deleteId, OSMAND_CHANGE_TAG, OSMAND_CHANGE_VALUE);
    }
    for (MapZoomPair mz : fStart.getZooms()) {
        TLongObjectHashMap<BinaryMapDataObject> startData = fStart.get(mz);
        TLongObjectHashMap<BinaryMapDataObject> endData = fEnd.get(mz);
        if (print) {
            System.out.println("Compare map " + mz);
        }
        if (endData == null) {
            continue;
        }
        for (Long idx : startData.keys()) {
            BinaryMapDataObject objE = endData.get(idx);
            BinaryMapDataObject objS = startData.get(idx);
            EntityId thisEntityId = getMapEntityId(objS.getId());
            if (print) {
                if (objE == null) {
                    System.out.println("Map " + idx + " is missing in (2): " + toString(objS));
                } else {
                    if (// !objS.getMapIndex().decodeType(objS.getTypes()[0]).tag.equals(OSMAND_CHANGE_TAG) &&
                    !objE.compareBinary(objS, COORDINATES_PRECISION_COMPARE)) {
                        System.out.println("Map " + idx + " is not equal: " + toString(objS) + " != " + toString(objE));
                    }
                    endData.remove(idx);
                }
            } else {
                if (objE == null) {
                    if (modifiedObjIds == null || modifiedObjIds.contains(thisEntityId) || thisEntityId == null) {
                        BinaryMapDataObject obj = new BinaryMapDataObject(idx, objS.getCoordinates(), null, objS.getObjectType(), objS.isArea(), new int[] { deleteId }, null);
                        endData.put(idx, obj);
                    }
                } else if (objE.compareBinary(objS, COORDINATES_PRECISION_COMPARE)) {
                    endData.remove(idx);
                }
            }
        }
        if (print) {
            for (BinaryMapDataObject e : endData.valueCollection()) {
                System.out.println("Map " + e.getId() + " is missing in (1): " + toString(e));
            }
        }
    }
}
Also used : EntityId(net.osmand.osm.edit.Entity.EntityId) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

Example 4 with MapZoomPair

use of net.osmand.binary.MapZooms.MapZoomPair in project OsmAnd-tools by osmandapp.

the class ObfFileInMemory method writeFile.

public void writeFile(File targetFile, boolean doNotSimplifyObjects) throws IOException, RTreeException, SQLException {
    boolean gzip = targetFile.getName().endsWith(".gz");
    File nonGzip = targetFile;
    if (gzip) {
        nonGzip = new File(targetFile.getParentFile(), targetFile.getName().substring(0, targetFile.getName().length() - 3));
    }
    final RandomAccessFile raf = new RandomAccessFile(nonGzip, "rw");
    // write files
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

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

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

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            raf.write(b, off, len);
        }
    });
    timestamp = timestamp == 0 ? System.currentTimeMillis() : timestamp;
    int version = IndexConstants.BINARY_MAP_VERSION;
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, version);
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, timestamp);
    BinaryMapIndexWriter writer = new BinaryMapIndexWriter(raf, ous);
    String defName = targetFile.getName().substring(0, targetFile.getName().indexOf('.'));
    if (mapObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteMapIndex(Algorithms.capitalizeFirstLetter(name));
        writer.writeMapEncodingRules(mapIndex.decodingRules);
        Iterator<Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> it = mapObjects.entrySet().iterator();
        while (it.hasNext()) {
            Entry<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> n = it.next();
            writeMapData(writer, n.getKey(), n.getValue(), targetFile, doNotSimplifyObjects);
        }
        writer.endWriteMapIndex();
    }
    if (routeObjects.size() > 0) {
        String name = mapIndex.getName();
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        writer.startWriteRouteIndex(name);
        writer.writeRouteRawEncodingRules(routeIndex.routeEncodingRules);
        writeRouteData(writer, routeObjects, targetFile);
        writer.endWriteRouteIndex();
    }
    if (poiObjects.size() > 0) {
        String name = "";
        boolean overwriteIds = false;
        if (Algorithms.isEmpty(name)) {
            name = defName;
        }
        MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
        final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
        File poiFile = new File(targetFile.getParentFile(), IndexCreator.getPoiFileName(name));
        indexPoiCreator.createDatabaseStructure(poiFile);
        for (Map<String, Amenity> mp : poiObjects.valueCollection()) {
            for (Amenity a : mp.values()) {
                indexPoiCreator.insertAmenityIntoPoi(a);
            }
        }
        indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
        indexPoiCreator.commitAndClosePoiFile(System.currentTimeMillis());
        indexPoiCreator.removePoiFile();
    }
    // TODO Write Transport
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, version);
    ous.flush();
    raf.close();
    if (gzip) {
        nonGzip.setLastModified(timestamp);
        FileInputStream fis = new FileInputStream(nonGzip);
        GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(targetFile));
        Algorithms.streamCopy(fis, gzout);
        fis.close();
        gzout.close();
        nonGzip.delete();
    }
    targetFile.setLastModified(timestamp);
}
Also used : Amenity(net.osmand.data.Amenity) CodedOutputStream(com.google.protobuf.CodedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) IOException(java.io.IOException) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) FileInputStream(java.io.FileInputStream) Entry(java.util.Map.Entry) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexWriter(net.osmand.data.preparation.BinaryMapIndexWriter) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 5 with MapZoomPair

use of net.osmand.binary.MapZooms.MapZoomPair in project OsmAnd-tools by osmandapp.

the class ObfRegionSplitter method split.

private void split(String[] args) throws IOException {
    File worldObf = new File(args[0]);
    File ocbfFile = new File(args[2]);
    File dir = new File(args[1]);
    String subFolder = args.length > 3 ? args[3] : "";
    String fileSuffix = args.length > 4 ? args[4] : "";
    if (!worldObf.exists() || !ocbfFile.exists()) {
        System.out.println("Incorrect file!");
        System.exit(1);
    }
    if (!dir.exists()) {
        dir.mkdir();
    }
    try {
        ObfFileInMemory fl = new ObfFileInMemory();
        fl.readObfFiles(Collections.singletonList(worldObf));
        OsmandRegions osmandRegions = new OsmandRegions();
        osmandRegions.prepareFile(ocbfFile.getAbsolutePath());
        osmandRegions.cacheAllCountries();
        Map<String, Map<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>>> regionsMapData = splitRegionMapData(fl, osmandRegions);
        Map<String, TLongObjectHashMap<RouteDataObject>> regionsRouteData = splitRegionRouteData(fl, osmandRegions);
        Map<String, TLongObjectHashMap<Map<String, Amenity>>> regionsPoiData = splitRegionPoiData(fl, osmandRegions);
        TreeSet<String> regionNames = new TreeSet<>();
        regionNames.addAll(regionsMapData.keySet());
        regionNames.addAll(regionsRouteData.keySet());
        for (String regionName : regionNames) {
            File folder = new File(dir, regionName);
            if (!Algorithms.isEmpty(subFolder)) {
                folder = new File(folder, subFolder);
            }
            folder.mkdirs();
            File result = new File(folder, Algorithms.capitalizeFirstLetter(regionName) + fileSuffix + ".obf.gz");
            ObfFileInMemory obf = new ObfFileInMemory();
            Map<MapZoomPair, TLongObjectHashMap<BinaryMapDataObject>> mp = regionsMapData.get(regionName);
            if (mp != null) {
                for (MapZoomPair mzPair : mp.keySet()) {
                    obf.putMapObjects(mzPair, mp.get(mzPair).valueCollection(), true);
                }
            }
            TLongObjectHashMap<RouteDataObject> ro = regionsRouteData.get(regionName);
            if (ro != null) {
                obf.putRoutingData(ro, true);
            }
            TLongObjectHashMap<Map<String, Amenity>> poi = regionsPoiData.get(regionName);
            if (poi != null) {
                obf.putPoiData(poi, true);
            }
            // TODO split Transport
            obf.updateTimestamp(fl.getTimestamp());
            obf.writeFile(result, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : Amenity(net.osmand.data.Amenity) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) IOException(java.io.IOException) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) OsmandRegions(net.osmand.map.OsmandRegions) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TreeSet(java.util.TreeSet) RouteDataObject(net.osmand.binary.RouteDataObject) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) Map(java.util.Map)

Aggregations

MapZoomPair (net.osmand.binary.MapZooms.MapZoomPair)6 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Amenity (net.osmand.data.Amenity)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 TreeSet (java.util.TreeSet)1