View Single Post
Posts: 31 | Thanked: 2 times | Joined on Apr 2006 @ Vilnius, Lithuania
#66
Here's a function to convert the google street map coordinates (x, y, zoomlevel) to the quadtree index string used by the google satellite maps (http://kh0.google.com/kh?n=404&v=6&t=%s).
Code:
void convert(int x, int y, int zoomlevel, char * buffer)
{
    char * quadrant = "qrts";
    char * ptr = buffer;
    int n;
    *ptr++ = 't';
    for (n = 16-zoomlevel; n >= 0; n--) {
        int xbit = (x >> n) & 1;
        int ybit = (y >> n) & 1;
        *ptr++ = quadrant[xbit + 2 * ybit];
    }
    *ptr++ = '\0';
}
Here's a set of unit tests for this function.
Code:
#include <stdio.h>
#include <string.h>

int test(int x, int y, int zoomlevel, char * expected)
{
    char buffer[19];
    convert(x, y, zoomlevel, buffer);
    if (strcmp(buffer, expected) != 0) {
        printf("failure with (%d, %d, %d): expected \"%s\", got \"%s\".\n",
               x, y, zoomlevel, expected, buffer);
        return 1;
    }
    return 0;
}

int main()
{
    int failures = 0;
    failures += test(0, 0, 17, "t");
    failures += test(0, 0, 16, "tq");
    failures += test(1, 0, 16, "tr");
    failures += test(1, 1, 16, "ts");
    failures += test(0, 1, 16, "tt");
    failures += test(0, 0, 15, "tqq");
    failures += test(1, 0, 15, "tqr");
    failures += test(2, 0, 15, "trq");
    failures += test(3, 0, 15, "trr");
    failures += test(2335, 1295, 5, "trtqsqqqrssss");
    if (!failures) {
        printf("All tests passed.\n");
        return 0;
    } else {
        return 1;
    }
}
Did I mention that I would be very happy to see Google satellite map support in MaemoMapper?