package com.sv.service.api.util;


import com.github.davidmoten.geo.GeoHash;
import java.math.BigDecimal;
import java.util.List;

public class GeoHashUtils {
    private static final double EARTH_RADIUS = 6378.13720703125D;

    public GeoHashUtils() {
    }

    public static final String encodeHash(BigDecimal longitude, BigDecimal latitude) {
        return GeoHash.encodeHash(latitude.doubleValue(), longitude.doubleValue());
    }

    public static final String encodeHash(BigDecimal longitude, BigDecimal latitude, Integer length) {
        return GeoHash.encodeHash(latitude.doubleValue(), longitude.doubleValue(), length.intValue());
    }

    public static final String encodeHash(double longitude, double latitude) {
        return GeoHash.encodeHash(latitude, longitude);
    }

    public static final String encodeHash(double longitude, double latitude, Integer length) {
        return GeoHash.encodeHash(latitude, longitude, length.intValue());
    }

    public static final String encodeHash(String longitude, String latitude) {
        return GeoHash.encodeHash(Double.valueOf(latitude).doubleValue(), Double.valueOf(longitude).doubleValue());
    }

    public static final String encodeHash(String longitude, String latitude, Integer length) {
        return GeoHash.encodeHash(Double.valueOf(latitude).doubleValue(), Double.valueOf(longitude).doubleValue(), length.intValue());
    }

    public static final List<String> neighbours(String hash) {
        return GeoHash.neighbours(hash);
    }

    public static BigDecimal distance(double fromLatitude, double fromLongitude, double toLatitude, double toLongitude) {
        double x1 = Math.cos(fromLatitude) * Math.cos(fromLongitude);
        double y1 = Math.cos(fromLatitude) * Math.sin(fromLongitude);
        double z1 = Math.sin(fromLatitude);
        double x2 = Math.cos(toLatitude) * Math.cos(toLongitude);
        double y2 = Math.cos(toLatitude) * Math.sin(toLongitude);
        double z2 = Math.sin(toLatitude);
        double lineDistance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
        double realDistance = 40075.017986394196D * Math.asin(0.5D * lineDistance) / 180.0D;
        return (new BigDecimal(realDistance + "")).setScale(1, 4);
    }
}

