diff --git a/.gitignore b/.gitignore index ed8ebf5..e8a373c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__ \ No newline at end of file +__pycache__ +.claude \ No newline at end of file diff --git a/code/prominence.cpp b/code/prominence.cpp index c6ccbba..adc060d 100644 --- a/code/prominence.cpp +++ b/code/prominence.cpp @@ -212,13 +212,16 @@ int main(int argc, char **argv) { auto threadPool = std::make_unique(numThreads); int num_tiles_processed = 0; vector> results; - // Use double precision to avoid accumulating floating-point error during loop - double lat = bounds[0]; - while (lat < bounds[1]) { + // Latitude range is half-open [bounds[0], bounds[1]); longitude range is + // closed [bounds[2], bounds[3]] and traversed east-to-west. + int numLatTiles = static_cast(std::round((bounds[1] - bounds[0]) / degreesAcross)); + int numLngTiles = static_cast(std::round((bounds[3] - bounds[2]) / degreesAcross)); + for (int latIndex = 0; latIndex < numLatTiles; ++latIndex) { + double lat = bounds[0] + latIndex * degreesAcross; // Go east-to-west to take advantage of some tile caching, since we need pixels // from our eastern neighbor. - double lng = bounds[3]; - while (lng >= bounds[2]) { + for (int lngIndex = 0; lngIndex <= numLngTiles; ++lngIndex) { + double lng = bounds[3] - lngIndex * degreesAcross; // Allow specifying longitude ranges that span the antimeridian (lng > 180) auto wrappedLng = lng; if (!fileFormat->isUtm() && wrappedLng >= 180) { @@ -229,8 +232,8 @@ int main(int argc, char **argv) { fileFormat->coordinateSystemForOrigin(lat, wrappedLng, utmZone)); // Skip tiles that don't intersect filtering polygon - if (!filter.intersects(lat, lat + fileFormat->degreesAcross(), - lng, lng + fileFormat->degreesAcross())) { + if (!filter.intersects(lat, lat + degreesAcross, + lng, lng + degreesAcross)) { VLOG(3) << "Skipping tile that doesn't intersect polygon " << lat << " " << lng; } else { // Actually calculate prominence @@ -239,10 +242,7 @@ int main(int argc, char **argv) { return task->run(lat, wrappedLng, *coordinateSystem); })); } - - lng -= fileFormat->degreesAcross(); } - lat += fileFormat->degreesAcross(); } for (auto && result : results) { diff --git a/scripts/run_prominence.py b/scripts/run_prominence.py index 907816b..f15fffb 100644 --- a/scripts/run_prominence.py +++ b/scripts/run_prominence.py @@ -117,8 +117,14 @@ def get_extent_transformed(src_ds, target_srs_name): transform = osr.CoordinateTransformation(src_srs, target_srs) projected = [transform.TransformPoint(x, y)[:2] for x, y in corners] - xs = [(x + 180) % 360 - 180 for x, _ in projected] # Deal with antimeridian + raw_xs = [x for x, _ in projected] ys = [y for _, y in projected] + # A dataset covering the whole globe in longitude (e.g. -180..180) would have + # its right edge wrapped onto its left edge by the antimeridian normalization + # below, collapsing the extent to zero width. Handle it explicitly. + if max(raw_xs) - min(raw_xs) >= 360 - 1e-6: + return -180, min(ys), 180, max(ys) + xs = [(x + 180) % 360 - 180 for x in raw_xs] # Deal with antimeridian return min(xs), min(ys), max(xs), max(ys) def create_vrts(tile_dir, input_files, skip_boundary, tile_srs, threads):