Search in sources :

Example 6 with TLongHashSet

use of gnu.trove.set.hash.TLongHashSet in project OsmAnd-tools by osmandapp.

the class BinaryMerger method combinePoiIndex.

private void combinePoiIndex(String name, BinaryMapIndexWriter writer, long dateCreated, PoiRegion[] poiRegions, BinaryMapIndexReader[] indexes) throws IOException, SQLException {
    final int[] writtenPoiCount = { 0 };
    MapRenderingTypesEncoder renderingTypes = new MapRenderingTypesEncoder(null, name);
    boolean overwriteIds = false;
    final IndexPoiCreator indexPoiCreator = new IndexPoiCreator(renderingTypes, overwriteIds);
    indexPoiCreator.createDatabaseStructure(new File(new File(System.getProperty("user.dir")), IndexCreator.getPoiFileName(name)));
    final Map<Long, List<Amenity>> amenityRelations = new HashMap<Long, List<Amenity>>();
    final TLongHashSet set = new TLongHashSet();
    final long[] generatedRelationId = { -1 };
    for (int i = 0; i < poiRegions.length; i++) {
        BinaryMapIndexReader index = indexes[i];
        final TLongHashSet file = new TLongHashSet();
        log.info("Region: " + extractRegionName(index));
        index.searchPoi(BinaryMapIndexReader.buildSearchPoiRequest(0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, -1, BinaryMapIndexReader.ACCEPT_ALL_POI_TYPE_FILTER, new ResultMatcher<Amenity>() {

            @Override
            public boolean publish(Amenity amenity) {
                try {
                    boolean isRelation = amenity.getId() < 0;
                    if (isRelation) {
                        long j = latlon(amenity);
                        List<Amenity> list;
                        if (!amenityRelations.containsKey(j)) {
                            list = new ArrayList<Amenity>(1);
                            amenityRelations.put(j, list);
                        } else {
                            list = amenityRelations.get(j);
                        }
                        boolean unique = true;
                        for (Amenity a : list) {
                            if (a.getType() == amenity.getType() && Algorithms.objectEquals(a.getSubType(), amenity.getSubType())) {
                                unique = false;
                                break;
                            }
                        }
                        if (unique) {
                            amenity.setId(generatedRelationId[0]--);
                            amenityRelations.get(j).add(amenity);
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    } else {
                        if (!set.contains(amenity.getId())) {
                            file.add(amenity.getId());
                            indexPoiCreator.insertAmenityIntoPoi(amenity);
                            writtenPoiCount[0]++;
                        }
                    }
                    return false;
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public boolean isCancelled() {
                return false;
            }
        }));
        set.addAll(file);
    }
    indexPoiCreator.writeBinaryPoiIndex(writer, name, null);
    indexPoiCreator.commitAndClosePoiFile(dateCreated);
    // REMOVE_POI_DB = false;
    if (REMOVE_POI_DB) {
        indexPoiCreator.removePoiFile();
    }
    log.info("Written " + writtenPoiCount[0] + " POI.");
}
Also used : Amenity(net.osmand.data.Amenity) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) IndexPoiCreator(net.osmand.data.preparation.IndexPoiCreator) MapRenderingTypesEncoder(net.osmand.osm.MapRenderingTypesEncoder) TLongHashSet(gnu.trove.set.hash.TLongHashSet) List(java.util.List) ArrayList(java.util.ArrayList) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Example 7 with TLongHashSet

use of gnu.trove.set.hash.TLongHashSet 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)

Example 8 with TLongHashSet

use of gnu.trove.set.hash.TLongHashSet in project Osmand by osmandapp.

the class GeocodingUtilities method reverseGeocodingSearch.

public List<GeocodingResult> reverseGeocodingSearch(RoutingContext ctx, double lat, double lon, boolean allowEmptyNames) throws IOException {
    RoutePlannerFrontEnd rp = new RoutePlannerFrontEnd(false);
    List<GeocodingResult> lst = new ArrayList<GeocodingUtilities.GeocodingResult>();
    List<RouteSegmentPoint> listR = new ArrayList<BinaryRoutePlanner.RouteSegmentPoint>();
    rp.findRouteSegment(lat, lon, ctx, listR);
    double distSquare = 0;
    TLongHashSet set = new TLongHashSet();
    Set<String> streetNames = new HashSet<String>();
    for (RouteSegmentPoint p : listR) {
        RouteDataObject road = p.getRoad();
        if (!set.add(road.getId())) {
            continue;
        }
        // System.out.println(road.toString() +  " " + Math.sqrt(p.distSquare));
        String name = Algorithms.isEmpty(road.getName()) ? road.getRef("", false, true) : road.getName();
        if (allowEmptyNames || !Algorithms.isEmpty(name)) {
            if (distSquare == 0 || distSquare > p.distSquare) {
                distSquare = p.distSquare;
            }
            GeocodingResult sr = new GeocodingResult();
            sr.searchPoint = new LatLon(lat, lon);
            sr.streetName = name == null ? "" : name;
            sr.point = p;
            sr.connectionPoint = new LatLon(MapUtils.get31LatitudeY(p.preciseY), MapUtils.get31LongitudeX(p.preciseX));
            sr.regionFP = road.region.getFilePointer();
            sr.regionLen = road.region.getLength();
            if (streetNames.add(sr.streetName)) {
                lst.add(sr);
            }
        }
        if (p.distSquare > STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS * STOP_SEARCHING_STREET_WITH_MULTIPLIER_RADIUS && distSquare != 0 && p.distSquare > THRESHOLD_MULTIPLIER_SKIP_STREETS_AFTER * distSquare) {
            break;
        }
        if (p.distSquare > STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS * STOP_SEARCHING_STREET_WITHOUT_MULTIPLIER_RADIUS) {
            break;
        }
    }
    Collections.sort(lst, GeocodingUtilities.DISTANCE_COMPARATOR);
    return lst;
}
Also used : BinaryRoutePlanner(net.osmand.router.BinaryRoutePlanner) ArrayList(java.util.ArrayList) LatLon(net.osmand.data.LatLon) RouteSegmentPoint(net.osmand.router.BinaryRoutePlanner.RouteSegmentPoint) TLongHashSet(gnu.trove.set.hash.TLongHashSet) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) HashSet(java.util.HashSet) TLongHashSet(gnu.trove.set.hash.TLongHashSet)

Example 9 with TLongHashSet

use of gnu.trove.set.hash.TLongHashSet in project Osmand by osmandapp.

the class BinaryMapPoiReaderAdapter method searchPoiIndex.

protected void searchPoiIndex(int left31, int right31, int top31, int bottom31, SearchRequest<Amenity> req, PoiRegion region) throws IOException {
    int indexOffset = codedIS.getTotalBytesRead();
    long time = System.currentTimeMillis();
    TLongHashSet skipTiles = null;
    if (req.zoom >= 0 && req.zoom < 16) {
        skipTiles = new TLongHashSet();
    }
    int length;
    int oldLimit;
    TIntLongHashMap offsetsMap = new TIntLongHashMap();
    while (true) {
        if (req.isCancelled()) {
            return;
        }
        int t = codedIS.readTag();
        int tag = WireFormat.getTagFieldNumber(t);
        switch(tag) {
            case 0:
                return;
            case OsmandOdb.OsmAndPoiIndex.BOXES_FIELD_NUMBER:
                length = readInt();
                oldLimit = codedIS.pushLimit(length);
                readBoxField(left31, right31, top31, bottom31, 0, 0, 0, offsetsMap, skipTiles, req, region);
                codedIS.popLimit(oldLimit);
                break;
            case OsmandOdb.OsmAndPoiIndex.POIDATA_FIELD_NUMBER:
                int[] offsets = offsetsMap.keys();
                // also offsets can be randomly skipped by limit
                Arrays.sort(offsets);
                if (skipTiles != null) {
                    skipTiles.clear();
                }
                LOG.info("Searched poi structure in " + (System.currentTimeMillis() - time) + " ms. Found " + offsets.length + " subtrees");
                for (int j = 0; j < offsets.length; j++) {
                    long skipVal = offsetsMap.get(offsets[j]);
                    if (skipTiles != null && skipVal != -1) {
                        int dzoom = ZOOM_TO_SKIP_FILTER_READ - ZOOM_TO_SKIP_FILTER;
                        long dx = (skipVal >> ZOOM_TO_SKIP_FILTER_READ);
                        long dy = skipVal - (dx << ZOOM_TO_SKIP_FILTER_READ);
                        skipVal = ((dx >> dzoom) << ZOOM_TO_SKIP_FILTER) | (dy >> dzoom);
                        if (skipVal != -1 && skipTiles.contains(skipVal)) {
                            continue;
                        }
                    }
                    codedIS.seek(offsets[j] + indexOffset);
                    int len = readInt();
                    int oldLim = codedIS.pushLimit(len);
                    boolean read = readPoiData(left31, right31, top31, bottom31, req, region, skipTiles, req.zoom == -1 ? 31 : req.zoom + ZOOM_TO_SKIP_FILTER);
                    if (read && skipVal != -1 && skipTiles != null) {
                        skipTiles.add(skipVal);
                    }
                    codedIS.popLimit(oldLim);
                    if (req.isCancelled()) {
                        return;
                    }
                }
                codedIS.skipRawBytes(codedIS.getBytesUntilLimit());
                return;
            default:
                skipUnknownField(t);
                break;
        }
    }
}
Also used : TIntLongHashMap(gnu.trove.map.hash.TIntLongHashMap) TLongHashSet(gnu.trove.set.hash.TLongHashSet) AmenityRoutePoint(net.osmand.data.Amenity.AmenityRoutePoint)

Example 10 with TLongHashSet

use of gnu.trove.set.hash.TLongHashSet in project Osmand by osmandapp.

the class RoutingContext method loadSubregionTile.

public void loadSubregionTile(final RoutingSubregionTile ts, boolean loadObjectsInMemory, List<RouteDataObject> toLoad, TLongHashSet excludeNotAllowed) {
    boolean wasUnloaded = ts.isUnloaded();
    int ucount = ts.getUnloadCont();
    if (nativeLib == null) {
        long now = System.nanoTime();
        try {
            BinaryMapIndexReader reader = reverseMap.get(ts.subregion.routeReg);
            ts.setLoadedNonNative();
            List<RouteDataObject> res = reader.loadRouteIndexData(ts.subregion);
            // System.out.println(ts.subregion.shiftToData + " " + res);
            if (toLoad != null) {
                toLoad.addAll(res);
            } else {
                for (RouteDataObject ro : res) {
                    if (ro != null) {
                        if (config.router.acceptLine(ro)) {
                            if (excludeNotAllowed != null && !excludeNotAllowed.contains(ro.getId())) {
                                ts.add(ro);
                            }
                        } else if (excludeNotAllowed != null && ro.getId() > 0) {
                            excludeNotAllowed.add(ro.getId());
                            if (ts.excludedIds == null) {
                                ts.excludedIds = new TLongHashSet();
                            }
                            ts.excludedIds.add(ro.getId());
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Loading data exception", e);
        }
        timeToLoad += (System.nanoTime() - now);
    } else {
        long now = System.nanoTime();
        NativeRouteSearchResult ns = nativeLib.loadRouteRegion(ts.subregion, loadObjectsInMemory);
        // System.out.println(ts.subregion.shiftToData + " " + Arrays.toString(ns.objects));
        ts.setLoadedNative(ns, this);
        timeToLoad += (System.nanoTime() - now);
    }
    loadedTiles++;
    if (wasUnloaded) {
        if (ucount == 1) {
            loadedPrevUnloadedTiles++;
        }
    } else {
        if (global != null) {
            global.allRoutes += ts.tileStatistics.allRoutes;
            global.coordinates += ts.tileStatistics.coordinates;
        }
        distinctLoadedTiles++;
    }
    global.size += ts.tileStatistics.size;
}
Also used : TLongHashSet(gnu.trove.set.hash.TLongHashSet) NativeRouteSearchResult(net.osmand.NativeLibrary.NativeRouteSearchResult) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) RouteDataObject(net.osmand.binary.RouteDataObject) IOException(java.io.IOException)

Aggregations

TLongHashSet (gnu.trove.set.hash.TLongHashSet)22 ArrayList (java.util.ArrayList)11 RouteDataObject (net.osmand.binary.RouteDataObject)6 TLongSet (gnu.trove.set.TLongSet)5 List (java.util.List)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 TLongObjectHashMap (gnu.trove.map.hash.TLongObjectHashMap)4 TIntArrayList (gnu.trove.list.array.TIntArrayList)3 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 PriorityQueue (java.util.PriorityQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)2 Node (net.osmand.osm.edit.Node)2 BinaryRoutePlanner (net.osmand.router.BinaryRoutePlanner)2 RouteSegment (net.osmand.router.BinaryRoutePlanner.RouteSegment)2 Async (br.com.brjdevs.java.utils.async.Async)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DiscordBotsAPI (com.github.natanbc.discordbotsapi.DiscordBotsAPI)1