shithub: map

ref: c6a16dc58a2cb3152cd0b08b81fbf85df713280b
dir: /osm.c/

View raw version
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "fns.h"

static double
asinh(double x)
{
	double s = sqrt(x*x + 1);
	return log(x + s);
}

int
lon2tilex(double lon, int z)
{
	return (int)(floor((lon + 180.0) / 360.0 * (1<<z)));
}

int
lat2tiley(double lat, int z)
{
	double latrad = lat * PI/180.0;
	return (int)(floor((1.0 - asinh(tan(latrad)) / PI) / 2.0 * (1<<z)));
}

double tilex2long(int x, int z)
{
	return x / (double)(1<<z) * 360.0 - 180.0;
}

double tiley2lat(int y, int z)
{
	double n = PI - 2.0 * PI * y / (double)(1<<z);
	return 180.0 / PI * atan(0.5 * (exp(n) - exp(-n)));
}

GBundle
getbundle(GPos pos, int z)
{
	GBundle ret;
	ret.x = lon2tilex(pos.lon, z);
	ret.y = lat2tiley(pos.lat, z);
	ret.z = z;
	return ret;
}

static char*
defaultformaturl(GBundle b)
{
	return smprint("https://tile.openstreetmap.org/%d/%d/%d.png", b.z, b.x, b.y);
}

static GProvider defaultprovider = {
	.formaturl = defaultformaturl,
};

GProvider*
getprovider()
{
	return &defaultprovider;
}