Background
image reference
Considering that the Earth is neither flat nor a perfect sphere, but an irregular ellipsoid, there is no perfect formula for quickly and accurately calculating the distance between two points at different longitudes and latitudes.
However, by using the geotools library, you can obtain mathematically corrected approximations quite easily.
Adding Dependencies
To use the Earth ellipsoid in geotools, you need to add the relevant library dependencies.
repositories {
maven { url "https://repo.osgeo.org/repository/release/" }
maven { url "https://download.osgeo.org/webdav/geotools/" }
mavenCentral()
}
dependencies {
...
implementation 'org.geotools:gt-referencing:26.2'
...
}
Writing Code
First, define the coordinates of Seoul and Busan as an enum class.
enum class City(val latitude: Double, val longitude: Double) {
SEOUL(37.5642135, 127.0016985),
BUSAN(35.1104, 129.0431);
}
Next, let's look at a simple usage example through a test code.
class EllipsoidTest {
@Test
internal fun createEllipsoid() {
val ellipsoid = DefaultEllipsoid.WGS84
val isSphere = ellipsoid.isSphere
val semiMajorAxis = ellipsoid.semiMajorAxis
val semiMinorAxis = ellipsoid.semiMinorAxis
val eccentricity = ellipsoid.eccentricity
val inverseFlattening = ellipsoid.inverseFlattening
val ivfDefinitive = ellipsoid.isIvfDefinitive
val orthodromicDistance = ellipsoid.orthodromicDistance(
City.SEOUL.longitude,
City.SEOUL.latitude,
City.BUSAN.longitude,
City.BUSAN.latitude
)
println("isSphere = $isSphere")
println("semiMajorAxis = $semiMajorAxis")
println("semiMinorAxis = $semiMinorAxis")
println("eccentricity = $eccentricity")
println("inverseFlattening = $inverseFlattening")
println("ivfDefinitive = $ivfDefinitive")
println("orthodromicDistance = $orthodromicDistance")
}
}
isSphere = false
semiMajorAxis = 6378137.0
semiMinorAxis = 6356752.314245179
eccentricity = 0.08181919084262128
inverseFlattening = 298.257223563
ivfDefinitive = true
orthodromicDistance = 328199.9794919944
You can create an Earth ellipsoid with DefaultEllipsoid.WGS84
. If you use SPHERE
instead of WGS84
, a sphere with a radius of 6371km will be created.
The distance result is in meters (m), so converting it to kilometers shows approximately 328km. If you search on Google, you may find 325km, so considering that there may be differences between the coordinates I chose and those chosen by Google, this is not a bad figure.
There are many other functions available as well. However, covering them all in this post would be too extensive, so if needed, I will cover them in another post.
The margin of error may not be satisfactory depending on business requirements, so before actual implementation, make sure to thoroughly test other methods in geotools.