1.What is geodesy?
In the vast universe, the earth is our home on which we live. Since ancient times, humans have been fascinated by the location on the planet and their distance from each other. Whether it is navigational expeditions, trade exchanges or scientific research, it is crucial to accurately calculate the distance between two locations.Geodesy: The magical power of geodesicsGeodesy, also known as geodesy, is a discipline that studies the shape, size and gravity field of the earth. It plays a crucial role in the calculation of the Earth's distance. Geodesy's principle is based on spherical geometry. First, Geodesy approximates the earth as a smooth sphere. Then, based on the latitude and longitude coordinates, the two locations are regarded as two points on the sphere. Finally, use the spherical distance formula:
d = R * arccos(sin(φ1) * sin(φ2) + cos(φ1) * cos(φ2) * cos(λ1 - λ2))
Among them, R is the radius of the earth, φ1 and φ2 are the latitudes of the two places, λ1 and λ2 are the longitudes of the two places, and d is the distance between the two points. Through this formula, Geodesy can quickly and accurately calculate the distance between two latitude and longitude coordinates on the earth.
2. Code Engineering
Experimental objectives
- 1. Use mathematical formulas to calculate
- 2.Use the Java library package Geodesy
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="/POM/4.0.0" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."> <parent> <artifactId>springboot-demo</artifactId> <groupId></groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>geodesy</artifactId> <properties> <>8</> <>8</> </properties> <dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId></groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId></groupId> <artifactId>geodesy</artifactId> <version>1.1.3</version> </dependency> </dependencies> </project>
Mathematical formula calculation class
package ; import ; import ; /** * * * <p>formula:S=R·arccos[cosβ1·cosβ·2cos(α1-α2)+sinβ1·sinβ2] */ @UtilityClass public class MathDistanceUtil { private static final double EARTH_RADIUS = 6371393; private static final double DEGREES_TO_RADIANS = 0.017453292519943295; /** * Calculate according to formula * * @param longitude1 * @param latitude1 * @param longitude2 * @param latitude2 * @return */ public static double getDistance( Double longitude1, Double latitude1, Double longitude2, Double latitude2) { double radiansLongitude1 = toRadians(longitude1); double radiansLatitude1 = toRadians(latitude1); double radiansLongitude2 = toRadians(longitude2); double radiansLatitude2 = (latitude2); final double cos = ((radiansLatitude1)) .multiply(((radiansLatitude2))) .multiply( ( ( (radiansLongitude1) .subtract((radiansLongitude2)) .doubleValue()))) .add( ((radiansLatitude1)) .multiply(((radiansLatitude2)))) .doubleValue(); double acos = (cos); return (EARTH_RADIUS).multiply((acos)).doubleValue(); } /** * refer:{@link Math#toRadians(double)} * * @param value value * @return {double} */ private static double toRadians(double value) { return (value).multiply((DEGREES_TO_RADIANS)).doubleValue(); } }
Library package call
The underlying principle is also based on formula calculation, so it is convenient for everyone to use before packaging it
package ; import ; import ; import ; import ; import ; import ; public class GeodsyDistanceUtils { /** * * * @param lonA A longitude * @param latA A latitude * @param lonB B longitude * @param latB B latitude * @param newScale The result is kept to decimal places * @return distant (m) */ public static double getDistance(Double lonA, Double latA, Double lonB, Double latB,int newScale) { GlobalCoordinates source = new GlobalCoordinates(latA, lonA); GlobalCoordinates target = new GlobalCoordinates(latB, lonB); GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(, source, target); double distance = (); BigDecimal distanceBig = new BigDecimal(distance).setScale(newScale, ); return (); } }
The above are just some key codes. Please refer to the code repository below.
Code Repository
/Harries/springboot-demo
3. Test
Write test classes
@Test public void getDistance() { // source (113.324553,23.106414) // target (121.499718, 31.239703) double distance1 = (113.324553,23.106414, 121.499718, 31.239703,2); ("distant1(m):" + distance1); double distance2 = (113.324553, 23.106414, 121.499718, 31.239703); ("distant2(m):" + distance2); }
Run the unit test and found that the error of the two calculation methods is not large
distant1(m):1212316.48
distant2(m):1212391.2574948743
This is the end of this article about SpringBoot integrating geodesy to implement distance calculation function. For more related SpringBoot geodesy distance calculation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!