From ce0fba0947dc769b1e9e2e9297d7b464135bfa3d Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Fri, 12 Feb 2021 12:35:59 +0100 Subject: [PATCH] Add methods to calculate precision xy --- .../main/java/net/osmand/util/MapUtils.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/MapUtils.java b/OsmAnd-java/src/main/java/net/osmand/util/MapUtils.java index 7a37eee3aa..a9c3411b68 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/MapUtils.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/MapUtils.java @@ -48,6 +48,47 @@ public class MapUtils { 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '~' }; + + public static int calculateFromBaseZoomPrecisionXY(int bazeZoom, int finalZoom, int xBase, int yBase) { + int px = xBase; + int py = yBase; + int precisionNumber = 1; + for (int zoom = finalZoom - 1; zoom >= bazeZoom; zoom--) { + int x = px / 2; // (int) MapUtils.getTileNumberX(zoom, lon); + int y = py / 2; // (int) MapUtils.getTileNumberY(zoom, lat); + int deltax = px - x * 2; + int deltay = py - y * 2; + precisionNumber = (precisionNumber << 2) + (deltax << 1) + deltay; + // StringBuilder spaces = new StringBuilder(); + // for (int i = 0; i < 32 - zoom; i++) { + // spaces.append(' '); + // } + // System.out.println(String.format("%d %s + %d %s %s + %d", zoom, Integer.toBinaryString(x), deltax, spaces.toString(), Integer.toBinaryString(y), deltay)); + px = x; + py = y; + } + System.out.println(String.format("Bits: %d %s (%d)", Integer.toBinaryString(precisionNumber).length(), Integer.toBinaryString(precisionNumber), precisionNumber)); + return precisionNumber; + } + + public static int[] calculateFinalXYFromBaseAndPrecisionXY(int bazeZoom, int finalZoom, + int precisionXY, int xBase, int yBase, boolean ignoreNotEnoughPrecision) { + // System.out.println(String.format("Base x, y at zoom %d: %d %d", zoomToStart, xBaseApproximation, yBaseApproximation)); + // calculate finish approximation using precisionNumber + int finalX = xBase; + int finalY = yBase; + int precisionCalc = precisionXY; + for (int zoom = bazeZoom; zoom < finalZoom; zoom++) { + if (precisionCalc <= 1 && precisionCalc > 0 && !ignoreNotEnoughPrecision) { + throw new IllegalArgumentException("Not enough bits to retrieve zoom approximation"); + } + finalY = finalY * 2 + (precisionXY & 1); + finalX = finalX * 2 + ((precisionXY & 2) >> 1); + precisionXY = precisionXY >> 2; + } + // System.out.println(String.format("Calc x, y at zoom %d: %d %d", finalZoom, finalX, finalY)); + return new int[] { finalX, finalY }; + }