From 46100c34dfd6c3bc71be9f131d94ea305cccf543 Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Fri, 24 Oct 2014 17:22:14 -0700 Subject: [PATCH] Move projection and tile arithmetic to its own file --- Makefile | 4 ++-- decode.cc | 12 ++++------ enumerate.c | 2 +- geojson.c | 67 +-------------------------------------------------- geometry.cc | 1 + projection.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ projection.h | 4 ++++ tile.cc | 1 + tile.h | 2 -- 9 files changed, 82 insertions(+), 79 deletions(-) create mode 100644 projection.c create mode 100644 projection.h diff --git a/Makefile b/Makefile index 11567adb..40c3a5d0 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/decode.cc b/decode.cc index 9d39b8f2..5fc539d7 100644 --- a/decode.cc +++ b/decode.cc @@ -7,6 +7,10 @@ #include #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; diff --git a/enumerate.c b/enumerate.c index c2c319a4..6d4cfdfb 100644 --- a/enumerate.c +++ b/enumerate.c @@ -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) { diff --git a/geojson.c b/geojson.c index 0424f99f..1c094f82 100644 --- a/geojson.c +++ b/geojson.c @@ -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; diff --git a/geometry.cc b/geometry.cc index 6e3818c8..a5405656 100644 --- a/geometry.cc +++ b/geometry.cc @@ -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) { diff --git a/projection.c b/projection.c new file mode 100644 index 00000000..a66a856d --- /dev/null +++ b/projection.c @@ -0,0 +1,68 @@ +#include +#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)); + } +} diff --git a/projection.h b/projection.h new file mode 100644 index 00000000..af7002ef --- /dev/null +++ b/projection.h @@ -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); diff --git a/tile.cc b/tile.cc index 3f052544..a84622e7 100644 --- a/tile.cc +++ b/tile.cc @@ -19,6 +19,7 @@ extern "C" { #include "pool.h" #include "clip.h" #include "mbtiles.h" + #include "projection.h" } #define CMD_BITS 3 diff --git a/tile.h b/tile.h index c41c32d1..81903613 100644 --- a/tile.h +++ b/tile.h @@ -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);