Blob Blame History Raw
From ad04b17fb9751816753d2416ccad05236b41cf1e Mon Sep 17 00:00:00 2001
From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Date: Tue, 27 Feb 2018 20:38:49 -0500
Subject: [PATCH 5/7] Normalize grid coordinates used for interpolation.

Most coordinates are in metres and thus fairly large. This can cause
precision issues in Qhull as it calculates distances via squares and
square roots, at least on non-x86_64 architectures.

This is equivalent to the `rescale=True` argument in SciPy, but doing it
ourselves means a) we don't need to bump requirements to 0.14 and b)
re-scaling is not done for every scalar/vector property.

Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
---
 lib/cartopy/vector_transform.py                    |  10 ++++++----
 7 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lib/cartopy/vector_transform.py b/lib/cartopy/vector_transform.py
index 1fe17a1..c684703 100644
--- a/lib/cartopy/vector_transform.py
+++ b/lib/cartopy/vector_transform.py
@@ -56,15 +56,17 @@ def _interpolate_to_grid(nx, ny, x, y, *scalars, **kwargs):
     target_extent = kwargs.get('target_extent', None)
     if target_extent is None:
         target_extent = (x.min(), x.max(), y.min(), y.max())
-    points = np.array([x.ravel(), y.ravel()]).T
     x0, x1, y0, y1 = target_extent
-    x_grid, y_grid = np.meshgrid(np.linspace(x0, x1, nx),
-                                 np.linspace(y0, y1, ny))
+    xr = x1 - x0
+    yr = y1 - y0
+    points = np.column_stack([(x.ravel() - x0) / xr, (y.ravel() - y0) / yr])
+    x_grid, y_grid = np.meshgrid(np.linspace(0, 1, nx),
+                                 np.linspace(0, 1, ny))
     s_grid_tuple = tuple()
     for s in scalars:
         s_grid_tuple += (griddata(points, s.ravel(), (x_grid, y_grid),
                                   method='linear'),)
-    return (x_grid, y_grid) + s_grid_tuple
+    return (x_grid * xr + x0, y_grid * yr + y0) + s_grid_tuple
 
 
 def vector_scalar_to_grid(src_crs, target_proj, regrid_shape, x, y, u, v,
-- 
2.14.3