001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.util;
017
018/**
019 *
020 */
021public class LengthUtils {
022  public static final int LAT_DEGREE_IN_METER = 110580;
023
024  /**
025   * Latitude degrees are roughly linear to meters on the earth spheroid.
026   * Just the longitudinal degree changes considerably with distance to the equator
027   * See https://en.wikipedia.org/wiki/Geographic_coordinate_system#Expressing_latitude_and_longitude_as_linear_units
028   *
029   * @return the decimal latitudinal degree equivalent to the given length in meters
030   */
031  public static double metersToLatDegree(double meter) {
032    // http://gis.stackexchange.com/questions/14449/java-vividsolutions-jts-wgs-84-distance-to-meters
033    return Math.round(meter/ LAT_DEGREE_IN_METER * 1000000.0) / 1000000.0;
034  }
035
036  /**
037   * Latitude degrees are roughly linear to meters on the earth spheroid.
038   * Just the longitudinal degree changes considerably with distance to the equator
039   * See https://en.wikipedia.org/wiki/Geographic_coordinate_system#Expressing_latitude_and_longitude_as_linear_units
040   *
041   * @return the length in meters equivalent to the given decimal latitudinal degree
042   */
043  public static double latDegreeToMeters(double latDegrees) {
044    // http://gis.stackexchange.com/questions/14449/java-vividsolutions-jts-wgs-84-distance-to-meters
045    return Math.round(latDegrees * LAT_DEGREE_IN_METER * 100.0) / 100.0;
046  }
047
048}