|
| 1 | +import numpy as np |
| 2 | +from pyproj import CRS |
| 3 | +from rasters.transform_xy import transform_xy |
| 4 | + |
| 5 | +def test_transform_xy_geographic_to_projected(): |
| 6 | + # WGS84 geographic CRS |
| 7 | + source_crs = CRS.from_epsg(4326) |
| 8 | + # UTM zone 33N projected CRS |
| 9 | + target_crs = CRS.from_epsg(32633) |
| 10 | + # Example coordinates: longitude, latitude |
| 11 | + x = np.array([12.0, 13.0]) |
| 12 | + y = np.array([55.0, 56.0]) |
| 13 | + x_t, y_t = transform_xy(x, y, source_crs, target_crs) |
| 14 | + # Check output shape and type |
| 15 | + assert x_t.shape == x.shape |
| 16 | + assert y_t.shape == y.shape |
| 17 | + assert np.all(np.isfinite(x_t)) |
| 18 | + assert np.all(np.isfinite(y_t)) |
| 19 | + # Check that transformed coordinates are not equal to input |
| 20 | + assert not np.allclose(x, x_t) |
| 21 | + assert not np.allclose(y, y_t) |
| 22 | + |
| 23 | +def test_transform_xy_projected_to_geographic(): |
| 24 | + # UTM zone 33N projected CRS |
| 25 | + source_crs = CRS.from_epsg(32633) |
| 26 | + # WGS84 geographic CRS |
| 27 | + target_crs = CRS.from_epsg(4326) |
| 28 | + # Example coordinates: easting, northing |
| 29 | + x = np.array([500000, 600000]) |
| 30 | + y = np.array([6100000, 6200000]) |
| 31 | + x_t, y_t = transform_xy(x, y, source_crs, target_crs) |
| 32 | + # Check output shape and type |
| 33 | + assert x_t.shape == x.shape |
| 34 | + assert y_t.shape == y.shape |
| 35 | + assert np.all(np.isfinite(x_t)) |
| 36 | + assert np.all(np.isfinite(y_t)) |
| 37 | + # Check that transformed coordinates are not equal to input |
| 38 | + assert not np.allclose(x, x_t) |
| 39 | + assert not np.allclose(y, y_t) |
| 40 | + |
| 41 | +def test_transform_xy_clip_geographic(): |
| 42 | + # WGS84 geographic CRS |
| 43 | + source_crs = CRS.from_epsg(4326) |
| 44 | + target_crs = CRS.from_epsg(4326) |
| 45 | + # Coordinates outside valid bounds |
| 46 | + x = np.array([-200, 0, 200]) |
| 47 | + y = np.array([-100, 0, 100]) |
| 48 | + x_t, y_t = transform_xy(x, y, source_crs, target_crs) |
| 49 | + # Out-of-bounds should be nan |
| 50 | + assert np.isnan(x_t[0]) |
| 51 | + assert np.isnan(x_t[2]) |
| 52 | + assert np.isnan(y_t[0]) |
| 53 | + assert np.isnan(y_t[2]) |
| 54 | + # Valid should remain |
| 55 | + assert x_t[1] == 0 |
| 56 | + assert y_t[1] == 0 |
0 commit comments