Commit c73a6aa8 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Converted all type hints to Python 3.6 style. Dropped Python 3.5 support. Fixed code duplicate.


Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent 2e8cc832
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
"""Main module.""" """Main module."""
from typing import Any, Union, List, Tuple from typing import Union, List, Tuple, Optional
import os import os
import multiprocessing import multiprocessing
import sys import sys
...@@ -47,8 +47,12 @@ from pyresample.kd_tree import resample_nearest, resample_gauss, resample_custom ...@@ -47,8 +47,12 @@ from pyresample.kd_tree import resample_nearest, resample_gauss, resample_custom
class SensorMapGeometryTransformer(object): class SensorMapGeometryTransformer(object):
def __init__(self, lons, lats, resamp_alg='nearest', radius_of_influence=30, **opts): def __init__(self,
# type: (np.ndarray, np.ndarray, str, int, Any) -> None lons: np.ndarray,
lats: np.ndarray,
resamp_alg: str = 'nearest',
radius_of_influence: int = 30,
**opts) -> None:
"""Get an instance of SensorMapGeometryTransformer. """Get an instance of SensorMapGeometryTransformer.
:param lons: 2D longitude array corresponding to the 2D sensor geometry array :param lons: 2D longitude array corresponding to the 2D sensor geometry array
...@@ -110,9 +114,9 @@ class SensorMapGeometryTransformer(object): ...@@ -110,9 +114,9 @@ class SensorMapGeometryTransformer(object):
'+gamma=0 +k=1 +x_0=50692.579 +y_0=81723.458 +ellps=WGS84 ' \ '+gamma=0 +k=1 +x_0=50692.579 +y_0=81723.458 +ellps=WGS84 ' \
'+towgs84=0,0,0,0,0,0,0 +units=m +no_defs' '+towgs84=0,0,0,0,0,0,0 +units=m +no_defs'
self.area_extent_ll = [np.min(lons), np.min(lats), np.max(lons), np.max(lats)] self.area_extent_ll = [np.min(lons), np.min(lats), np.max(lons), np.max(lats)]
self.area_definition = None # type: AreaDefinition self.area_definition: Optional[AreaDefinition] = None
def _get_target_extent(self, tgt_epsg): def _get_target_extent(self, tgt_epsg: int):
if tgt_epsg == 4326: if tgt_epsg == 4326:
tgt_extent = self.area_extent_ll tgt_extent = self.area_extent_ll
else: else:
...@@ -121,16 +125,15 @@ class SensorMapGeometryTransformer(object): ...@@ -121,16 +125,15 @@ class SensorMapGeometryTransformer(object):
[self.lons[-1, 0], self.lats[-1, 0]], # LL_xy [self.lons[-1, 0], self.lats[-1, 0]], # LL_xy
[self.lons[-1, -1], self.lats[-1, -1]], # LR_xy [self.lons[-1, -1], self.lats[-1, -1]], # LR_xy
] ]
corner_coords_tgt_prj = [transform_any_prj(EPSG2WKT(4326), EPSG2WKT(tgt_epsg), x, y) tgt_extent = _corner_coords_lonlat_to_extent(corner_coords_ll, tgt_epsg)
for x, y in corner_coords_ll]
corner_coords_tgt_prj_np = np.array(corner_coords_tgt_prj)
x_coords, y_coords = corner_coords_tgt_prj_np[:, 0], corner_coords_tgt_prj_np[:, 1]
tgt_extent = [np.min(x_coords), np.min(y_coords), np.max(x_coords), np.max(y_coords)]
return tgt_extent return tgt_extent
def compute_areadefinition_sensor2map(self, data, tgt_prj, tgt_extent=None, tgt_res=None): def compute_areadefinition_sensor2map(self,
# type: (np.ndarray, Union[int, str], Tuple[float, float, float, float], Tuple[float, float]) -> AreaDefinition data: np.ndarray,
tgt_prj: Union[int, str],
tgt_extent: Tuple[float, float, float, float] = None,
tgt_res: Tuple[float, float] = None) -> AreaDefinition:
"""Compute the area_definition to resample a sensor geometry array to map geometry. """Compute the area_definition to resample a sensor geometry array to map geometry.
:param data: numpy array to be warped to sensor or map geometry :param data: numpy array to be warped to sensor or map geometry
...@@ -170,6 +173,7 @@ class SensorMapGeometryTransformer(object): ...@@ -170,6 +173,7 @@ class SensorMapGeometryTransformer(object):
# create VRT for X/Y coordinate array # create VRT for X/Y coordinate array
ds_xy_coords = gdal.Open(path_xycoords) ds_xy_coords = gdal.Open(path_xycoords)
drv_vrt = gdal.GetDriverByName("VRT") drv_vrt = gdal.GetDriverByName("VRT")
# noinspection PyUnusedLocal
vrt = raiseErr_if_empty(drv_vrt.CreateCopy(path_xycoords_vrt, ds_xy_coords)) vrt = raiseErr_if_empty(drv_vrt.CreateCopy(path_xycoords_vrt, ds_xy_coords))
del ds_xy_coords, vrt del ds_xy_coords, vrt
...@@ -238,8 +242,10 @@ class SensorMapGeometryTransformer(object): ...@@ -238,8 +242,10 @@ class SensorMapGeometryTransformer(object):
return area_definition return area_definition
def _resample(self, data, source_geo_def, target_geo_def): def _resample(self,
# type: (np.ndarray, Union[AreaDefinition, SwathDefinition], Union[AreaDefinition, SwathDefinition]) -> ... data: np.ndarray,
source_geo_def: Union[AreaDefinition, SwathDefinition],
target_geo_def: Union[AreaDefinition, SwathDefinition]) -> np.ndarray:
"""Run the resampling algorithm. """Run the resampling algorithm.
:param data: numpy array to be warped to sensor or map geometry :param data: numpy array to be warped to sensor or map geometry
...@@ -277,19 +283,22 @@ class SensorMapGeometryTransformer(object): ...@@ -277,19 +283,22 @@ class SensorMapGeometryTransformer(object):
else: else:
raise ValueError(self.resamp_alg) raise ValueError(self.resamp_alg)
return result # type: np.ndarray return result
@staticmethod @staticmethod
def _get_gt_prj_from_areadefinition(area_definition): def _get_gt_prj_from_areadefinition(area_definition: AreaDefinition) -> (Tuple[float, float, float,
# type: (AreaDefinition) -> (Tuple[float, float, float, float, float, float], str) float, float, float], str):
gt = area_definition.area_extent[0], area_definition.pixel_size_x, 0, \ gt = area_definition.area_extent[0], area_definition.pixel_size_x, 0, \
area_definition.area_extent[3], 0, -area_definition.pixel_size_y area_definition.area_extent[3], 0, -area_definition.pixel_size_y
prj = proj4_to_WKT(area_definition.proj_str) prj = proj4_to_WKT(area_definition.proj_str)
return gt, prj return gt, prj
def to_map_geometry(self, data, tgt_prj=None, tgt_extent=None, tgt_res=None, area_definition=None): def to_map_geometry(self, data: np.ndarray,
# type: (np.ndarray, Union[str, int], Tuple[float, float, float, float], Tuple, AreaDefinition) -> ... tgt_prj: Union[str, int] = None,
tgt_extent: Tuple[float, float, float, float] = None,
tgt_res: Tuple = None,
area_definition: AreaDefinition = None) -> Tuple[np.ndarray, tuple, str]:
"""Transform the input sensor geometry array into map geometry. """Transform the input sensor geometry array into map geometry.
:param data: numpy array (representing sensor geometry) to be warped to map geometry :param data: numpy array (representing sensor geometry) to be warped to map geometry
...@@ -331,10 +340,11 @@ class SensorMapGeometryTransformer(object): ...@@ -331,10 +340,11 @@ class SensorMapGeometryTransformer(object):
raise RuntimeError('The computed map geometry output has only one band instead of the expected %d bands.' raise RuntimeError('The computed map geometry output has only one band instead of the expected %d bands.'
% data.shape[2]) % data.shape[2])
return data_mapgeo, out_gt, out_prj # type: Tuple[np.ndarray, tuple, str] return data_mapgeo, out_gt, out_prj
def to_sensor_geometry(self, data, src_prj, src_extent): def to_sensor_geometry(self, data: np.ndarray,
# type: (np.ndarray, Union[str, int], List[float, float, float, float]) -> np.ndarray src_prj: Union[str, int],
src_extent: List[float, float, float, float]) -> np.ndarray:
"""Transform the input map geometry array into sensor geometry """Transform the input map geometry array into sensor geometry
:param data: numpy array (representing map geometry) to be warped to sensor geometry :param data: numpy array (representing map geometry) to be warped to sensor geometry
...@@ -381,9 +391,25 @@ def _initializer(lats, lons, data): ...@@ -381,9 +391,25 @@ def _initializer(lats, lons, data):
_global_shared_data = data _global_shared_data = data
def _corner_coords_lonlat_to_extent(corner_coords_ll: List, tgt_epsg: int):
corner_coords_tgt_prj = [transform_any_prj(EPSG2WKT(4326), EPSG2WKT(tgt_epsg), x, y)
for x, y in corner_coords_ll]
corner_coords_tgt_prj_np = np.array(corner_coords_tgt_prj)
x_coords = corner_coords_tgt_prj_np[:, 0]
y_coords = corner_coords_tgt_prj_np[:, 1]
tgt_extent = [np.min(x_coords), np.min(y_coords), np.max(x_coords), np.max(y_coords)]
return tgt_extent
class SensorMapGeometryTransformer3D(object): class SensorMapGeometryTransformer3D(object):
def __init__(self, lons, lats, resamp_alg='nearest', radius_of_influence=30, mp_alg='auto', **opts): def __init__(self,
# type: (np.ndarray, np.ndarray, str, int, str, Any) -> None lons: np.ndarray,
lats: np.ndarray,
resamp_alg: str = 'nearest',
radius_of_influence: int = 30,
mp_alg: str = 'auto',
**opts) -> None:
"""Get an instance of SensorMapGeometryTransformer. """Get an instance of SensorMapGeometryTransformer.
:param lons: 3D longitude array corresponding to the 3D sensor geometry array :param lons: 3D longitude array corresponding to the 3D sensor geometry array
...@@ -445,8 +471,7 @@ class SensorMapGeometryTransformer3D(object): ...@@ -445,8 +471,7 @@ class SensorMapGeometryTransformer3D(object):
self.mp_alg = 'tiles' self.mp_alg = 'tiles'
@staticmethod @staticmethod
def _to_map_geometry_2D(kwargs_dict): def _to_map_geometry_2D(kwargs_dict: dict) -> Tuple[np.ndarray, tuple, str, int]:
# type: (dict) -> Tuple[np.ndarray, tuple, str, int]
assert [var is not None for var in (_global_shared_lons, _global_shared_lats, _global_shared_data)] assert [var is not None for var in (_global_shared_lons, _global_shared_lats, _global_shared_data)]
SMGT2D = SensorMapGeometryTransformer(lons=_global_shared_lons[:, :, kwargs_dict['band_idx']], SMGT2D = SensorMapGeometryTransformer(lons=_global_shared_lons[:, :, kwargs_dict['band_idx']],
...@@ -467,16 +492,15 @@ class SensorMapGeometryTransformer3D(object): ...@@ -467,16 +492,15 @@ class SensorMapGeometryTransformer3D(object):
[self.lons[-1, 0, :].min(), self.lats[-1, 0, :].min()], # common LL_xy [self.lons[-1, 0, :].min(), self.lats[-1, 0, :].min()], # common LL_xy
[self.lons[-1, -1, :].max(), self.lats[-1, -1, :].min()], # common LR_xy [self.lons[-1, -1, :].max(), self.lats[-1, -1, :].min()], # common LR_xy
] ]
corner_coords_tgt_prj = [transform_any_prj(EPSG2WKT(4326), EPSG2WKT(tgt_epsg), x, y) tgt_extent = _corner_coords_lonlat_to_extent(corner_coords_ll, tgt_epsg)
for x, y in corner_coords_ll]
corner_coords_tgt_prj_np = np.array(corner_coords_tgt_prj)
x_coords, y_coords = corner_coords_tgt_prj_np[:, 0], corner_coords_tgt_prj_np[:, 1]
tgt_extent = [np.min(x_coords), np.min(y_coords), np.max(x_coords), np.max(y_coords)]
return tgt_extent return tgt_extent
def to_map_geometry(self, data, tgt_prj, tgt_extent=None, tgt_res=None): def to_map_geometry(self,
# type: (np.ndarray, Union[str, int], Tuple[float, float, float, float], Tuple) -> ... data: np.ndarray,
tgt_prj: Union[str, int],
tgt_extent: Tuple[float, float, float, float] = None,
tgt_res: Tuple = None) -> Tuple[np.ndarray, tuple, str]:
"""Transform the input sensor geometry array into map geometry. """Transform the input sensor geometry array into map geometry.
:param data: 3D numpy array (representing sensor geometry) to be warped to map geometry :param data: 3D numpy array (representing sensor geometry) to be warped to map geometry
...@@ -523,11 +547,10 @@ class SensorMapGeometryTransformer3D(object): ...@@ -523,11 +547,10 @@ class SensorMapGeometryTransformer3D(object):
out_gt = result[0][1] out_gt = result[0][1]
out_prj = result[0][2] out_prj = result[0][2]
return data_mapgeo, out_gt, out_prj # type: Tuple[np.ndarray, tuple, str] return data_mapgeo, out_gt, out_prj
@staticmethod @staticmethod
def _to_sensor_geometry_2D(kwargs_dict): def _to_sensor_geometry_2D(kwargs_dict: dict) -> (np.ndarray, int):
# type: (dict) -> (np.ndarray, int)
assert [var is not None for var in (_global_shared_lons, _global_shared_lats, _global_shared_data)] assert [var is not None for var in (_global_shared_lons, _global_shared_lats, _global_shared_data)]
SMGT2D = SensorMapGeometryTransformer(lons=_global_shared_lons[:, :, kwargs_dict['band_idx']], SMGT2D = SensorMapGeometryTransformer(lons=_global_shared_lons[:, :, kwargs_dict['band_idx']],
...@@ -541,8 +564,10 @@ class SensorMapGeometryTransformer3D(object): ...@@ -541,8 +564,10 @@ class SensorMapGeometryTransformer3D(object):
return data_sensorgeo, kwargs_dict['band_idx'] return data_sensorgeo, kwargs_dict['band_idx']
def to_sensor_geometry(self, data, src_prj, src_extent): def to_sensor_geometry(self,
# type: (np.ndarray, Union[str, int], List[float, float, float, float]) -> np.ndarray data: np.ndarray,
src_prj: Union[str, int],
src_extent: List[float, float, float, float]) -> np.ndarray:
"""Transform the input map geometry array into sensor geometry """Transform the input map geometry array into sensor geometry
:param data: 3D numpy array (representing map geometry) to be warped to sensor geometry :param data: 3D numpy array (representing map geometry) to be warped to sensor geometry
......
...@@ -53,7 +53,6 @@ setup( ...@@ -53,7 +53,6 @@ setup(
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Natural Language :: English', 'Natural Language :: English',
'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8', 'Programming Language :: Python :: 3.8',
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment