All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.powsybl.iidm.geodata.utils.DistanceCalculator Maven / Gradle / Ivy

/**
 * Copyright (c) 2020, RTE (http://www.rte-france.com)
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * SPDX-License-Identifier: MPL-2.0
 */
package com.powsybl.iidm.geodata.utils;

import com.powsybl.iidm.network.extensions.Coordinate;

/**
 * @author Chamseddine Benhamed {@literal }
 */
public final class DistanceCalculator {

    private DistanceCalculator() {

    }

    public static double distance(Coordinate coord1, Coordinate coord2) {
        return DistanceCalculator.distance(coord1.getLatitude(), coord1.getLongitude(), coord2.getLatitude(), coord2.getLongitude());
    }

    /**
     * Compute an approximate distance in meters between two geographical points (latitude, longitude in degrees).
     * The computation assumes that the earth is spherical and its radius is equal to 6378137 meters.
     * @param the lat and lon of the two points in degrees
     * @return the approximate distance in meters
     */
    public static double distance(double lat1, double lon1, double lat2, double lon2) {
        // source : https://geodesie.ign.fr/contenu/fichiers/Distance_longitude_latitude.pdf
        if (lat1 == lat2 && lon1 == lon2) {
            return 0;
        } else {
            double dL = lon1 - lon2;
            double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(dL));
            dist = Math.acos(dist);
            //6 378 137 is the conventional earth radius
            return dist * 6_378_137;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy