Skip to content

Commit

Permalink
Use pyproj TransformDirection enum for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Aug 14, 2023
1 parent 046acf6 commit 93e8178
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pyresample/_spatial_mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import numpy as np
import pyproj
from pyproj import CRS
from pyproj.enums import TransformDirection

try:
import numexpr as ne
Expand Down Expand Up @@ -244,7 +245,8 @@ def _parallel_proj(scheduler, data1, data2, res1, res2, proj_args, proj_kwargs,
crs = CRS.from_user_input(proj_def)
gcrs = get_geodetic_crs_with_no_datum_shift(crs)
transformer = pyproj.Transformer.from_crs(gcrs, crs, always_xy=True)
trans_kwargs = {"radians": radians, "errcheck": errcheck, "direction": "INVERSE" if inverse else "FORWARD"}
trans_kwargs = {"radians": radians, "errcheck": errcheck,

Check warning on line 248 in pyresample/_spatial_mp.py

View check run for this annotation

Codecov / codecov/patch

pyresample/_spatial_mp.py#L248

Added line #L248 was not covered by tests
"direction": TransformDirection.INVERSE if inverse else TransformDirection.FORWARD}

# Reproject data segment
for s in scheduler:
Expand Down
5 changes: 3 additions & 2 deletions pyresample/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
da = None

from pyproj import CRS
from pyproj.enums import TransformDirection

logger = getLogger(__name__)
HashType = hashlib._hashlib.HASH
Expand Down Expand Up @@ -1276,7 +1277,7 @@ def _invproj(data_x, data_y, proj_wkt):
crs = CRS.from_wkt(proj_wkt)
gcrs = get_geodetic_crs_with_no_datum_shift(crs)
transformer = pyproj.Transformer.from_crs(gcrs, crs, always_xy=True)
lon, lat = transformer.transform(data_x, data_y, direction="INVERSE")
lon, lat = transformer.transform(data_x, data_y, direction=TransformDirection.INVERSE)
return np.stack([lon.astype(data_x.dtype), lat.astype(data_y.dtype)])


Expand Down Expand Up @@ -2528,7 +2529,7 @@ def get_lonlats(self, nprocs=None, data_slice=None, cache=False, dtype=None, chu
gcrs = get_geodetic_crs_with_no_datum_shift(self.crs)
target_trans = pyproj.Transformer.from_crs(gcrs, self.crs, always_xy=True)
target_proj = target_trans.transform
proj_kwargs["direction"] = "INVERSE"
proj_kwargs["direction"] = TransformDirection.INVERSE

# Get corresponding longitude and latitude values
lons, lats = target_proj(target_x, target_y, **proj_kwargs)
Expand Down
3 changes: 2 additions & 1 deletion pyresample/slicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import numpy as np
from pyproj import Transformer
from pyproj.enums import TransformDirection

from pyresample import AreaDefinition, SwathDefinition
from pyresample.geometry import (
Expand Down Expand Up @@ -154,7 +155,7 @@ def get_polygon_to_contain(self):
x, y = self.area_to_contain.get_edge_bbox_in_projection_coordinates(frequency=10)
if self.area_to_crop.is_geostationary:
x_geos, y_geos = get_geostationary_bounding_box_in_proj_coords(self.area_to_crop, 360)
x_geos, y_geos = self._transformer.transform(x_geos, y_geos, direction='INVERSE')
x_geos, y_geos = self._transformer.transform(x_geos, y_geos, direction=TransformDirection.INVERSE)
geos_poly = Polygon(zip(x_geos, y_geos))
poly = Polygon(zip(x, y))
poly = poly.intersection(geos_poly)
Expand Down

0 comments on commit 93e8178

Please sign in to comment.