Location/Coordinates and has initializers with correct order of lat,lon coordinatespull/1/head
parent
62ae9bbfda
commit
96c8119b50
@ -1,73 +0,0 @@
|
||||
/**
|
||||
* Coordinates.java
|
||||
* Copyright 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
* first published 04.10.2009 on http://yacy.net
|
||||
*
|
||||
* This file is part of YaCy Content Integration
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.geolocalization;
|
||||
|
||||
public class Coordinates {
|
||||
|
||||
private static final double tenmeter = 90.0d / 1.0e6d;
|
||||
|
||||
private final double lon, lat;
|
||||
|
||||
public Coordinates(double lon, double lat) {
|
||||
this.lon = lon;
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
public double lon() {
|
||||
return this.lon;
|
||||
}
|
||||
|
||||
public double lat() {
|
||||
return this.lat;
|
||||
}
|
||||
|
||||
private static final double bits30 = new Double(1L << 30).doubleValue(); // this is about one billion (US)
|
||||
private static final double upscale = bits30 / 360.0;
|
||||
|
||||
private static final int coord2int(double coord) {
|
||||
return (int) ((180.0 - coord) * upscale);
|
||||
}
|
||||
|
||||
/**
|
||||
* compute the hash code of a coordinate
|
||||
* this produces identical hash codes for locations that are close to each other
|
||||
*/
|
||||
public int hashCode() {
|
||||
return coord2int(this.lon) + (coord2int(this.lat) >> 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* equality test that is needed to use the class inside HashMap/HashSet
|
||||
*/
|
||||
public boolean equals(final Object o) {
|
||||
if (!(o instanceof Coordinates)) return false;
|
||||
Coordinates oo = (Coordinates) o;
|
||||
if (this.lon == oo.lon && this.lat == oo.lat) return true;
|
||||
// we access fuzzy values that are considered as equal if they are close to each other
|
||||
return Math.abs(this.lon - oo.lon) < tenmeter && Math.abs(this.lat - oo.lat) < tenmeter;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "[" + this.lon + "," + this.lat + "]";
|
||||
}
|
||||
}
|
@ -1,90 +1,93 @@
|
||||
/**
|
||||
* Location.java
|
||||
* Copyright 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
* first published 08.10.2009 on http://yacy.net
|
||||
*
|
||||
* This file is part of YaCy Content Integration
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.geolocalization;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
public class Location extends Coordinates implements Comparable<Location>, Comparator<Location> {
|
||||
|
||||
private String name;
|
||||
private int population;
|
||||
|
||||
public Location(float lon, float lat) {
|
||||
super(lon, lat);
|
||||
this.name = null;
|
||||
this.population = 0;
|
||||
}
|
||||
|
||||
public Location(float lon, float lat, String name) {
|
||||
super(lon, lat);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setPopulation(int population) {
|
||||
this.population = population;
|
||||
}
|
||||
|
||||
public int getPopulation() {
|
||||
return this.population;
|
||||
}
|
||||
|
||||
public boolean equals(Object loc) {
|
||||
if (!(loc instanceof Location)) return false;
|
||||
if (this.name == null || ((Location) loc).name == null) return super.equals(loc);
|
||||
return super.equals(loc) && this.name.toLowerCase().equals(((Location) loc).name.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* comparator that is needed to use the object inside TreeMap/TreeSet
|
||||
* a Location is smaller than another if it has a _greater_ population
|
||||
* this order is used to get sorted lists of locations where the first elements
|
||||
* have the greatest population
|
||||
*/
|
||||
public int compareTo(Location o) {
|
||||
if (this.equals(o)) return 0;
|
||||
long s = (ph(this.getPopulation()) << 30) + this.hashCode();
|
||||
long t = (ph(o.getPopulation()) << 30) + o.hashCode();
|
||||
if (s > t) return -1;
|
||||
if (s < t) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
private long ph(int population) {
|
||||
if (population > 10000) population -= 10000;
|
||||
return (long) population;
|
||||
}
|
||||
|
||||
public int compare(Location o1, Location o2) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* GeoLocation
|
||||
* Copyright 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
* first published 08.10.2009 on http://yacy.net
|
||||
*
|
||||
* This file is part of YaCy Content Integration
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.geolocalization;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
|
||||
public class GeoLocation extends GeoPoint implements Comparable<GeoLocation>, Comparator<GeoLocation> {
|
||||
|
||||
private String name;
|
||||
private int population;
|
||||
|
||||
public GeoLocation(double lat, double lon) {
|
||||
super(lat, lon);
|
||||
this.name = null;
|
||||
this.population = 0;
|
||||
}
|
||||
|
||||
public GeoLocation(double lat, double lon, String name) {
|
||||
super(lat, lon);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setPopulation(int population) {
|
||||
this.population = population;
|
||||
}
|
||||
|
||||
public int getPopulation() {
|
||||
return this.population;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object loc) {
|
||||
if (!(loc instanceof GeoLocation)) return false;
|
||||
if (this.name == null || ((GeoLocation) loc).name == null) return super.equals(loc);
|
||||
return super.equals(loc) && this.name.toLowerCase().equals(((GeoLocation) loc).name.toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* comparator that is needed to use the object inside TreeMap/TreeSet
|
||||
* a Location is smaller than another if it has a _greater_ population
|
||||
* this order is used to get sorted lists of locations where the first elements
|
||||
* have the greatest population
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(GeoLocation o) {
|
||||
if (this.equals(o)) return 0;
|
||||
long s = (ph(this.getPopulation()) << 30) + this.hashCode();
|
||||
long t = (ph(o.getPopulation()) << 30) + o.hashCode();
|
||||
if (s > t) return -1;
|
||||
if (s < t) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
private long ph(int population) {
|
||||
if (population > 10000) population -= 10000;
|
||||
return population;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(GeoLocation o1, GeoLocation o2) {
|
||||
return o1.compareTo(o2);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
/**
|
||||
* GeoPoint
|
||||
* Copyright 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
||||
* first published 08.10.2009 on http://yacy.net
|
||||
*
|
||||
* This file is part of YaCy Content Integration
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program in the file lgpl21.txt
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.yacy.document.geolocalization;
|
||||
|
||||
|
||||
public class GeoPoint {
|
||||
|
||||
public static final double meter = 90.0d / 1.0e7d; // this is actually the definition of 'meter': 10 million meter shall be the distance from the equator to the pole
|
||||
|
||||
private final long latlon; // using one variable for the coordinate pair saves some space
|
||||
|
||||
public GeoPoint(double lat, double lon) {
|
||||
this.latlon = (((long) coord2int(lat)) << 32) | (coord2int(lon));
|
||||
}
|
||||
|
||||
|
||||
public GeoPoint(int lat, int lon) {
|
||||
this.latlon = (((long) coord2int(lat / 1e6d)) << 32) | (coord2int(lon / 1e6d));
|
||||
}
|
||||
|
||||
public double lon() {
|
||||
return int2coord((int) (this.latlon & (Integer.MAX_VALUE)));
|
||||
}
|
||||
|
||||
public double lat() {
|
||||
return int2coord((int) (this.latlon >>> 32));
|
||||
}
|
||||
|
||||
private static final double maxint = new Double(Integer.MAX_VALUE).doubleValue();
|
||||
private static final double upscale = maxint / 360.0;
|
||||
|
||||
private static final int coord2int(double coord) {
|
||||
return (int) ((coord + 180.0) * upscale);
|
||||
}
|
||||
|
||||
private static final double int2coord(int z) {
|
||||
return (z / upscale) - 180.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* compute the hash code of a coordinate
|
||||
* this produces identical hash codes for locations that are close to each other
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return (int) ((this.latlon & Integer.MAX_VALUE) >> 1) + (int) (this.latlon >> 33);
|
||||
}
|
||||
|
||||
/**
|
||||
* equality test that is needed to use the class inside HashMap/HashSet
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (!(o instanceof GeoPoint)) return false;
|
||||
GeoPoint oo = (GeoPoint) o;
|
||||
return (this.latlon == oo.latlon);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + this.lat() + "," + this.lon() + "]";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
double lat = 13.419444d;
|
||||
double lon = 52.548611d;
|
||||
GeoPoint c = new GeoPoint(lat, lon);
|
||||
System.out.println(c.toString() + " #" + c.hashCode());
|
||||
System.out.println("error: lat: " + (Math.abs(c.lat() - lat) / meter) + " meter; lon: " + (Math.abs(c.lon() - lon) / meter) + " meter");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue