Move projection and tile arithmetic to its own file

This commit is contained in:
Eric Fischer 2014-10-24 17:22:14 -07:00
parent 754dbf152a
commit 46100c34df
9 changed files with 82 additions and 79 deletions

View File

@ -10,13 +10,13 @@ vector_tile.pb.cc vector_tile.pb.h: vector_tile.proto
PG=
tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o geometry.o
tippecanoe: geojson.o jsonpull.o vector_tile.pb.o tile.o clip.o pool.o mbtiles.o geometry.o projection.o
g++ $(PG) -O3 -g -Wall -o $@ $^ -lm -lz -lprotobuf-lite -lsqlite3
enumerate: enumerate.o
gcc $(PG) -O3 -g -Wall -o $@ $^ -lsqlite3
decode: decode.o vector_tile.pb.o
decode: decode.o vector_tile.pb.o projection.o
g++ $(PG) -O3 -g -Wall -o $@ $^ -lm -lz -lprotobuf-lite -lsqlite3
libjsonpull.a: jsonpull.o

View File

@ -7,6 +7,10 @@
#include <math.h>
#include "vector_tile.pb.h"
extern "C" {
#include "projection.h"
}
// https://github.com/mapbox/mapnik-vector-tile/blob/master/src/vector_tile_compression.hpp
inline bool is_compressed(std::string const& data) {
return data.size() > 2 && (uint8_t)data[0] == 0x78 && (uint8_t)data[1] == 0x9C;
@ -44,14 +48,6 @@ int dezig(unsigned n) {
return (n >> 1) ^ (-(n & 1));
}
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void tile2latlon(unsigned int x, unsigned int y, int zoom, double *lat, double *lon) {
unsigned long long n = 1LL << zoom;
*lon = 360.0 * x / n - 180.0;
double lat_rad = atan(sinh(M_PI * (1 - 2.0 * y / n)));
*lat = lat_rad * 180 / M_PI;
}
void handle(std::string message, int z, unsigned x, unsigned y) {
GOOGLE_PROTOBUF_VERIFY_VERSION;

View File

@ -43,7 +43,7 @@ void usage(char **argv) {
int main(int argc, char **argv) {
extern int optind;
extern char *optarg;
//extern char *optarg;
int i;
while ((i = getopt(argc, argv, "")) != -1) {

View File

@ -17,6 +17,7 @@
#include "tile.h"
#include "pool.h"
#include "mbtiles.h"
#include "projection.h"
int low_detail = 10;
int full_detail = 12;
@ -56,72 +57,6 @@ int mb_geometry[GEOM_TYPES] = {
VT_POLYGON,
};
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void latlon2tile(double lat, double lon, int zoom, unsigned int *x, unsigned int *y) {
double lat_rad = lat * M_PI / 180;
unsigned long long n = 1LL << zoom;
long long llx = n * ((lon + 180) / 360);
long long lly = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2;
if (lat >= 85.0511) {
lly = 0;
}
if (lat <= -85.0511) {
lly = n - 1;
}
if (llx < 0) {
llx = 0;
}
if (lly < 0) {
lly = 0;
}
if (llx >= n) {
llx = n - 1;
}
if (lly >= n) {
lly = n - 1;
}
*x = llx;
*y = lly;
}
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void tile2latlon(unsigned int x, unsigned int y, int zoom, double *lat, double *lon) {
unsigned long long n = 1LL << zoom;
*lon = 360.0 * x / n - 180.0;
float lat_rad = atan(sinh(M_PI * (1 - 2.0 * y / n)));
*lat = lat_rad * 180 / M_PI;
}
unsigned long long encode(unsigned int wx, unsigned int wy) {
long long out = 0;
int i;
for (i = 0; i < 32; i++) {
long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
v |= (wy >> (32 - (i + 1))) & 1;
v = v << (64 - 2 * (i + 1));
out |= v;
}
return out;
}
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
*wx = *wy = 0;
int i;
for (i = 0; i < 32; i++) {
*wx |= ((index >> (64 - 2 * (i + 1) + 1)) & 1) << (32 - (i + 1));
*wy |= ((index >> (64 - 2 * (i + 1) + 0)) & 1) << (32 - (i + 1));
}
}
int indexcmp(const void *v1, const void *v2) {
const struct index *i1 = v1;
const struct index *i2 = v2;

View File

@ -13,6 +13,7 @@
extern "C" {
#include "tile.h"
#include "clip.h"
#include "projection.h"
}
drawvec decode_geometry(char **meta, int z, unsigned tx, unsigned ty, int detail) {

68
projection.c Normal file
View File

@ -0,0 +1,68 @@
#include <math.h>
#include "projection.h"
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void latlon2tile(double lat, double lon, int zoom, unsigned int *x, unsigned int *y) {
double lat_rad = lat * M_PI / 180;
unsigned long long n = 1LL << zoom;
long long llx = n * ((lon + 180) / 360);
long long lly = n * (1 - (log(tan(lat_rad) + 1/cos(lat_rad)) / M_PI)) / 2;
if (lat >= 85.0511) {
lly = 0;
}
if (lat <= -85.0511) {
lly = n - 1;
}
if (llx < 0) {
llx = 0;
}
if (lly < 0) {
lly = 0;
}
if (llx >= n) {
llx = n - 1;
}
if (lly >= n) {
lly = n - 1;
}
*x = llx;
*y = lly;
}
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
void tile2latlon(unsigned int x, unsigned int y, int zoom, double *lat, double *lon) {
unsigned long long n = 1LL << zoom;
*lon = 360.0 * x / n - 180.0;
float lat_rad = atan(sinh(M_PI * (1 - 2.0 * y / n)));
*lat = lat_rad * 180 / M_PI;
}
unsigned long long encode(unsigned int wx, unsigned int wy) {
long long out = 0;
int i;
for (i = 0; i < 32; i++) {
long long v = ((wx >> (32 - (i + 1))) & 1) << 1;
v |= (wy >> (32 - (i + 1))) & 1;
v = v << (64 - 2 * (i + 1));
out |= v;
}
return out;
}
void decode(unsigned long long index, unsigned *wx, unsigned *wy) {
*wx = *wy = 0;
int i;
for (i = 0; i < 32; i++) {
*wx |= ((index >> (64 - 2 * (i + 1) + 1)) & 1) << (32 - (i + 1));
*wy |= ((index >> (64 - 2 * (i + 1) + 0)) & 1) << (32 - (i + 1));
}
}

4
projection.h Normal file
View File

@ -0,0 +1,4 @@
void latlon2tile(double lat, double lon, int zoom, unsigned int *x, unsigned int *y);
void tile2latlon(unsigned int x, unsigned int y, int zoom, double *lat, double *lon);
unsigned long long encode(unsigned int wx, unsigned int wy);
void decode(unsigned long long index, unsigned *wx, unsigned *wy);

View File

@ -19,6 +19,7 @@ extern "C" {
#include "pool.h"
#include "clip.h"
#include "mbtiles.h"
#include "projection.h"
}
#define CMD_BITS 3

2
tile.h
View File

@ -24,6 +24,4 @@ struct index {
struct index *next;
};
unsigned long long encode(unsigned int wx, unsigned int wy);
long long write_tile(struct index *start, struct index *end, char *metabase, unsigned *file_bbox, int z, unsigned x, unsigned y, int detail, int basezoom, struct pool *file_keys, char *layername, sqlite3 *outdb, double droprate);