This is probably more of a Julia question than a Proj4 question, but how can I perform coordinate transformations on multiple locations at once? I’m writing a function that would need to handle the following three cases:
1: A single location
The Proj4.jl readme provides clear guidance, and I’m able to get it working perfectly:
using Proj4
# Define the transformation:
trans = Proj4.Transformation("EPSG:4326", "+proj=utm +zone=32 +datum=WGS84");
# Define a single location:
lat = 55;
lon = 12;
# A single location works perfectly:
x, y = trans([lat,lon]);
println(x)
println(y)
>691875.632137542
>6.098907825129169e6
Perfect. Above, we’ve transformed (55N,12E) into projected coordinates in meters. Great, now try something similar for:
2: Multiple arbitrary locations:
Following a similar method to what’s described above, I’d like to transform four arbitrary geographic locations into projected coordinates. So here for lat
and lon
I enter four values, and try adding a .
in the call to trans()
:
# Four locations:
lat = [54, 55, 56, 57];
lon = [11, 12, 13, 14];
x, y = trans.([lat, lon]);
println(x)
println(y)
>[3.3824660011659563e6, 7.011981052605791e6, 56.0, 57.0]
>[827871.392810812, 1.217618384382686e6, 13.0, 14.0]
Oh no! The location (55N,12E) no longer returns the same projected coordinate values as we got when we only entered it as a single location. And the last two points are just returning the same values we fed into trans()
.
3: A grid of points
I can think of two ways I might transform a grid of points. One way might be to transpose the lon
variable:
# Four equally-spaced points in each dimension to creates a grid:
lat = [54, 55, 56, 57];
lon = [11, 12, 13, 14];
# Does transposing lon work? (Nope)
x, y = trans.([lat, lon']);
>[3.3824660011659563e6, 7.011981052605791e6, 56.0, 57.0]
>[827871.392810812, 1.217618384382686e6, 13.0, 14.0]
How about an array comprehension? Seems like this might be able to create a grid of transformed coordinates:
x, y = [trans([lati, loni]) for lati=lat, loni=lon]
print(x)
print(y)
> [631090.8762477529, 5.985372985435059e6][627928.1913765112, 6.096620706492608e6]
That’s only two locations!
So what’s going on here? How can I get Proj4
to transform a single point, an arbitrary array of points, and a grid?