using Proj
using DataFrames, CSV
This blog post is about geodetic coordinate transforms using the Julia
programming language.
In applied geophysics, the following question comes up frequently:
How can I transform geographical coordinates given as “latitude, longitude
” into some other coordinate system, e.g., UTM?
The Julia
package Proj.jl
offers all functionality that is required.
After installing the package using Julia
’s package manager from the REPL, we are ready to go:
Let’s assume that we have downloaded a set of coordinates from a handheld GPS receiver. The content of the data file coords.dat
may look like this:
# lat, lon
50.924833, 13.330562
50.982648, 13.530406
We first read in the data:
= DataFrame(CSV.File("coords.dat", delim=",")) df
2 rows × 2 columns
# lat | lon | |
---|---|---|
Float64 | Float64 | |
1 | 50.9248 | 13.3306 |
2 | 50.9826 | 13.5304 |
Next we arrange the data such that it is suitable for processing with Proj.jl
:
= Array(df) latlon
2×2 Matrix{Float64}:
50.9248 13.3306
50.9826 13.5304
The following step is essential. Since we transform data from one coordinate system into another, we have to inform Proj.jl
about the source and target systems. To this end, we exploit the convenient EPSG codes.
= Proj.Transformation("EPSG:4326", "EPSG:25833") trans
Transformation
source: WGS 84
target: ETRS89 / UTM zone 33N
direction: forward
The next lines will finally transform our GPS coordinates into UTM zone 33 coordinates which we refer to as easting and northing:
= [trans(latlon[i, :]) for i in 1:size(latlon, 1)]
etrs = hcat(collect.(etrs)...);
UTM
= UTM[1, :]
easting = UTM[2, :]
northing @show easting
@show northing;
easting = [382670.42457542894, 396843.0546304857]
northing = [5.642793241297958e6, 5.648923257065746e6]