Transform coordinate columns already in memory#

Input

Call

Runs on

Ready result

NumPy

transform

Host

NumPy

CuPy

transform

CUDA

CuPy

NumPy

transform_device

CUDA, after staging inputs

CuPy, and the call waits

For transform, mixing NumPy and CuPy ordinates raises TypeError. Put all ordinates in the chosen input library before calling it.

Transform and inspect the rows#

Create longitude [3., 6.] and latitude [0., 45.] in your chosen array library. Use Transformer.from_crs(4326, 32631, always_xy=True), then call the selected method with longitude and latitude as x and y and request row statuses. A 3D pair also needs its z column. Unpack coordinates first, followed by statuses and then failures when requested.

Transform NumPy arrays#

import numpy as np

from cugeo.proj import RowStatus, Transformer

transformer = Transformer.from_crs(4326, 32631)
x, y, status = transformer.transform(
    np.array([3.0, 6.0]),
    np.array([0.0, 45.0]),
    return_status=True,
)

assert np.all(status == RowStatus.OK)
assert isinstance(x, np.ndarray) and isinstance(y, np.ndarray)
assert x.dtype == y.dtype == np.dtype("float64")
transformer.close()

Check against pyproj (test extra), with a one-micrometre limit in the projected CRS’s metres.

import pyproj
import sys

oracle = pyproj.Transformer.from_crs(4326, 32631, always_xy=True)
expected_x, expected_y = oracle.transform([3.0, 6.0], [0.0, 45.0])
assert np.all(np.hypot(x - expected_x, y - expected_y) <= 1e-6)
print(f"PROJ {pyproj.proj_version_str}: {oracle.definition}", file=sys.stderr)

The result stays in input order. The two OK rows lie near (500000, 0) and (736446.026101, 4987329.504699) metres. With masked host columns, a row must pass both the input mask and explicit validity.

Transform CuPy arrays#

import cupy as cp

from cugeo.proj import Transformer

transformer = Transformer.from_crs(4326, 32631)
longitude = cp.asarray([3.0, 6.0])
latitude = cp.asarray([0.0, 45.0])
easting, northing, status = transformer.transform(longitude, latitude, return_status=True)
assert isinstance(easting, cp.ndarray) and isinstance(northing, cp.ndarray)
assert easting.dtype == northing.dtype == cp.dtype("float64")
assert bool(cp.all(status == 0))
transformer.close()

Keep NumPy input on the GPU#

transform_device() copies NumPy input to CUDA, waits and releases the execution.

import numpy as np
import pyproj
import sys

device_easting, device_northing = Transformer.from_crs(
    4326, 32631
).transform_device(
    np.asarray([3.0, 6.0]),
    np.asarray([0.0, 45.0]),
)

Copy the CuPy result to the host explicitly to check it against pyproj.

oracle = pyproj.Transformer.from_crs(4326, 32631, always_xy=True)
expected_x, expected_y = oracle.transform([3.0, 6.0], [0.0, 45.0])
assert isinstance(device_easting, cp.ndarray) and isinstance(device_northing, cp.ndarray)
assert device_easting.dtype == device_northing.dtype == cp.dtype("float64")
assert np.all(np.hypot(cp.asnumpy(device_easting) - expected_x,
                       cp.asnumpy(device_northing) - expected_y) <= 1e-6)
assert np.all(np.hypot(cp.asnumpy(easting) - expected_x,
                       cp.asnumpy(northing) - expected_y) <= 1e-6)
print(f"PROJ {pyproj.proj_version_str}: {oracle.definition}", file=sys.stderr)

Schedule a consumer before completion#

Use a nondefault stream and retain the execution through completion.

stream = cp.cuda.Stream(non_blocking=True)
transformer = Transformer.from_crs(4326, 32631)

with stream:
    execution = transformer.enqueue_device(longitude, latitude, stream=stream)

easting, northing, status = execution.wait()
assert bool(cp.all(status == 0))
assert np.all(np.hypot(cp.asnumpy(easting) - expected_x,
                       cp.asnumpy(northing) - expected_y) <= 1e-6)
execution.close()
transformer.close()

Submission may copy inputs, pack validity, and allocate outputs or workspace. Request return_failures=True when you need row causes. To order another CUDA task against the transform, see Overlap transforms with other CUDA work.