Search in sources :

Example 1 with NativeOsmandLibrary

use of net.osmand.plus.render.NativeOsmandLibrary in project Osmand by osmandapp.

the class RouteProvider method findVectorMapsRoute.

protected RouteCalculationResult findVectorMapsRoute(final RouteCalculationParams params, boolean calcGPXRoute) throws IOException {
    BinaryMapIndexReader[] files = params.ctx.getResourceManager().getRoutingMapFiles();
    RoutePlannerFrontEnd router = new RoutePlannerFrontEnd(false);
    OsmandSettings settings = params.ctx.getSettings();
    router.setUseFastRecalculation(settings.USE_FAST_RECALCULATION.get());
    RoutingConfiguration.Builder config = params.ctx.getDefaultRoutingConfig();
    GeneralRouter generalRouter = SettingsNavigationActivity.getRouter(config, params.mode);
    if (generalRouter == null) {
        return applicationModeNotSupported(params);
    }
    RoutingConfiguration cf = initOsmAndRoutingConfig(config, params, settings, generalRouter);
    if (cf == null) {
        return applicationModeNotSupported(params);
    }
    PrecalculatedRouteDirection precalculated = null;
    if (calcGPXRoute) {
        ArrayList<Location> sublist = findStartAndEndLocationsFromRoute(params.gpxRoute.points, params.start, params.end, null, null);
        LatLon[] latLon = new LatLon[sublist.size()];
        for (int k = 0; k < latLon.length; k++) {
            latLon[k] = new LatLon(sublist.get(k).getLatitude(), sublist.get(k).getLongitude());
        }
        precalculated = PrecalculatedRouteDirection.build(latLon, generalRouter.getMaxDefaultSpeed());
        precalculated.setFollowNext(true);
    // cf.planRoadDirection = 1;
    }
    // BUILD context
    NativeOsmandLibrary lib = settings.SAFE_MODE.get() ? null : NativeOsmandLibrary.getLoadedLibrary();
    // check loaded files
    int leftX = MapUtils.get31TileNumberX(params.start.getLongitude());
    int rightX = leftX;
    int bottomY = MapUtils.get31TileNumberY(params.start.getLatitude());
    int topY = bottomY;
    if (params.intermediates != null) {
        for (LatLon l : params.intermediates) {
            leftX = Math.min(MapUtils.get31TileNumberX(l.getLongitude()), leftX);
            rightX = Math.max(MapUtils.get31TileNumberX(l.getLongitude()), rightX);
            bottomY = Math.max(MapUtils.get31TileNumberY(l.getLatitude()), bottomY);
            topY = Math.min(MapUtils.get31TileNumberY(l.getLatitude()), topY);
        }
    }
    LatLon l = params.end;
    leftX = Math.min(MapUtils.get31TileNumberX(l.getLongitude()), leftX);
    rightX = Math.max(MapUtils.get31TileNumberX(l.getLongitude()), rightX);
    bottomY = Math.max(MapUtils.get31TileNumberY(l.getLatitude()), bottomY);
    topY = Math.min(MapUtils.get31TileNumberY(l.getLatitude()), topY);
    params.ctx.getResourceManager().getRenderer().checkInitialized(15, lib, leftX, rightX, bottomY, topY);
    RoutingContext ctx = router.buildRoutingContext(cf, lib, files, RouteCalculationMode.NORMAL);
    RoutingContext complexCtx = null;
    boolean complex = params.mode.isDerivedRoutingFrom(ApplicationMode.CAR) && !settings.DISABLE_COMPLEX_ROUTING.get() && precalculated == null;
    ctx.leftSideNavigation = params.leftSide;
    ctx.calculationProgress = params.calculationProgress;
    if (params.previousToRecalculate != null && params.onlyStartPointChanged) {
        int currentRoute = params.previousToRecalculate.getCurrentRoute();
        List<RouteSegmentResult> originalRoute = params.previousToRecalculate.getOriginalRoute();
        if (originalRoute != null && currentRoute < originalRoute.size()) {
            ctx.previouslyCalculatedRoute = originalRoute.subList(currentRoute, originalRoute.size());
        }
    }
    if (complex && router.getRecalculationEnd(ctx) != null) {
        complex = false;
    }
    if (complex) {
        complexCtx = router.buildRoutingContext(cf, lib, files, RouteCalculationMode.COMPLEX);
        complexCtx.calculationProgress = params.calculationProgress;
        complexCtx.leftSideNavigation = params.leftSide;
        complexCtx.previouslyCalculatedRoute = ctx.previouslyCalculatedRoute;
    }
    LatLon st = new LatLon(params.start.getLatitude(), params.start.getLongitude());
    LatLon en = new LatLon(params.end.getLatitude(), params.end.getLongitude());
    List<LatLon> inters = new ArrayList<LatLon>();
    if (params.intermediates != null) {
        inters = new ArrayList<LatLon>(params.intermediates);
    }
    return calcOfflineRouteImpl(params, router, ctx, complexCtx, st, en, inters, precalculated);
}
Also used : PrecalculatedRouteDirection(net.osmand.router.PrecalculatedRouteDirection) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) ArrayList(java.util.ArrayList) GeneralRouter(net.osmand.router.GeneralRouter) OsmandSettings(net.osmand.plus.OsmandSettings) TargetPoint(net.osmand.plus.TargetPointsHelper.TargetPoint) LocationPoint(net.osmand.data.LocationPoint) LatLon(net.osmand.data.LatLon) RoutingContext(net.osmand.router.RoutingContext) Builder(net.osmand.router.RoutingConfiguration.Builder) RoutingConfiguration(net.osmand.router.RoutingConfiguration) RoutePlannerFrontEnd(net.osmand.router.RoutePlannerFrontEnd) NativeOsmandLibrary(net.osmand.plus.render.NativeOsmandLibrary) RouteSegmentResult(net.osmand.router.RouteSegmentResult) Location(net.osmand.Location)

Example 2 with NativeOsmandLibrary

use of net.osmand.plus.render.NativeOsmandLibrary in project Osmand by osmandapp.

the class ContextMenuLayer method showContextMenu.

private boolean showContextMenu(PointF point, RotatedTileBox tileBox, boolean showUnknownLocation) {
    LatLon objectLatLon = null;
    Map<Object, IContextMenuProvider> selectedObjects = selectObjectsForContextMenu(tileBox, point, false, showUnknownLocation);
    NativeOsmandLibrary nativeLib = NativeOsmandLibrary.getLoadedLibrary();
    if (nativeLib != null) {
        MapRenderRepositories maps = activity.getMyApplication().getResourceManager().getRenderer();
        RenderingContext rc = maps.getVisibleRenderingContext();
        RenderedObject[] renderedObjects = null;
        if (rc != null && rc.zoom == tileBox.getZoom()) {
            double lat = MapUtils.get31LatitudeY((int) (rc.topY * rc.tileDivisor));
            double lon = MapUtils.get31LongitudeX((int) (rc.leftX * rc.tileDivisor));
            float x = tileBox.getPixXFromLatLon(lat, lon);
            float y = tileBox.getPixYFromLatLon(lat, lon);
            renderedObjects = nativeLib.searchRenderedObjectsFromContext(rc, (int) (point.x - x), (int) (point.y - y));
        }
        if (renderedObjects != null) {
            int TILE_SIZE = 256;
            double cosRotateTileSize = Math.cos(Math.toRadians(rc.rotate)) * TILE_SIZE;
            double sinRotateTileSize = Math.sin(Math.toRadians(rc.rotate)) * TILE_SIZE;
            for (RenderedObject r : renderedObjects) {
                double cx = r.getBbox().centerX();
                double cy = r.getBbox().centerY();
                double dTileX = (cx * cosRotateTileSize + cy * sinRotateTileSize) / (TILE_SIZE * TILE_SIZE);
                double dTileY = (cy * cosRotateTileSize - cx * sinRotateTileSize) / (TILE_SIZE * TILE_SIZE);
                int x31 = (int) ((dTileX + rc.leftX) * rc.tileDivisor);
                int y31 = (int) ((dTileY + rc.topY) * rc.tileDivisor);
                double lat = MapUtils.get31LatitudeY(y31);
                double lon = MapUtils.get31LongitudeX(x31);
                r.setLabelLatLon(new LatLon(lat, lon));
            }
            IContextMenuProvider poiMenuProvider = activity.getMapLayers().getPoiMapLayer();
            for (RenderedObject renderedObject : renderedObjects) {
                if (renderedObject.getX() != null && renderedObject.getX().size() == 1 && renderedObject.getY() != null && renderedObject.getY().size() == 1) {
                    objectLatLon = new LatLon(MapUtils.get31LatitudeY(renderedObject.getY().get(0)), MapUtils.get31LongitudeX(renderedObject.getX().get(0)));
                } else if (renderedObject.getLabelLatLon() != null) {
                    objectLatLon = renderedObject.getLabelLatLon();
                }
                if (renderedObject.getId() != null) {
                    List<String> names = new ArrayList<>();
                    if (!Algorithms.isEmpty(renderedObject.getName())) {
                        names.add(renderedObject.getName());
                    }
                    for (Entry<String, String> entry : renderedObject.getTags().entrySet()) {
                        if (entry.getKey().startsWith("name:") && !entry.getValue().equals("")) {
                            names.add(entry.getValue());
                        }
                        if (entry.getKey().equals("name") && !entry.getValue().equals("")) {
                            names.add(entry.getValue());
                        }
                    }
                    LatLon searchLatLon = objectLatLon;
                    if (searchLatLon == null) {
                        searchLatLon = tileBox.getLatLonFromPixel(point.x, point.y);
                    }
                    Amenity amenity = findAmenity(activity.getMyApplication(), renderedObject.getId() >> 7, names, searchLatLon, 50);
                    if (amenity != null) {
                        if (renderedObject.getX() != null && renderedObject.getX().size() > 1 && renderedObject.getY() != null && renderedObject.getY().size() > 1) {
                            amenity.getX().addAll(renderedObject.getX());
                            amenity.getY().addAll(renderedObject.getY());
                        }
                        boolean exists = false;
                        for (Object o : selectedObjects.keySet()) {
                            if (o instanceof Amenity && ((Amenity) o).compareTo(amenity) == 0) {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists) {
                            selectedObjects.put(amenity, poiMenuProvider);
                        }
                        continue;
                    }
                    selectedObjects.put(renderedObject, null);
                }
            }
        }
    }
    for (Map.Entry<Object, IContextMenuProvider> entry : selectedObjects.entrySet()) {
        IContextMenuProvider provider = entry.getValue();
        if (provider != null && provider.runExclusiveAction(entry.getKey(), showUnknownLocation)) {
            return true;
        }
    }
    if (selectedObjects.size() == 1) {
        Object selectedObj = selectedObjects.keySet().iterator().next();
        LatLon latLon = objectLatLon;
        PointDescription pointDescription = null;
        final IContextMenuProvider provider = selectedObjects.get(selectedObj);
        if (provider != null) {
            if (latLon == null) {
                latLon = provider.getObjectLocation(selectedObj);
            }
            pointDescription = provider.getObjectName(selectedObj);
        }
        if (latLon == null) {
            latLon = getLatLon(point, tileBox);
        }
        if (mInAddGpxPointMode) {
            String title = pointDescription == null ? "" : pointDescription.getName();
            mAddGpxPointBottomSheetHelper.setTitle(title);
            view.getAnimatedDraggingThread().startMoving(latLon.getLatitude(), latLon.getLongitude(), view.getZoom(), true);
        } else {
            showContextMenu(latLon, pointDescription, selectedObj, provider);
        }
        return true;
    } else if (selectedObjects.size() > 1) {
        selectedObjectContextMenuProvider = null;
        showContextMenuForSelectedObjects(getLatLon(point, tileBox), selectedObjects);
        return true;
    } else if (showUnknownLocation) {
        hideVisibleMenues();
        selectedObjectContextMenuProvider = null;
        LatLon latLon = getLatLon(point, tileBox);
        activity.getMapViewTrackingUtilities().setMapLinkedToLocation(false);
        if (mInAddGpxPointMode) {
            mAddGpxPointBottomSheetHelper.setTitle("");
            view.getAnimatedDraggingThread().startMoving(latLon.getLatitude(), latLon.getLongitude(), view.getZoom(), true);
        } else {
            menu.show(latLon, null, null);
        }
        return true;
    }
    return false;
}
Also used : RenderingContext(net.osmand.RenderingContext) Amenity(net.osmand.data.Amenity) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) NewGpxPoint(net.osmand.plus.views.AddGpxPointBottomSheetHelper.NewGpxPoint) Paint(android.graphics.Paint) LatLon(net.osmand.data.LatLon) RenderedObject(net.osmand.NativeLibrary.RenderedObject) PointDescription(net.osmand.data.PointDescription) NativeOsmandLibrary(net.osmand.plus.render.NativeOsmandLibrary) MapRenderRepositories(net.osmand.plus.render.MapRenderRepositories) CallbackWithObject(net.osmand.CallbackWithObject) RenderedObject(net.osmand.NativeLibrary.RenderedObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with NativeOsmandLibrary

use of net.osmand.plus.render.NativeOsmandLibrary in project Osmand by osmandapp.

the class AppInitializer method initNativeCore.

private void initNativeCore() {
    if (!"qnx".equals(System.getProperty("os.name"))) {
        OsmandSettings osmandSettings = app.getSettings();
        if (osmandSettings.USE_OPENGL_RENDER.get()) {
            boolean success = false;
            if (!osmandSettings.OPENGL_RENDER_FAILED.get()) {
                osmandSettings.OPENGL_RENDER_FAILED.set(true);
                success = NativeCoreContext.tryCatchInit(app);
                if (success) {
                    osmandSettings.OPENGL_RENDER_FAILED.set(false);
                }
            }
            if (!success) {
                // try next time once again ?
                osmandSettings.OPENGL_RENDER_FAILED.set(false);
                warnings.add("Native OpenGL library is not supported. Please try again after exit");
            }
        }
        if (osmandSettings.NATIVE_RENDERING_FAILED.get()) {
            osmandSettings.SAFE_MODE.set(true);
            osmandSettings.NATIVE_RENDERING_FAILED.set(false);
            warnings.add(app.getString(R.string.native_library_not_supported));
        } else {
            osmandSettings.SAFE_MODE.set(false);
            osmandSettings.NATIVE_RENDERING_FAILED.set(true);
            startTask(app.getString(R.string.init_native_library), -1);
            RenderingRulesStorage storage = app.getRendererRegistry().getCurrentSelectedRenderer();
            NativeOsmandLibrary lib = NativeOsmandLibrary.getLibrary(storage, app);
            boolean initialized = lib != null;
            osmandSettings.NATIVE_RENDERING_FAILED.set(false);
            if (!initialized) {
                LOG.info("Native library could not be loaded!");
            } else {
                File ls = app.getAppPath("fonts");
                lib.loadFontData(ls);
            }
        }
        app.getResourceManager().initMapBoundariesCacheNative();
    }
}
Also used : NativeOsmandLibrary(net.osmand.plus.render.NativeOsmandLibrary) File(java.io.File) RenderingRulesStorage(net.osmand.render.RenderingRulesStorage)

Aggregations

NativeOsmandLibrary (net.osmand.plus.render.NativeOsmandLibrary)3 ArrayList (java.util.ArrayList)2 LatLon (net.osmand.data.LatLon)2 Paint (android.graphics.Paint)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CallbackWithObject (net.osmand.CallbackWithObject)1 Location (net.osmand.Location)1 RenderedObject (net.osmand.NativeLibrary.RenderedObject)1 RenderingContext (net.osmand.RenderingContext)1 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)1 Amenity (net.osmand.data.Amenity)1 LocationPoint (net.osmand.data.LocationPoint)1 PointDescription (net.osmand.data.PointDescription)1 OsmandSettings (net.osmand.plus.OsmandSettings)1 TargetPoint (net.osmand.plus.TargetPointsHelper.TargetPoint)1 MapRenderRepositories (net.osmand.plus.render.MapRenderRepositories)1 NewGpxPoint (net.osmand.plus.views.AddGpxPointBottomSheetHelper.NewGpxPoint)1