/* The MIT License (MIT) [OSI Approved License] The MIT License (MIT) Copyright (c) 2014 Daniel Glasson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package geocode; import geocode.kdtree.KDNodeComparator; import static java.lang.Math.cos; import static java.lang.Math.sin; import static java.lang.Math.toRadians; import java.util.Comparator; /** * Created by Daniel Glasson on 18/05/2014. * This class works with a placenames files from http://download.geonames.org/export/dump/ */ public class GeoPoint extends KDNodeComparator { public double latitude; public double longitude; public double point[] = new double[3]; // The 3D coordinates of the point public GeoPoint(Double latitude, Double longitude) { this.latitude = latitude; this.longitude = longitude; setPoint(); } private void setPoint() { point[0] = cos(toRadians(latitude)) * cos(toRadians(longitude)); point[1] = cos(toRadians(latitude)) * sin(toRadians(longitude)); point[2] = sin(toRadians(latitude)); } @Override protected double squaredDistance(GeoPoint other) { double x = this.point[0] - other.point[0]; double y = this.point[1] - other.point[1]; double z = this.point[2] - other.point[2]; return (x*x) + (y*y) + (z*z); } @Override protected double axisSquaredDistance(GeoPoint other, int axis) { double distance = point[axis] - other.point[axis]; return distance * distance; } @Override protected Comparator getComparator(int axis) { return GeoNameComparator.values()[axis]; } protected static enum GeoNameComparator implements Comparator { x { @Override public int compare(GeoPoint a, GeoPoint b) { return Double.compare(a.point[0], b.point[0]); } }, y { @Override public int compare(GeoPoint a, GeoPoint b) { return Double.compare(a.point[1], b.point[1]); } }, z { @Override public int compare(GeoPoint a, GeoPoint b) { return Double.compare(a.point[2], b.point[2]); } }; } }