Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix precision problems with very high-resolution tiles #28

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions code/prominence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,10 @@ int main(int argc, char **argv) {
auto threadPool = std::make_unique<ThreadPool>(numThreads);
int num_tiles_processed = 0;
vector<std::future<bool>> results;
float lat = bounds[0];
// Use double precision to avoid accumulating floating-point error during loop
double lat = bounds[0];
while (lat < bounds[1]) {
float lng = bounds[2];
double lng = bounds[2];
while (lng < bounds[3]) {
// Allow specifying longitude ranges that span the antimeridian (lng > 180)
auto wrappedLng = lng;
Expand All @@ -209,17 +210,21 @@ int main(int argc, char **argv) {
}

std::shared_ptr<CoordinateSystem> coordinateSystem(
fileFormat.coordinateSystemForOrigin(lat, wrappedLng, utmZone));
fileFormat.coordinateSystemForOrigin(
static_cast<float>(lat), static_cast<float>(wrappedLng), utmZone));

// Skip tiles that don't intersect filtering polygon
if (!filter.intersects(lat, lat + fileFormat.degreesAcross(),
lng, lng + fileFormat.degreesAcross())) {
if (!filter.intersects(static_cast<float>(lat),
static_cast<float>(lat + fileFormat.degreesAcross()),
static_cast<float>(lng),
static_cast<float>(lng + fileFormat.degreesAcross()))) {
VLOG(3) << "Skipping tile that doesn't intersect polygon " << lat << " " << lng;
} else {
// Actually calculate prominence
ProminenceTask *task = new ProminenceTask(cache.get(), options);
results.push_back(threadPool->enqueue([=] {
return task->run(lat, wrappedLng, *coordinateSystem);
return task->run(static_cast<float>(lat), static_cast<float>(wrappedLng),
*coordinateSystem);
}));
}

Expand Down
16 changes: 11 additions & 5 deletions code/prominence_task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "divide_tree.h"
#include "island_tree.h"
#include "tree_builder.h"
#include "util.h"

#include "easylogging++.h"

Expand Down Expand Up @@ -55,7 +56,7 @@ bool ProminenceTask::run(float lat, float lng, const CoordinateSystem &coordinat
VLOG(2) << "Couldn't load tile for " << lat << " " << lng;
return false;
}

// Flip tile upside down if we're computing anti-prominence
if (mOptions.antiprominence) {
tile->flipElevations();
Expand Down Expand Up @@ -115,11 +116,16 @@ bool ProminenceTask::writeStringToOutputFile(const string &filename, const strin

string ProminenceTask::getFilenamePrefix() const {
char filename[PATH_MAX];
int latHundredths = fractionalDegree(mCurrentLatitude);
int lngHundredths = fractionalDegree(mCurrentLongitude);

// Deal with floating point imprecision
float lat = adjustCoordinate(mCurrentLatitude);
float lng = adjustCoordinate(mCurrentLongitude);

int latHundredths = fractionalDegree(lat);
int lngHundredths = fractionalDegree(lng);
snprintf(filename, sizeof(filename), "prominence-%02dx%02d-%03dx%02d",
static_cast<int>(mCurrentLatitude), latHundredths,
static_cast<int>(mCurrentLongitude), lngHundredths);
static_cast<int>(lat), latHundredths,
static_cast<int>(lng), lngHundredths);
return mOptions.outputDir + "/" + filename;
}

Expand Down
6 changes: 6 additions & 0 deletions code/tile_loading_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Tile *BasicTileLoadingPolicy::loadTile(float minLat, float minLng) const {
Tile *BasicTileLoadingPolicy::loadInternal(float minLat, float minLng) const {
TileLoader *loader = nullptr;

// If the lat/lng can't be represented exactly in floating point,
// there's sometimes roundoff error when converting to integers for
// tile filenames. Adding a little slop prevents truncation.
minLat = adjustCoordinate(minLat);
minLng = adjustCoordinate(minLng);

switch (mFileFormat.value()) {
case FileFormat::Value::HGT:
loader = new HgtLoader();
Expand Down
7 changes: 7 additions & 0 deletions code/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ float metersToFeet(float meters) {
return meters / 0.3048f;
}

float adjustCoordinate(float coordinate) {
// A tile is not going to be smaller than 0.1 degrees or so, so this
// amount should be safe.
const float epsilon = 0.001f;
return coordinate + ((coordinate >= 0) ? epsilon : -epsilon);
}

string trim(const string &s) {
static const std::string whitespace = " \t\f\v\n\r";
std::size_t start = s.find_first_not_of(whitespace);
Expand Down
4 changes: 4 additions & 0 deletions code/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ float metersToFeet(float meters);
// Trim whitespace from the start and end of the string, return a new string
std::string trim(const std::string &s);

// Adjust the given coordinate by an epsilon value away from 0, so that truncation
// to int doesn't give incorrect values to due floating-point imprecision.
float adjustCoordinate(float coordinate);

/*
* Split the given string by the given delimiter, putting the
* result in elems and returning it.
Expand Down