Home > Java > javaTutorial > body text

How to Calculate Great Circle Distance and Generate Bounding Boxes from Latitude and Longitude Points in Java?

Linda Hamilton
Release: 2024-11-01 01:00:02
Original
362 people have browsed it

How to Calculate Great Circle Distance and Generate Bounding Boxes from Latitude and Longitude Points in Java?

Calculating Distance and Generating Bounding Boxes from Latitude Longitude Points in Java

Measuring distances between latitude and longitude coordinates, known as great circle distance, is essential for geospatial applications. Additionally, creating bounding boxes around points using distances enables visualization and analysis.

Great Circle Distance Calculation

The Haversine formula, a widely used method for calculating great circle distances, can be implemented in Java as follows:

<code class="java">public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
    double earthRadius = 3958.75; // miles (or 6371.0 kilometers)
    double dLat = Math.toRadians(lat2 - lat1);
    double dLng = Math.toRadians(lng2 - lng1);
    double sindLat = Math.sin(dLat / 2);
    double sindLng = Math.sin(dLng / 2);
    double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
            * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double dist = earthRadius * c;

    return dist;
}</code>
Copy after login

Generating Bounding Boxes

Once the distance is calculated, determining the points that represent the bounding box requires:

  1. Calculating North Point:

    • Add the provided distance to the latitude of the original point.
    • Maintain the same longitude.
  2. Calculating East Point:

    • Maintain the same latitude as the original point.
    • Add the provided distance to the longitude of the original point.

By combining the original point with the north and east points, a bounding box can be generated.

The above is the detailed content of How to Calculate Great Circle Distance and Generate Bounding Boxes from Latitude and Longitude Points in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!