def __init__(self, projparams=None, preserve_units=True, **kwargs): # Copy dict-type arguments as they will be modified in-place if isinstance(projparams, dict): projparams = projparams.copy() # Pyproj<2 uses __new__ to initiate data and does not define its own __init__ method. if is_pyproj2(): # If init is found in any of the data, override any other area parameters. if 'init' in kwargs: warnings.warn( 'init="EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead' ) projparams = kwargs.pop('init') # Proj takes params in projparams over the params in kwargs. if isinstance(projparams, (dict, str)) and 'init' in projparams: warn_msg = '{"init": "EPSG:XXXX"} is no longer supported. Use "EPSG:XXXX" as a proj string instead' if isinstance(projparams, str): warn_msg = '"+init=EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead' # Proj-dicts are cleaner to parse than strings. projparams = proj4_str_to_dict(projparams) warnings.warn(warn_msg) projparams = projparams.pop('init') # New syntax 'EPSG:XXXX' if 'EPSG' in kwargs or (isinstance(projparams, dict) and 'EPSG' in projparams): if 'EPSG' in kwargs: epsg_code = kwargs.pop('EPSG') else: epsg_code = projparams.pop('EPSG') projparams = 'EPSG:{}'.format(epsg_code) super(BaseProj, self).__init__(projparams=projparams, preserve_units=preserve_units, **kwargs)
def test_get_area_def_from_raster(self): from pyresample import utils from rasterio.crs import CRS from affine import Affine x_size = 791 y_size = 718 transform = Affine(300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0) crs = CRS(init='epsg:3857') if utils.is_pyproj2(): # pyproj 2.0+ expands CRS parameters from pyproj import CRS proj_dict = CRS(3857).to_dict() else: proj_dict = crs.to_dict() source = tmptiff(x_size, y_size, transform, crs=crs) area_id = 'area_id' proj_id = 'proj_id' description = 'name' area_def = utils._rasterio.get_area_def_from_raster(source, area_id=area_id, name=description, proj_id=proj_id) self.assertEqual(area_def.area_id, area_id) self.assertEqual(area_def.proj_id, proj_id) self.assertEqual(area_def.description, description) self.assertEqual(area_def.width, x_size) self.assertEqual(area_def.height, y_size) self.assertDictEqual(proj_dict, area_def.proj_dict) self.assertTupleEqual( area_def.area_extent, (transform.c, transform.f + transform.e * y_size, transform.c + transform.a * x_size, transform.f))
def test_def2yaml_converter(self): from pyresample import parse_area_file, convert_def_to_yaml from pyresample.utils import is_pyproj2 import tempfile def_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg') filehandle, yaml_file = tempfile.mkstemp() os.close(filehandle) try: convert_def_to_yaml(def_file, yaml_file) areas_new = set(parse_area_file(yaml_file)) areas = parse_area_file(def_file) for area in areas: if is_pyproj2(): # pyproj 2.0 adds units back in # pyproj <2 doesn't continue # initialize _proj_dict area.proj_dict # noqa area._proj_dict.pop('units', None) areas_old = set(areas) areas_new = {area.area_id: area for area in areas_new} areas_old = {area.area_id: area for area in areas_old} self.assertEqual(areas_new, areas_old) finally: os.remove(yaml_file)
def __init__(self, projparams=None, preserve_units=True, **kwargs): if is_pyproj2(): # have to have this because pyproj uses __new__ # subclasses would fail when calling __init__ otherwise super(BaseProj, self).__init__(projparams=projparams, preserve_units=preserve_units, **kwargs)
def __init__(self, projparams=None, preserve_units=True, **kwargs): # Copy dict-type arguments as they will be modified in-place if isinstance(projparams, dict): projparams = projparams.copy() # Pyproj<2 uses __new__ to initiate data and does not define its own __init__ method. if is_pyproj2(): # If init is found in any of the data, override any other area parameters. if 'init' in kwargs: warnings.warn('init="EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead') projparams = kwargs.pop('init') # Proj takes params in projparams over the params in kwargs. if isinstance(projparams, (dict, str)) and 'init' in projparams: warn_msg = '{"init": "EPSG:XXXX"} is no longer supported. Use "EPSG:XXXX" as a proj string instead' if isinstance(projparams, str): warn_msg = '"+init=EPSG:XXXX" is no longer supported. Use "EPSG:XXXX" as a proj string instead' # Proj-dicts are cleaner to parse than strings. projparams = proj4_str_to_dict(projparams) warnings.warn(warn_msg) projparams = projparams.pop('init') # New syntax 'EPSG:XXXX' if 'EPSG' in kwargs or (isinstance(projparams, dict) and 'EPSG' in projparams): if 'EPSG' in kwargs: epsg_code = kwargs.pop('EPSG') else: epsg_code = projparams.pop('EPSG') projparams = 'EPSG:{}'.format(epsg_code) super(BaseProj, self).__init__(projparams=projparams, preserve_units=preserve_units, **kwargs)
def test_proj4_string(self): """Test 'proj_str' property of AreaDefinition.""" from pyresample.utils import is_pyproj2 proj4_string = self.area_def.proj_str expected_string = '+a=6378144.0 +b=6356759.0 +lat_ts=50.0 +lon_0=8.0 +proj=stere +lat_0=50.0' if is_pyproj2(): expected_string += ' +type=crs' self.assertEqual(frozenset(proj4_string.split()), frozenset(expected_string.split()))
def test_area_parser_legacy(self): """Test legacy area parser.""" from pyresample import parse_area_file from pyresample.utils import is_pyproj2 ease_nh, ease_sh = parse_area_file( os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ease_nh', 'ease_sh') if is_pyproj2(): # pyproj 2.0+ adds some extra parameters projection = ("{'R': '6371228', 'lat_0': '90', 'lon_0': '0', " "'no_defs': 'None', 'proj': 'laea', 'type': 'crs', " "'units': 'm', 'x_0': '0', 'y_0': '0'}") else: projection = ("{'a': '6371228.0', 'lat_0': '90.0', " "'lon_0': '0.0', 'proj': 'laea', 'units': 'm'}") nh_str = """Area ID: ease_nh Description: Arctic EASE grid Projection ID: ease_nh Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(ease_nh.__str__(), nh_str) self.assertIsInstance(ease_nh.proj_dict['lat_0'], (int, float)) if is_pyproj2(): projection = ("{'R': '6371228', 'lat_0': '-90', 'lon_0': '0', " "'no_defs': 'None', 'proj': 'laea', 'type': 'crs', " "'units': 'm', 'x_0': '0', 'y_0': '0'}") else: projection = ("{'a': '6371228.0', 'lat_0': '-90.0', " "'lon_0': '0.0', 'proj': 'laea', 'units': 'm'}") sh_str = """Area ID: ease_sh Description: Antarctic EASE grid Projection ID: ease_sh Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(ease_sh.__str__(), sh_str) self.assertIsInstance(ease_sh.proj_dict['lat_0'], (int, float))
def test_proj4_string(self): """Test 'proj_str' property of AreaDefinition.""" from pyresample.utils import is_pyproj2 proj4_string = self.area_def.proj_str expected_string = '+a=6378144.0 +b=6356759.0 +lat_ts=50.0 +lon_0=8.0 +proj=stere +lat_0=50.0' if is_pyproj2(): expected_string = '+a=6378144 +k=1 +lat_0=50 +lon_0=8 ' \ '+no_defs +proj=stere +rf=298.253168108487 ' \ '+type=crs +units=m +x_0=0 +y_0=0' self.assertEqual( frozenset(proj4_string.split()), frozenset(expected_string.split()))
def test_get_area_def_from_raster_non_georef_respects_proj_dict(self): from pyresample import utils from affine import Affine transform = Affine(300.0379266750948, 0.0, 101985.0, 0.0, -300.041782729805, 2826915.0) source = tmptiff(transform=transform) proj_dict = {'init': 'epsg:3857'} area_def = utils._rasterio.get_area_def_from_raster(source, proj_dict=proj_dict) if utils.is_pyproj2(): from pyproj import CRS proj_dict = CRS(3857).to_dict() self.assertDictEqual(area_def.proj_dict, proj_dict)
def test_load_area(self): from pyresample import load_area from pyresample.utils import is_pyproj2 ease_nh = load_area(os.path.join(os.path.dirname(__file__), 'test_files', 'areas.cfg'), 'ease_nh') if is_pyproj2(): # pyproj 2.0+ adds some extra parameters projection = ("{'R': '6371228', 'lat_0': '90', 'lon_0': '0', " "'no_defs': 'None', 'proj': 'laea', 'type': 'crs', " "'units': 'm', 'x_0': '0', 'y_0': '0'}") else: projection = ("{'a': '6371228.0', 'lat_0': '90.0', " "'lon_0': '0.0', 'proj': 'laea', 'units': 'm'}") nh_str = """Area ID: ease_nh Description: Arctic EASE grid Projection ID: ease_nh Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format(projection) self.assertEqual(nh_str, ease_nh.__str__())
def test_area_parser_yaml(self): """Test YAML area parser.""" from pyresample import parse_area_file test_area_file = os.path.join(os.path.dirname(__file__), 'test_files', 'areas.yaml') test_areas = parse_area_file(test_area_file, 'ease_nh', 'ease_sh', 'test_meters', 'test_degrees', 'test_latlong') ease_nh, ease_sh, test_m, test_deg, test_latlong = test_areas from pyresample.utils import is_pyproj2 if is_pyproj2(): # pyproj 2.0+ adds some extra parameters projection = ("{'R': '6371228', 'lat_0': '-90', 'lon_0': '0', " "'no_defs': 'None', 'proj': 'laea', 'type': 'crs', " "'units': 'm', 'x_0': '0', 'y_0': '0'}") else: projection = ("{'a': '6371228.0', 'lat_0': '-90.0', " "'lon_0': '0.0', 'proj': 'laea', 'units': 'm'}") nh_str = """Area ID: ease_nh Description: Arctic EASE grid Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(ease_nh.__str__(), nh_str) sh_str = """Area ID: ease_sh Description: Antarctic EASE grid Projection: {} Number of columns: 425 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(ease_sh.__str__(), sh_str) m_str = """Area ID: test_meters Description: test_meters Projection: {} Number of columns: 850 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(test_m.__str__(), m_str) deg_str = """Area ID: test_degrees Description: test_degrees Projection: {} Number of columns: 850 Number of rows: 425 Area extent: (-5326849.0625, -5326849.0625, 5326849.0625, 5326849.0625)""".format( projection) self.assertEqual(test_deg.__str__(), deg_str) if is_pyproj2(): # pyproj 2.0+ adds some extra parameters projection = ("{'ellps': 'WGS84', 'lat_0': '27.12', " "'lon_0': '-81.36', 'proj': 'longlat', " "'type': 'crs'}") else: projection = ("{'ellps': 'WGS84', 'lat_0': '27.12', " "'lon_0': '-81.36', 'proj': 'longlat'}") latlong_str = """Area ID: test_latlong Description: Basic latlong grid Projection: {} Number of columns: 3473 Number of rows: 4058 Area extent: (-0.0812, 0.4039, 0.0812, 0.5428)""".format(projection) self.assertEqual(test_latlong.__str__(), latlong_str)
def is_latlong(self): if is_pyproj2(): return self.crs.is_geographic return super(BaseProj, self).is_latlong()
def is_latlong(self): if is_pyproj2(): return self.crs.is_geographic return super(BaseProj, self).is_latlong()