From 99e634aaba1a915aaff4e94c4233024a4f1db694 Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 29 Apr 2016 19:59:53 -0400 Subject: [PATCH] [fix] some weirdness with the dateline and polygons that have a longitude of exactly 180.0 --- scripts/geodata/math/floats.py | 5 +++++ scripts/geodata/math/sampling.py | 6 +----- scripts/geodata/osm/admin_boundaries.py | 5 +++++ 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 scripts/geodata/math/floats.py diff --git a/scripts/geodata/math/floats.py b/scripts/geodata/math/floats.py new file mode 100644 index 00000000..1051e332 --- /dev/null +++ b/scripts/geodata/math/floats.py @@ -0,0 +1,5 @@ +FLOAT_EPSILON = 1e-09 + + +def isclose(a, b, rel_tol=FLOAT_EPSILON, abs_tol=0.0): + return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) diff --git a/scripts/geodata/math/sampling.py b/scripts/geodata/math/sampling.py index a0278464..33b94004 100644 --- a/scripts/geodata/math/sampling.py +++ b/scripts/geodata/math/sampling.py @@ -2,7 +2,7 @@ import bisect import random import sys -FLOAT_EPSILON = 1e-09 +from geodata.math.floats import isclose, FLOAT_EPSILON def weighted_choice(values, cdf): @@ -16,10 +16,6 @@ def weighted_choice(values, cdf): return values[i] -def isclose(a, b, rel_tol=FLOAT_EPSILON, abs_tol=0.0): - return abs(a - b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol) - - def check_probability_distribution(probs): cumulative = 0.0 for p in probs: diff --git a/scripts/geodata/osm/admin_boundaries.py b/scripts/geodata/osm/admin_boundaries.py index 4be5142a..2e9a349d 100644 --- a/scripts/geodata/osm/admin_boundaries.py +++ b/scripts/geodata/osm/admin_boundaries.py @@ -15,6 +15,7 @@ from itertools import izip, combinations from geodata.coordinates.conversion import latlon_to_decimal from geodata.graph.scc import strongly_connected_components +from geodata.math.floats import isclose from geodata.osm.extract import * @@ -179,6 +180,10 @@ class OSMPolygonReader(object): lat, lon = latlon_to_decimal(lat, lon) if lat is None or lon is None: continue + + if isclose(lon, 180.0): + lon = 179.999 + # Nodes are stored in a sorted array, coordinate indices are simply # [lon, lat, lon, lat ...] so the index can be calculated as 2 * i # Note that the pairs are lon, lat instead of lat, lon for geometry purposes