Search in sources :

Example 6 with BinaryMapDataObject

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

the class ObfFileInMemory method readObfFiles.

public void readObfFiles(List<File> files) throws IOException {
    for (int i = 0; i < files.size(); i++) {
        File inputFile = files.get(i);
        File nonGzip = inputFile;
        boolean gzip = false;
        if (inputFile.getName().endsWith(".gz")) {
            nonGzip = new File(inputFile.getParentFile(), inputFile.getName().substring(0, inputFile.getName().length() - 3));
            GZIPInputStream gzin = new GZIPInputStream(new FileInputStream(inputFile));
            FileOutputStream fous = new FileOutputStream(nonGzip);
            Algorithms.streamCopy(gzin, fous);
            fous.close();
            gzin.close();
            gzip = true;
        }
        RandomAccessFile raf = new RandomAccessFile(nonGzip, "r");
        BinaryMapIndexReader indexReader = new BinaryMapIndexReader(raf, nonGzip);
        for (BinaryIndexPart p : indexReader.getIndexes()) {
            if (p instanceof MapIndex) {
                MapIndex mi = (MapIndex) p;
                for (MapRoot mr : mi.getRoots()) {
                    MapZooms.MapZoomPair pair = new MapZooms.MapZoomPair(mr.getMinZoom(), mr.getMaxZoom());
                    TLongObjectHashMap<BinaryMapDataObject> objects = readBinaryMapData(indexReader, mi, mr.getMinZoom());
                    putMapObjects(pair, objects.valueCollection(), true);
                }
            } else if (p instanceof RouteRegion) {
                RouteRegion rr = (RouteRegion) p;
                readRoutingData(indexReader, rr, ZOOM_LEVEL_ROUTING, true);
            } else if (p instanceof PoiRegion) {
                PoiRegion pr = (PoiRegion) p;
                TLongObjectHashMap<Map<String, Amenity>> rr = readPoiData(indexReader, pr, ZOOM_LEVEL_POI, true);
                putPoiData(rr, true);
            } else if (p instanceof TransportIndex) {
            // read all data later
            }
        }
        readTransportData(indexReader, true);
        updateTimestamp(indexReader.getDateCreated());
        indexReader.close();
        raf.close();
        if (gzip) {
            nonGzip.delete();
        }
    }
}
Also used : MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) BinaryIndexPart(net.osmand.binary.BinaryIndexPart) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) MapRoot(net.osmand.binary.BinaryMapIndexReader.MapRoot) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) MapZoomPair(net.osmand.binary.MapZooms.MapZoomPair) RandomAccessFile(java.io.RandomAccessFile) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) FileOutputStream(java.io.FileOutputStream) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapZooms(net.osmand.binary.MapZooms) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TreeMap(java.util.TreeMap) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 7 with BinaryMapDataObject

use of net.osmand.binary.BinaryMapDataObject 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 8 with BinaryMapDataObject

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

Example 9 with BinaryMapDataObject

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

the class ObfRegionSplitter method splitRegionRouteData.

private Map<String, TLongObjectHashMap<RouteDataObject>> splitRegionRouteData(ObfFileInMemory fl, OsmandRegions osmandRegions) throws IOException {
    Map<String, TLongObjectHashMap<RouteDataObject>> result = new HashMap<>();
    TLongObjectHashMap<RouteDataObject> routingData = fl.getRoutingData();
    for (RouteDataObject obj : routingData.valueCollection()) {
        // if(obj.getPointsLength() == 0) {
        // continue;
        // }
        int x = obj.getPoint31XTile(0);
        int y = obj.getPoint31YTile(0);
        List<BinaryMapDataObject> l = osmandRegions.query(x, y);
        for (BinaryMapDataObject b : l) {
            if (osmandRegions.contain(b, x, y)) {
                String dw = osmandRegions.getDownloadName(b);
                WorldRegion wr = osmandRegions.getRegionDataByDownloadName(dw);
                if (dw == null || wr == null) {
                    continue;
                }
                if (!Algorithms.isEmpty(dw) && wr.isRegionMapDownload()) {
                    TLongObjectHashMap<RouteDataObject> mp = result.get(dw);
                    if (mp == null) {
                        mp = new TLongObjectHashMap<>();
                        result.put(dw, mp);
                    }
                    mp.put(obj.getId(), obj);
                }
            }
        }
    }
    return result;
}
Also used : WorldRegion(net.osmand.map.WorldRegion) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) RouteDataObject(net.osmand.binary.RouteDataObject)

Example 10 with BinaryMapDataObject

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

the class ObfRegionSplitter method splitRegionPoiData.

private Map<String, TLongObjectHashMap<Map<String, Amenity>>> splitRegionPoiData(ObfFileInMemory fl, OsmandRegions osmandRegions) throws IOException {
    Map<String, TLongObjectHashMap<Map<String, Amenity>>> result = new HashMap<>();
    TLongObjectHashMap<Map<String, Amenity>> poiData = fl.getPoiObjects();
    for (Map<String, Amenity> objMap : poiData.valueCollection()) {
        Amenity obj = objMap.values().iterator().next();
        int x = MapUtils.get31TileNumberX(obj.getLocation().getLongitude());
        int y = MapUtils.get31TileNumberY(obj.getLocation().getLatitude());
        List<BinaryMapDataObject> l = osmandRegions.query(x, y);
        for (BinaryMapDataObject b : l) {
            if (osmandRegions.contain(b, x, y)) {
                String dw = osmandRegions.getDownloadName(b);
                WorldRegion wr = osmandRegions.getRegionDataByDownloadName(dw);
                if (dw == null || wr == null) {
                    continue;
                }
                if (!Algorithms.isEmpty(dw) && wr.isRegionMapDownload()) {
                    TLongObjectHashMap<Map<String, Amenity>> mp = result.get(dw);
                    if (mp == null) {
                        mp = new TLongObjectHashMap<>();
                        result.put(dw, mp);
                    }
                    mp.put(obj.getId(), objMap);
                }
            }
        }
    }
    return result;
}
Also used : Amenity(net.osmand.data.Amenity) WorldRegion(net.osmand.map.WorldRegion) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) Map(java.util.Map)

Aggregations

BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)41 IOException (java.io.IOException)12 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 WorldRegion (net.osmand.map.WorldRegion)10 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)9 File (java.io.File)9 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)8 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)8 Paint (android.graphics.Paint)7 TIntArrayList (gnu.trove.list.array.TIntArrayList)7 LinkedList (java.util.LinkedList)7 OsmandRegions (net.osmand.map.OsmandRegions)7 HashMap (java.util.HashMap)6 TreeSet (java.util.TreeSet)6 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)6 QuadRect (net.osmand.data.QuadRect)6 TextPaint (android.text.TextPaint)5 RandomAccessFile (java.io.RandomAccessFile)5 Map (java.util.Map)5