Skip to content

Commit

Permalink
Merge pull request #276 from mraspaud/feature-from_epsg
Browse files Browse the repository at this point in the history
Create AreaDefinition from epsg codes
  • Loading branch information
mraspaud authored May 13, 2020
2 parents 92ad021 + 6ec251c commit 914a424
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,25 @@ def y_size(self):
warnings.warn("'y_size' is deprecated, use 'height' instead.", PendingDeprecationWarning)
return self.height

@classmethod
def from_epsg(cls, code, resolution):
"""Create an AreaDefinition object from an epsg code (string or int) and a resolution."""
if CRS is None:
raise NotImplementedError
crs = CRS('EPSG:' + str(code))
bounds = crs.area_of_use.bounds
proj = Proj(crs)
left1, low1 = proj(bounds[0], bounds[1])
right1, up1 = proj(bounds[2], bounds[3])
left2, up2 = proj(bounds[0], bounds[3])
right2, low2 = proj(bounds[2], bounds[1])
left = min(left1, left2)
right = max(right1, right2)
up = max(up1, up2)
low = min(low1, low2)
area_extent = (left, low, right, up)
return create_area_def(crs.name, crs.to_dict(), area_extent=area_extent, resolution=resolution)

@classmethod
def from_extent(cls, area_id, projection, shape, area_extent, units=None, **kwargs):
"""Create an AreaDefinition object from area_extent and shape.
Expand Down
15 changes: 15 additions & 0 deletions pyresample/test/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1326,6 +1326,21 @@ def test_area_def_geocentric_resolution(self):
geo_res = area_def.geocentric_resolution()
np.testing.assert_allclose(298.647232, geo_res)

def test_from_epsg(self):
"""Test the from_epsg class method."""
from pyresample.geometry import AreaDefinition
sweref = AreaDefinition.from_epsg('3006', 2000)
assert sweref.name == 'SWEREF99 TM'
assert sweref.proj_dict == {'ellps': 'GRS80', 'no_defs': None,
'proj': 'utm', 'type': 'crs', 'units': 'm',
'zone': 33}
assert sweref.width == 453
assert sweref.height == 794
import numpy as np
np.testing.assert_allclose(sweref.area_extent,
(181896.3291, 6101648.0705,
1086312.942376, 7689478.3056))

@unittest.skipIf(CRS is None, "pyproj 2.0+ required")
def test_area_def_init_projection(self):
"""Test AreaDefinition with different projection definitions."""
Expand Down

0 comments on commit 914a424

Please sign in to comment.