Skip to content

Commit

Permalink
Merge pull request #467 from WilhelmusLab/feat-julia-latlon
Browse files Browse the repository at this point in the history
Fixing error in latlon.py
  • Loading branch information
danielmwatkins authored Oct 29, 2024
2 parents 754a583 + 24ed0cd commit c14b297
Show file tree
Hide file tree
Showing 7 changed files with 2,258 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/latlon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ def getlatlon(imgpath):
im = rasterio.open(imgpath)
crs = im.crs.__str__() # Coordinate reference system code
nrows, ncols = im.shape
rows, cols = np.meshgrid(np.arange(nrows), np.arange(ncols))
cols, rows = np.meshgrid(np.arange(ncols), np.arange(nrows))
xs, ys = rasterio.transform.xy(im.transform, rows, cols)
xs = np.array(xs)
ys = np.array(ys)

# X and Y are the 1D index vectors
X = np.array(xs)[:, 0]
Y = np.array(ys)[0, :]
X = xs[0, :]
Y = ys[:, 0]
ps_to_ll = Transformer.from_crs(im.crs, "WGS84", always_xy=True)
lons, lats = ps_to_ll.transform(np.ravel(xs), np.ravel(ys))

# longitude and latitude are 2D index arrays
longitude = np.reshape(lons, (nrows, ncols))
latitude = np.reshape(lats, (nrows, ncols))
Expand Down
27 changes: 27 additions & 0 deletions test/test-latlon.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using IceFloeTracker: getlatlon

@testset "getlatlon" begin
imgpth = "test_inputs/latlon/latlon_test_image-2020-06-21T00_00_00Z.tif"

# vec needed to convert to vector instead of nx1 matrix
expected_X = vec(readdlm("test_inputs/latlon/X.csv", ',', Float64))
expected_Y = vec(readdlm("test_inputs/latlon/Y.csv", ',', Float64))

expected_lat = readdlm("test_inputs/latlon/latitude.csv", ',', Float64)
expected_lon = readdlm("test_inputs/latlon/longitude.csv", ',', Float64)

latlon = getlatlon(imgpth)
X = latlon["X"]
Y = latlon["Y"]
lat = latlon["latitude"]
lon = latlon["longitude"]

# round to 4 decimal places to avoid weirdness in different arch/os
expected_lat, expected_lon, expected_X, expected_Y = [round.(arr, digits=4) for arr in [expected_lat, expected_lon, expected_X, expected_Y]]
lat, lon, X, Y = [round.(arr, digits=4) for arr in [lat, lon, X, Y]]

@test expected_X == X
@test expected_Y == Y
@test expected_lat == lat
@test expected_lon == lon
end
Loading

0 comments on commit c14b297

Please sign in to comment.