Skip to content

Commit

Permalink
Crunch down memory required per polygon joint
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Sep 20, 2023
1 parent 390771f commit f1d9aab
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
12 changes: 8 additions & 4 deletions mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,21 @@ sqlite3 *mbtiles_open(char *dbname, char **argv, int forcetable) {
return outdb;
}

void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size) {
unsigned long long fnv1a(std::string const &s) {
// Store tiles by a hash of their contents (fnv1a 64-bit)
// http://www.isthe.com/chongo/tech/comp/fnv/
const unsigned long long fnv_offset_basis = 14695981039346656037u;
const unsigned long long fnv_prime = 1099511628211u;
unsigned long long h = fnv_offset_basis;
for (int i = 0; i < size; i++) {
h ^= (unsigned char) data[i];
for (size_t i = 0; i < s.size(); i++) {
h ^= (unsigned char) s[i];
h *= fnv_prime;
}
std::string hash = std::to_string(h);
return h;
}

void mbtiles_write_tile(sqlite3 *outdb, int z, int tx, int ty, const char *data, int size) {
std::string hash = std::to_string(fnv1a(std::string(data, size)));

// following https://github.com/mapbox/node-mbtiles/blob/master/lib/mbtiles.js

Expand Down
2 changes: 2 additions & 0 deletions mbtiles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ std::map<std::string, layermap_entry> merge_layermaps(std::vector<std::map<std::

void add_to_file_keys(std::map<std::string, type_and_string_stats> &file_keys, std::string const &layername, type_and_string const &val);

unsigned long long fnv1a(std::string const &s);

#endif
37 changes: 19 additions & 18 deletions tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,30 +1295,31 @@ long long choose_minextent(std::vector<long long> &extents, double f) {
}

struct joint {
draw p1;
draw mid;
draw p2;
long long x : 34; // enough to wrap around the world either way
long long y : 33; // enough to touch the top and bottom of the world

unsigned long long p1p2 : 61;

joint(draw one, draw hinge, draw two) {
if (one < two) {
p1 = one;
p2 = two;
} else {
p1 = two;
p2 = one;
std::swap(one, two);
}

mid = hinge;
long long coord[4] = {one.x, one.y, two.x, two.y};
p1p2 = fnv1a(std::string((const char *) &coord, sizeof(coord)));

x = hinge.x;
y = hinge.y;
}

bool operator<(const joint &o) const {
if (mid < o.mid) {
if (y < o.y) {
return true;
} else if (mid == o.mid) {
if (p1 < o.p1) {
} else if (y == o.y) {
if (x < o.x) {
return true;
} else if (p1 == o.p1) {
if (p2 < o.p2) {
} else if (x == o.x) {
if (p1p2 < o.p1p2) {
return true;
}
}
Expand Down Expand Up @@ -2433,10 +2434,10 @@ long long write_tile(decompressor *geoms, std::atomic<long long> *geompos_in, ch

std::sort(shared_joints.begin(), shared_joints.end());
for (size_t i = 0; i + 1 < shared_joints.size(); i++) {
if (shared_joints[i].mid == shared_joints[i + 1].mid) {
if (shared_joints[i].p1 != shared_joints[i + 1].p1 ||
shared_joints[i].p2 != shared_joints[i + 1].p2) {
shared_nodes.push_back(shared_joints[i].mid);
if (shared_joints[i].x == shared_joints[i + 1].x &&
shared_joints[i].y == shared_joints[i + 1].y) {
if (shared_joints[i].p1p2 != shared_joints[i + 1].p1p2) {
shared_nodes.push_back(draw(VT_MOVETO, shared_joints[i].x, shared_joints[i].y));
}
}
}
Expand Down

0 comments on commit f1d9aab

Please sign in to comment.