Skip to content

Commit

Permalink
rewrite resample to use CoordinateGenerator
Browse files Browse the repository at this point in the history
RegularGrid.resample now uses CoordinateGenerator rather than building
arrays with numpy.meshgrid

This may make future efficiency improvements easier as they can be
focused on CoordinateGenerator
  • Loading branch information
njwilson23 committed Mar 26, 2017
1 parent 58fe8c5 commit d2a7ce3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions karta/raster/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -756,21 +756,23 @@ def resample(self, dx, dy, method='nearest'):
"(got {0}, {1})".format(dx, dy))

xmin, xmax, ymin, ymax = self.get_extent()
ny = int((ymax - ymin) // (dy / self.resolution[1])) + 1
nx = int((xmax - xmin) // (dx / self.resolution[0])) + 1

t = self._transform
tnew = (xmin-0.5*dx-0.5*t[4], ymin-0.5*dy-0.5*t[5], dx, dy, t[4], t[5])
cg = CoordinateGenerator(tnew, (ny, nx), self.crs, self.crs)
X, Y = cg[:,:]

if method == 'nearest':
X, Y = np.meshgrid(np.arange(xmin, xmax, dx),
np.arange(ymin, ymax, dy))
values = self.sample_nearest(X, Y)
elif method == 'linear':
X, Y = np.meshgrid(np.arange(xmin, xmax, dx),
np.arange(ymin, ymax, dy))
values = self.sample_bilinear(X, Y)
else:
raise NotImplementedError('method "{0}" unavailable'.format(method))

if values.ndim == 3:
values = values.transpose(1, 2, 0)
t = self._transform
tnew = (xmin-0.5*dx-0.5*t[4], ymin-0.5*dy-0.5*t[5], dx, dy, t[4], t[5])
return RegularGrid(tnew, values=values, crs=self.crs,
nodata_value=self.nodata)

Expand Down

0 comments on commit d2a7ce3

Please sign in to comment.