Search in sources :

Example 1 with MapIndex

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

the class IndexUploader method extractRoadOnlyFile.

public static void extractRoadOnlyFile(File mainFile, File roadOnlyFile) throws IOException, RTreeException {
    RandomAccessFile raf = new RandomAccessFile(mainFile, "r");
    BinaryMapIndexReader index = new BinaryMapIndexReader(raf, mainFile);
    final RandomAccessFile routf = new RandomAccessFile(roadOnlyFile, "rw");
    routf.setLength(0);
    CodedOutputStream ous = CodedOutputStream.newInstance(new OutputStream() {

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

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

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            routf.write(b, off, len);
        }
    });
    byte[] BUFFER_TO_READ = new byte[BUFFER_SIZE];
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSION_FIELD_NUMBER, index.getVersion());
    ous.writeInt64(OsmandOdb.OsmAndStructure.DATECREATED_FIELD_NUMBER, index.getDateCreated());
    for (int i = 0; i < index.getIndexes().size(); i++) {
        BinaryIndexPart part = index.getIndexes().get(i);
        if (part instanceof MapIndex) {
            // skip map part
            copyMapIndex(roadOnlyFile, (MapIndex) part, index, ous, routf);
            continue;
        } else if (part instanceof AddressRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ADDRESSINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof TransportIndex) {
            ous.writeTag(OsmandOdb.OsmAndStructure.TRANSPORTINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof PoiRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.POIINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else if (part instanceof RouteRegion) {
            ous.writeTag(OsmandOdb.OsmAndStructure.ROUTINGINDEX_FIELD_NUMBER, WireFormat.WIRETYPE_FIXED32_LENGTH_DELIMITED);
        } else {
            throw new UnsupportedOperationException();
        }
        BinaryMerger.writeInt(ous, part.getLength());
        BinaryMerger.copyBinaryPart(ous, BUFFER_TO_READ, raf, part.getFilePointer(), part.getLength());
    }
    ous.writeInt32(OsmandOdb.OsmAndStructure.VERSIONCONFIRM_FIELD_NUMBER, index.getVersion());
    ous.flush();
    routf.close();
    raf.close();
}
Also used : BinaryIndexPart(net.osmand.binary.BinaryIndexPart) CodedOutputStream(com.google.protobuf.CodedOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CodedOutputStream(com.google.protobuf.CodedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) RouteRegion(net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) AddressRegion(net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion) TransportIndex(net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex) PoiRegion(net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)

Example 2 with MapIndex

use of net.osmand.binary.BinaryMapIndexReader.MapIndex 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 3 with MapIndex

use of net.osmand.binary.BinaryMapIndexReader.MapIndex 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 4 with MapIndex

use of net.osmand.binary.BinaryMapIndexReader.MapIndex in project Osmand by osmandapp.

the class MapRenderRepositories method readMapObjectsForRendering.

private MapIndex readMapObjectsForRendering(final int zoom, final RenderingRuleSearchRequest renderingReq, ArrayList<BinaryMapDataObject> tempResult, ArrayList<BinaryMapDataObject> basemapResult, TLongSet ids, int[] count, boolean[] ocean, boolean[] land, List<BinaryMapDataObject> coastLines, List<BinaryMapDataObject> basemapCoastLines, int leftX, int rightX, int bottomY, int topY) {
    BinaryMapIndexReader.SearchFilter searchFilter = new BinaryMapIndexReader.SearchFilter() {

        @Override
        public boolean accept(TIntArrayList types, BinaryMapIndexReader.MapIndex root) {
            for (int j = 0; j < types.size(); j++) {
                int type = types.get(j);
                TagValuePair pair = root.decodeType(type);
                if (pair != null) {
                    // TODO is it fast enough ?
                    for (int i = 1; i <= 3; i++) {
                        renderingReq.setIntFilter(renderingReq.ALL.R_MINZOOM, zoom);
                        renderingReq.setStringFilter(renderingReq.ALL.R_TAG, pair.tag);
                        renderingReq.setStringFilter(renderingReq.ALL.R_VALUE, pair.value);
                        if (renderingReq.search(i, false)) {
                            return true;
                        }
                    }
                    renderingReq.setStringFilter(renderingReq.ALL.R_TAG, pair.tag);
                    renderingReq.setStringFilter(renderingReq.ALL.R_VALUE, pair.value);
                    if (renderingReq.search(RenderingRulesStorage.TEXT_RULES, false)) {
                        return true;
                    }
                }
            }
            return false;
        }
    };
    if (zoom > 16) {
        searchFilter = null;
    }
    MapIndex mi = null;
    searchRequest = BinaryMapIndexReader.buildSearchRequest(leftX, rightX, topY, bottomY, zoom, searchFilter);
    for (BinaryMapIndexReader c : files.values()) {
        boolean basemap = c.isBasemap();
        searchRequest.clearSearchResults();
        List<BinaryMapDataObject> res;
        try {
            res = c.searchMapIndex(searchRequest);
        } catch (IOException e) {
            res = new ArrayList<BinaryMapDataObject>();
            // $NON-NLS-1$
            log.debug("Search failed " + c.getRegionNames(), e);
        }
        if (res.size() > 0) {
            if (basemap) {
                renderedState |= 1;
            } else {
                renderedState |= 2;
            }
        }
        for (BinaryMapDataObject r : res) {
            if (checkForDuplicateObjectIds && !basemap) {
                if (ids.contains(r.getId()) && r.getId() > 0) {
                    // do not add object twice
                    continue;
                }
                ids.add(r.getId());
            }
            count[0]++;
            if (r.containsType(r.getMapIndex().coastlineEncodingType)) {
                if (basemap) {
                    basemapCoastLines.add(r);
                } else {
                    coastLines.add(r);
                }
            } else {
                // do not mess coastline and other types
                if (basemap) {
                    basemapResult.add(r);
                } else {
                    tempResult.add(r);
                }
            }
            if (checkWhetherInterrupted()) {
                return null;
            }
        }
        if (searchRequest.isOcean()) {
            mi = c.getMapIndexes().get(0);
            ocean[0] = true;
        }
        if (searchRequest.isLand()) {
            mi = c.getMapIndexes().get(0);
            land[0] = true;
        }
    }
    return mi;
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex) TagValuePair(net.osmand.binary.BinaryMapIndexReader.TagValuePair) IOException(java.io.IOException) TIntArrayList(gnu.trove.list.array.TIntArrayList)

Example 5 with MapIndex

use of net.osmand.binary.BinaryMapIndexReader.MapIndex in project Osmand by osmandapp.

the class MapRenderRepositories method loadVectorData.

private boolean loadVectorData(QuadRect dataBox, final int zoom, final RenderingRuleSearchRequest renderingReq) {
    double cBottomLatitude = dataBox.bottom;
    double cTopLatitude = dataBox.top;
    double cLeftLongitude = dataBox.left;
    double cRightLongitude = dataBox.right;
    long now = System.currentTimeMillis();
    // to clear previous objects
    System.gc();
    ArrayList<BinaryMapDataObject> tempResult = new ArrayList<BinaryMapDataObject>();
    ArrayList<BinaryMapDataObject> basemapResult = new ArrayList<BinaryMapDataObject>();
    int[] count = new int[] { 0 };
    boolean[] ocean = new boolean[] { false };
    boolean[] land = new boolean[] { false };
    List<BinaryMapDataObject> coastLines = new ArrayList<BinaryMapDataObject>();
    List<BinaryMapDataObject> basemapCoastLines = new ArrayList<BinaryMapDataObject>();
    int leftX = MapUtils.get31TileNumberX(cLeftLongitude);
    int rightX = MapUtils.get31TileNumberX(cRightLongitude);
    int bottomY = MapUtils.get31TileNumberY(cBottomLatitude);
    int topY = MapUtils.get31TileNumberY(cTopLatitude);
    TLongSet ids = new TLongHashSet();
    MapIndex mi = readMapObjectsForRendering(zoom, renderingReq, tempResult, basemapResult, ids, count, ocean, land, coastLines, basemapCoastLines, leftX, rightX, bottomY, topY);
    int renderRouteDataFile = 0;
    if (renderingReq.searchRenderingAttribute("showRoadMapsAttribute")) {
        renderRouteDataFile = renderingReq.getIntPropertyValue(renderingReq.ALL.R_ATTR_INT_VALUE);
    }
    if (checkWhetherInterrupted()) {
        return false;
    }
    boolean objectsFromMapSectionRead = tempResult.size() > 0;
    if (renderRouteDataFile >= 0 && zoom >= zoomOnlyForBasemaps) {
        searchRequest = BinaryMapIndexReader.buildSearchRequest(leftX, rightX, topY, bottomY, zoom, null);
        for (BinaryMapIndexReader c : files.values()) {
            // false positive case when we have 2 sep maps Country-roads & Country
            if (c.getMapIndexes().size() == 0 || renderRouteDataFile == 1) {
                readRouteDataAsMapObjects(searchRequest, c, tempResult, ids);
            }
        }
        log.info(String.format("Route objects %s", tempResult.size() + ""));
    }
    String coastlineTime = "";
    boolean addBasemapCoastlines = true;
    boolean emptyData = zoom > zoomOnlyForBasemaps && tempResult.isEmpty() && coastLines.isEmpty();
    boolean basemapMissing = zoom <= zoomOnlyForBasemaps && basemapCoastLines.isEmpty() && mi == null;
    boolean detailedLandData = zoom >= zoomForBaseRouteRendering && tempResult.size() > 0 && objectsFromMapSectionRead;
    if (!coastLines.isEmpty()) {
        long ms = System.currentTimeMillis();
        boolean coastlinesWereAdded = processCoastlines(coastLines, leftX, rightX, bottomY, topY, zoom, basemapCoastLines.isEmpty(), true, tempResult);
        addBasemapCoastlines = (!coastlinesWereAdded && !detailedLandData) || zoom <= zoomOnlyForBasemaps;
        coastlineTime = "(coastline " + (System.currentTimeMillis() - ms) + " ms )";
    } else {
        addBasemapCoastlines = !detailedLandData;
    }
    if (addBasemapCoastlines) {
        long ms = System.currentTimeMillis();
        boolean coastlinesWereAdded = processCoastlines(basemapCoastLines, leftX, rightX, bottomY, topY, zoom, true, true, tempResult);
        addBasemapCoastlines = !coastlinesWereAdded;
        coastlineTime = "(coastline " + (System.currentTimeMillis() - ms) + " ms )";
    }
    if (addBasemapCoastlines && mi != null) {
        int[] coordinates = new int[] { leftX, topY, rightX, topY, rightX, bottomY, leftX, bottomY, leftX, topY };
        BinaryMapDataObject o = new BinaryMapDataObject(-1, coordinates, new int[0][], RenderingRulesStorage.POLYGON_RULES, true, new int[] { ocean[0] && !land[0] ? mi.coastlineEncodingType : (mi.landEncodingType) }, null);
        o.setMapIndex(mi);
        tempResult.add(o);
    }
    if (emptyData || basemapMissing) {
        // message
        MapIndex mapIndex;
        if (!tempResult.isEmpty()) {
            mapIndex = tempResult.get(0).getMapIndex();
        } else {
            mapIndex = new MapIndex();
            mapIndex.initMapEncodingRule(0, 1, "natural", "coastline");
            mapIndex.initMapEncodingRule(0, 2, "name", "");
        }
    }
    if (zoom <= zoomOnlyForBasemaps || emptyData) {
        tempResult.addAll(basemapResult);
    }
    if (count[0] > 0) {
        log.info(// $NON-NLS-1$
        String.format(// $NON-NLS-1$
        "BLat=%s, TLat=%s, LLong=%s, RLong=%s, zoom=%s", cBottomLatitude, cTopLatitude, cLeftLongitude, cRightLongitude, zoom));
        // $NON-NLS-1$
        log.info(String.format("Searching: %s ms  %s (%s results found)", System.currentTimeMillis() - now, coastlineTime, count[0]));
    }
    cObjects = tempResult;
    cObjectsBox = dataBox;
    cObjectsZoom = zoom;
    return true;
}
Also used : TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TLongSet(gnu.trove.set.TLongSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

Aggregations

MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)16 TIntArrayList (gnu.trove.list.array.TIntArrayList)8 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)8 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 PoiRegion (net.osmand.binary.BinaryMapPoiReaderAdapter.PoiRegion)6 TransportIndex (net.osmand.binary.BinaryMapTransportReaderAdapter.TransportIndex)6 TLongArrayList (gnu.trove.list.array.TLongArrayList)5 AddressRegion (net.osmand.binary.BinaryMapAddressReaderAdapter.AddressRegion)5 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)5 MapRoot (net.osmand.binary.BinaryMapIndexReader.MapRoot)5 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 CitiesBlock (net.osmand.binary.BinaryMapAddressReaderAdapter.CitiesBlock)3 TagValuePair (net.osmand.binary.BinaryMapIndexReader.TagValuePair)3 RouteSubregion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteSubregion)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)2