Skip to content

Commit

Permalink
Allow geometry transformation from crs=salem.Grid (#125)
Browse files Browse the repository at this point in the history
* Allow geometry transformation from salem.Grid

* Added test for transformation from salem.Grid
  • Loading branch information
matthiasdusch authored and fmaussion committed Oct 30, 2018
1 parent 6f5e707 commit ec097d7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 7 additions & 5 deletions salem/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ def lookup_transform(self, data, grid=None, method=np.mean, lut=None,
according to a user given rule (e.g. ``np.mean``, ``len``, ``np.std``),
applied to all grid points found below a grid point in ``self``.
See also :py:meth:`Grid.grid_lookup` and examples in the docs
Parameters
Expand Down Expand Up @@ -1144,12 +1144,12 @@ def to_dataset(self):

def to_geometry(self, to_crs=None):
"""Makes a geometrical representation of the grid (e.g. for drawing).
This can come also handy when doing shape-to-raster operations.
TODO: currently returns one polygon for each grid point, but this
could do more.
Returns
-------
a geopandas.GeoDataFrame
Expand Down Expand Up @@ -1260,6 +1260,8 @@ def transform_geometry(geom, crs=wgs84, to_crs=wgs84):
project = partial(transform_proj, from_crs, to_crs)
elif isinstance(to_crs, Grid):
project = partial(to_crs.transform, crs=from_crs)
elif isinstance(from_crs, Grid):
project = partial(from_crs.ij_to_crs, crs=to_crs)
else:
raise NotImplementedError()

Expand Down Expand Up @@ -1502,6 +1504,6 @@ def googlestatic_mercator_grid(center_ll=None, nx=640, ny=640, zoom=12, scale=1)
corner = (-xx / 2. + e, yy / 2. + n)
dxdy = (xx / nx, - yy / ny)

return Grid(proj=projloc, x0y0=corner,
return Grid(proj=projloc, x0y0=corner,
nxny=(nx, ny), dxdy=dxdy,
pixel_ref='corner')
3 changes: 3 additions & 0 deletions salem/tests/test_gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,9 @@ def test_geometry(self):
o = gis.transform_geometry(p, to_crs=g)
assert_allclose(p.exterior.coords, o.exterior.coords)

q = gis.transform_geometry(o, crs=g)
assert_allclose(p.exterior.coords, q.exterior.coords)

o = gis.transform_geometry(p, to_crs=g.center_grid)
totest = np.array(o.exterior.coords) + 0.5
assert_allclose(p.exterior.coords, totest)
Expand Down

0 comments on commit ec097d7

Please sign in to comment.