Add methods to calculate precision xy

This commit is contained in:
Victor Shcherb 2021-02-12 12:35:59 +01:00
parent d51683778d
commit ce0fba0947

View file

@ -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 };
}