Search in sources :

Example 1 with RenderingContext

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

the class NativeSwingRendering method renderImage.

public BufferedImage renderImage(RenderingImageContext ctx) throws IOException {
    long time = -System.currentTimeMillis();
    RenderingContext rctx = new RenderingContext() {

        @Override
        protected byte[] getIconRawData(String data) {
            return _R.getIconData(data);
        }
    };
    rctx.preferredLocale = renderingProps.get("lang") != null ? renderingProps.get("lang") : "";
    rctx.nightMode = "true".equals(renderingProps.get("nightMode"));
    RenderingRuleSearchRequest request = new RenderingRuleSearchRequest(storage);
    request.setBooleanFilter(request.ALL.R_NIGHT_MODE, rctx.nightMode);
    for (RenderingRuleProperty customProp : storage.PROPS.getCustomRules()) {
        String res = renderingProps.get(customProp.getAttrName());
        if (customProp.getAttrName().equals(RenderingRuleStorageProperties.A_ENGINE_V1)) {
            request.setBooleanFilter(customProp, true);
        } else if (!Algorithms.isEmpty(res)) {
            if (customProp.isString()) {
                request.setStringFilter(customProp, res);
            } else if (customProp.isBoolean()) {
                request.setBooleanFilter(customProp, "true".equalsIgnoreCase(res));
            } else {
                try {
                    request.setIntFilter(customProp, Integer.parseInt(res));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        } else {
            if (customProp.isString()) {
                request.setStringFilter(customProp, "");
            } else if (customProp.isBoolean()) {
                request.setBooleanFilter(customProp, false);
            } else {
                request.setIntFilter(customProp, -1);
            }
        }
    }
    request.setIntFilter(request.ALL.R_MINZOOM, ctx.zoom);
    request.saveState();
    NativeSearchResult res = searchObjectsForRendering(ctx.sleft, ctx.sright, ctx.stop, ctx.sbottom, ctx.zoom, request, true, rctx, "Nothing found");
    // ctx.zoomDelta =  1;
    // double scale = MapUtils.getPowZoom((float) ctx.zoomDelta);
    float scale = 1;
    if (renderingProps.get("density") != null) {
        scale *= Float.parseFloat(renderingProps.get("density"));
    }
    rctx.leftX = ctx.leftX * scale;
    rctx.topY = ctx.topY * scale;
    rctx.width = (int) (ctx.width * scale);
    rctx.height = (int) (ctx.height * scale);
    // map density scales corresponding to zoom delta
    // (so the distance between the road is the same)
    rctx.setDensityValue(scale);
    // rctx.textScale = 1f;//Text/icon scales according to mapDensity
    // Text/icon stays same for all sizes
    rctx.textScale = 1 / scale;
    if (renderingProps.get("textScale") != null) {
        rctx.textScale *= Float.parseFloat(renderingProps.get("textScale"));
    }
    rctx.screenDensityRatio = 1 / Math.max(1, 1f);
    final double tileDivisor = MapUtils.getPowZoom((float) (31 - ctx.zoom)) / scale;
    request.clearState();
    if (request.searchRenderingAttribute(RenderingRuleStorageProperties.A_DEFAULT_COLOR)) {
        rctx.defaultColor = request.getIntPropertyValue(request.ALL.R_ATTR_COLOR_VALUE);
    }
    request.clearState();
    request.setIntFilter(request.ALL.R_MINZOOM, ctx.zoom);
    if (request.searchRenderingAttribute(RenderingRuleStorageProperties.A_SHADOW_RENDERING)) {
        rctx.shadowRenderingMode = request.getIntPropertyValue(request.ALL.R_ATTR_INT_VALUE);
        rctx.shadowRenderingColor = request.getIntPropertyValue(request.ALL.R_SHADOW_COLOR);
    }
    rctx.zoom = ctx.zoom;
    rctx.tileDivisor = tileDivisor;
    long search = time + System.currentTimeMillis();
    final RenderingGenerationResult rres = NativeSwingRendering.generateRenderingIndirect(rctx, res.nativeHandler, false, request, true);
    long rendering = time + System.currentTimeMillis() - search;
    InputStream inputStream = new InputStream() {

        int nextInd = 0;

        @Override
        public int read() throws IOException {
            if (nextInd >= rres.bitmapBuffer.capacity()) {
                return -1;
            }
            byte b = rres.bitmapBuffer.get(nextInd++);
            if (b < 0) {
                return b + 256;
            } else {
                return b;
            }
        }
    };
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");
    ImageReader reader = readers.next();
    reader.setInput(new MemoryCacheImageInputStream(inputStream), true);
    BufferedImage img = reader.read(0);
    ctx.searchTime = search;
    ctx.renderingTime = rendering;
    ctx.context = rctx;
    long last = time + System.currentTimeMillis() - rendering;
    System.out.println(" TIMES search - " + search + " rendering - " + rendering + " unpack - " + last);
    res.deleteNativeResult();
    return img;
}
Also used : RenderingContext(net.osmand.RenderingContext) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) BufferedImage(java.awt.image.BufferedImage) RenderingRuleSearchRequest(net.osmand.render.RenderingRuleSearchRequest) ImageReader(javax.imageio.ImageReader)

Example 2 with RenderingContext

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

Aggregations

RenderingContext (net.osmand.RenderingContext)2 Paint (android.graphics.Paint)1 TIntArrayList (gnu.trove.list.array.TIntArrayList)1 BufferedImage (java.awt.image.BufferedImage)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ImageReader (javax.imageio.ImageReader)1 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)1 CallbackWithObject (net.osmand.CallbackWithObject)1 RenderedObject (net.osmand.NativeLibrary.RenderedObject)1 Amenity (net.osmand.data.Amenity)1 LatLon (net.osmand.data.LatLon)1 PointDescription (net.osmand.data.PointDescription)1 MapRenderRepositories (net.osmand.plus.render.MapRenderRepositories)1 NativeOsmandLibrary (net.osmand.plus.render.NativeOsmandLibrary)1 NewGpxPoint (net.osmand.plus.views.AddGpxPointBottomSheetHelper.NewGpxPoint)1 RenderingRuleProperty (net.osmand.render.RenderingRuleProperty)1