2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.lucene.spatial.geometry;
21 * Enum representing difference distance units, currently only kilometers and
24 public enum DistanceUnits {
26 MILES("miles", 3959, 24902),
27 KILOMETERS("km", 6371, 40076);
29 private static final double MILES_KILOMETRES_RATIO = 1.609344;
31 private final String unit;
33 private final double earthCircumference;
35 private final double earthRadius;
38 * Creates a new DistanceUnit that represents the given unit
40 * @param unit Distance unit in String form
41 * @param earthRadius Radius of the Earth in the specific distance unit
42 * @param earthCircumfence Circumference of the Earth in the specific distance unit
44 DistanceUnits(String unit, double earthRadius, double earthCircumfence) {
46 this.earthCircumference = earthCircumfence;
47 this.earthRadius = earthRadius;
51 * Returns the DistanceUnit which represents the given unit
53 * @param unit Unit whose DistanceUnit should be found
54 * @return DistanceUnit representing the unit
55 * @throws IllegalArgumentException if no DistanceUnit which represents the given unit is found
57 public static DistanceUnits findDistanceUnit(String unit) {
58 if (MILES.getUnit().equalsIgnoreCase(unit) || unit.equalsIgnoreCase("mi")) {
62 if (KILOMETERS.getUnit().equalsIgnoreCase(unit)) {
66 throw new IllegalArgumentException("Unknown distance unit " + unit);
70 * Converts the given distance in given DistanceUnit, to a distance in the unit represented by {@code this}
72 * @param distance Distance to convert
73 * @param from Unit to convert the distance from
74 * @return Given distance converted to the distance in the given unit
76 public double convert(double distance, DistanceUnits from) {
80 return (this == MILES) ? distance / MILES_KILOMETRES_RATIO : distance * MILES_KILOMETRES_RATIO;
84 * Returns the string representation of the distance unit
86 * @return String representation of the distance unit
88 public String getUnit() {
93 * Returns the <a href="http://en.wikipedia.org/wiki/Earth_radius">average earth radius</a>
95 * @return the average earth radius
97 public double earthRadius() {
102 * Returns the <a href="http://www.lyberty.com/encyc/articles/earth.html">circumference of the Earth</a>
104 * @return the circumference of the Earth
106 public double earthCircumference() {
107 return earthCircumference;