Search in sources :

Example 11 with BinaryMapIndexReader

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

the class BinaryMerger method mergeCitiesByNameDistance.

private void mergeCitiesByNameDistance(List<City> orderedCities, Map<City, List<City>> mergeGroup, Map<City, BinaryMapIndexReader> cityMap, boolean rename) {
    for (int i = 0; i < orderedCities.size() - 1; i++) {
        int j = i;
        City oc = orderedCities.get(i);
        City nc = orderedCities.get(j);
        BinaryMapIndexReader ocIndexReader = cityMap.get(oc);
        List<City> uniqueNamesakes = new ArrayList<City>();
        boolean renameGroup = false;
        while (MapObject.BY_NAME_COMPARATOR.areEqual(nc, oc)) {
            boolean isUniqueCity = true;
            for (ListIterator<City> uci = uniqueNamesakes.listIterator(); uci.hasNext(); ) {
                City uc = uci.next();
                if (isSameCity(uc, nc)) {
                    // Prefer cities with shortest names ("1101DL" instead of "1101 DL")
                    boolean shorter = nc.getName().length() < uc.getName().length();
                    if (shorter) {
                        mergeGroup.put(nc, mergeGroup.remove(uc));
                        uniqueNamesakes.remove(uc);
                        uniqueNamesakes.add(nc);
                        City tmp = uc;
                        uc = nc;
                        nc = tmp;
                    }
                    orderedCities.remove(nc);
                    mergeGroup.get(uc).add(nc);
                    isUniqueCity = false;
                    break;
                }
            }
            if (isUniqueCity) {
                uniqueNamesakes.add(nc);
                mergeGroup.put(nc, new ArrayList<City>());
                j++;
            }
            boolean areCitiesInSameRegion = ocIndexReader == cityMap.get(nc);
            renameGroup = renameGroup || (rename && !areCitiesInSameRegion && uniqueNamesakes.size() > 1);
            nc = (j < orderedCities.size()) ? orderedCities.get(j) : null;
        }
        if (uniqueNamesakes.size() == 1 && mergeGroup.get(uniqueNamesakes.get(0)).size() == 0) {
            mergeGroup.remove(uniqueNamesakes.get(0));
        } else {
            if (renameGroup) {
                for (City uc : uniqueNamesakes) {
                    for (City c : mergeGroup.get(uc)) {
                        addRegionToCityName(c, cityMap.get(c));
                    }
                    addRegionToCityName(uc, cityMap.get(uc));
                }
            }
        }
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) City(net.osmand.data.City)

Example 12 with BinaryMapIndexReader

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

the class QuickSearchDialogFragment method completeQueryWithObject.

public void completeQueryWithObject(SearchResult sr) {
    if (sr.object instanceof PoiType && ((PoiType) sr.object).isAdditional()) {
        PoiType additional = (PoiType) sr.object;
        AbstractPoiType parent = additional.getParentType();
        if (parent != null) {
            PoiUIFilter custom = app.getPoiFilters().getFilterById(PoiUIFilter.STD_PREFIX + parent.getKeyName());
            if (custom != null) {
                custom.clearFilter();
                custom.updateTypesToAccept(parent);
                custom.setFilterByName(additional.getKeyName().replace('_', ':').toLowerCase());
                SearchPhrase phrase = searchUICore.getPhrase();
                sr = new SearchResult(phrase);
                sr.localeName = custom.getName();
                sr.object = custom;
                sr.priority = SEARCH_AMENITY_TYPE_PRIORITY;
                sr.priorityDistance = 0;
                sr.objectType = ObjectType.POI_TYPE;
            }
        }
    }
    searchUICore.selectSearchResult(sr);
    if (addressSearch) {
        startAddressSearch();
        if (sr.objectType == ObjectType.CITY) {
            if (sr.relatedObject != null && sr.relatedObject instanceof BinaryMapIndexReader) {
                File f = ((BinaryMapIndexReader) sr.relatedObject).getFile();
                if (f != null) {
                    RegionAddressRepository region = app.getResourceManager().getRegionRepository(f.getName());
                    if (region != null) {
                        app.getSettings().setLastSearchedRegion(region.getFileName(), region.getEstimatedRegionCenter());
                        City city = (City) sr.object;
                        app.getSettings().setLastSearchedCity(city.getId(), sr.localeName, city.getLocation());
                    }
                }
            }
        }
    }
    String txt = searchUICore.getPhrase().getText(true);
    replaceQueryWithText(txt);
    if (sr.objectType == ObjectType.CITY) {
        openKeyboard();
    }
}
Also used : RegionAddressRepository(net.osmand.plus.resources.RegionAddressRepository) AbstractPoiType(net.osmand.osm.AbstractPoiType) PoiType(net.osmand.osm.PoiType) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) SearchResult(net.osmand.search.core.SearchResult) City(net.osmand.data.City) SpannableString(android.text.SpannableString) AbstractPoiType(net.osmand.osm.AbstractPoiType) File(java.io.File) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) SearchPhrase(net.osmand.search.core.SearchPhrase) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter)

Example 13 with BinaryMapIndexReader

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

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

the class MapRenderRepositories method checkInitialized.

public void checkInitialized(final int zoom, NativeOsmandLibrary library, int leftX, int rightX, int bottomY, int topY) {
    if (library == null) {
        return;
    }
    boolean useLive = context.getSettings().USE_OSM_LIVE_FOR_ROUTING.get();
    for (String mapName : files.keySet()) {
        BinaryMapIndexReader fr = files.get(mapName);
        if (fr != null && (fr.containsMapData(leftX, topY, rightX, bottomY, zoom) || fr.containsRouteData(leftX, topY, rightX, bottomY, zoom))) {
            if (!nativeFiles.contains(mapName)) {
                long time = System.currentTimeMillis();
                nativeFiles.add(mapName);
                if (!library.initMapFile(fr.getFile().getAbsolutePath(), useLive)) {
                    continue;
                }
                // $NON-NLS-1$ //$NON-NLS-2$
                log.debug("Native resource " + mapName + " initialized " + (System.currentTimeMillis() - time) + " ms");
            }
        }
    }
}
Also used : BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader)

Example 15 with BinaryMapIndexReader

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

BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)48 File (java.io.File)26 RandomAccessFile (java.io.RandomAccessFile)25 ArrayList (java.util.ArrayList)24 IOException (java.io.IOException)16 List (java.util.List)7 RoutePlannerFrontEnd (net.osmand.router.RoutePlannerFrontEnd)7 RoutingConfiguration (net.osmand.router.RoutingConfiguration)7 LinkedHashMap (java.util.LinkedHashMap)6 LatLon (net.osmand.data.LatLon)6 HashMap (java.util.HashMap)5 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)5 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)5 RouteRegion (net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion)5 City (net.osmand.data.City)5 RoutingContext (net.osmand.router.RoutingContext)5 Point (java.awt.Point)4 FileOutputStream (java.io.FileOutputStream)4 GeocodingResult (net.osmand.binary.GeocodingUtilities.GeocodingResult)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3