Name and CountryCode Attributes are not the problem

This commit is contained in:
Lennart Hensler
2012-01-17 14:20:37 +01:00
parent fc05cda6fa
commit 94fc9816df
7 changed files with 299 additions and 299 deletions
@@ -20,96 +20,96 @@ public interface Placemark extends Serializable {
* @return The 2 letters country code of the Placemark
*/
String getCountryCode();
/**
* @return The {@link Position} of the Placemark
*/
Position getPosition();
/**
* @return The population of the Placemark
*/
long getPopulation();
/**
* @param position The {@link Position} from which the distance is calculated
* @return The {@link Distance} between the given {@link Position} and the Placemark
*/
Distance distanceFrom(Position position);
/**
* Creates a new {@link Position} out of the given coordinates and calculates the distance between {@link Position} and
* the Placemark via {@link Placemark#distanceFrom(Position) distanceFrom(Position)}.
* @param latDeg Latitude in degrees
* @param lngDeg Longitude in degrees
* @return The {@link Distance} between the given coordinates and the Placemark
*/
Distance distanceFrom(double latDeg, double lngDeg);
/**
* @return The name of the country where the Placemark is located
*/
String getCountryName();
/**
* Sorts the Placemarks by Population from low to high.
* @author Lennart Hensler (D054527)
*
*/
public class ByPopulation implements Comparator<Placemark> {
@Override
public int compare(Placemark p1, Placemark p2) {
return p1.getPopulation() > p2.getPopulation() ? 1 : p1.getPopulation() < p2.getPopulation() ? -1 : 0;
}
}
/**
* Sorts the Placemarks by distance to a {@link Position} from near to far.<br /><br />
*
* If you use this for {@link ReverseGeocoder#getPlacemarkLast(Position, float, Comparator) getPlacemarkBest(Position position,...)} to
* get the nearest Placemark for <code>position</code> you can use {@link ReverseGeocoder#getPlacemarkNearest(Position) getPlacemark} instead.
* <code>getPlacemark</code> returns per default the nearest Placemark for a Position.
* @author Lennart Hensler (D054527)
*
*/
public class ByDistance implements Comparator<Placemark> {
private Position position;
public ByDistance(Position position) {
this.position = position;
}
@Override
public int compare(Placemark p1, Placemark p2) {
Distance d1 = p1.distanceFrom(position);
Distance d2 = p2.distanceFrom(position);
return d1.compareTo(d2);
}
}
public class ByPopulationDistanceRatio implements Comparator<Placemark> {
private Position position;
public ByPopulationDistanceRatio(Position position) {
this.position = position;
}
@Override
public int compare(Placemark p1, Placemark p2) {
double r1 = p1.getPopulation() / p1.distanceFrom(position).getKilometers();
double r2 = p2.getPopulation() / p2.distanceFrom(position).getKilometers();
return r1 > r2 ? 1 : r1 < r2 ? -1 : 0;
}
}
//
// /**
// * @return The {@link Position} of the Placemark
// */
// Position getPosition();
//
// /**
// * @return The population of the Placemark
// */
// long getPopulation();
//
// /**
// * @param position The {@link Position} from which the distance is calculated
// * @return The {@link Distance} between the given {@link Position} and the Placemark
// */
// Distance distanceFrom(Position position);
//
// /**
// * Creates a new {@link Position} out of the given coordinates and calculates the distance between {@link Position} and
// * the Placemark via {@link Placemark#distanceFrom(Position) distanceFrom(Position)}.
// * @param latDeg Latitude in degrees
// * @param lngDeg Longitude in degrees
// * @return The {@link Distance} between the given coordinates and the Placemark
// */
// Distance distanceFrom(double latDeg, double lngDeg);
//
// /**
// * @return The name of the country where the Placemark is located
// */
// String getCountryName();
//
//
//
//
//
//
// /**
// * Sorts the Placemarks by Population from low to high.
// * @author Lennart Hensler (D054527)
// *
// */
// public class ByPopulation implements Comparator<Placemark> {
// @Override
// public int compare(Placemark p1, Placemark p2) {
// return p1.getPopulation() > p2.getPopulation() ? 1 : p1.getPopulation() < p2.getPopulation() ? -1 : 0;
// }
// }
//
// /**
// * Sorts the Placemarks by distance to a {@link Position} from near to far.<br /><br />
// *
// * If you use this for {@link ReverseGeocoder#getPlacemarkLast(Position, float, Comparator) getPlacemarkBest(Position position,...)} to
// * get the nearest Placemark for <code>position</code> you can use {@link ReverseGeocoder#getPlacemarkNearest(Position) getPlacemark} instead.
// * <code>getPlacemark</code> returns per default the nearest Placemark for a Position.
// * @author Lennart Hensler (D054527)
// *
// */
// public class ByDistance implements Comparator<Placemark> {
//
// private Position position;
//
// public ByDistance(Position position) {
// this.position = position;
// }
//
// @Override
// public int compare(Placemark p1, Placemark p2) {
// Distance d1 = p1.distanceFrom(position);
// Distance d2 = p2.distanceFrom(position);
//
// return d1.compareTo(d2);
// }
// }
//
// public class ByPopulationDistanceRatio implements Comparator<Placemark> {
//
// private Position position;
//
// public ByPopulationDistanceRatio(Position position) {
// this.position = position;
// }
//
// @Override
// public int compare(Placemark p1, Placemark p2) {
// double r1 = p1.getPopulation() / p1.distanceFrom(position).getKilometers();
// double r2 = p2.getPopulation() / p2.distanceFrom(position).getKilometers();
//
// return r1 > r2 ? 1 : r1 < r2 ? -1 : 0;
// }
//
// }
}
@@ -19,27 +19,27 @@ public class PlacemarkImpl implements Placemark, Serializable {
private String name;
private String countryCode;
private SerializablePosition position;
private long population;
// private SerializablePosition position;
// private long population;
//
PlacemarkImpl() {}
/**
* Creates a new Placemark with the given parameters as attributes.
*/
public PlacemarkImpl(String name, String countryCode, SerializablePosition position, long population) {
public PlacemarkImpl(String name, String countryCode/*, SerializablePosition position, long population*/) {
this.name = name;
this.countryCode = countryCode;
this.position = position;
this.population = population;
// this.position = position;
// this.population = population;
}
/**
* Creates a new Placemark with the given parameters as attributes and a <code>population</code> of <code>0</code>;
*/
public PlacemarkImpl(String name, String countryCode, String countryName, SerializablePosition position, String type) {
this(name, countryCode, position, 0);
}
// /**
// * Creates a new Placemark with the given parameters as attributes and a <code>population</code> of <code>0</code>;
// */
// public PlacemarkImpl(String name, String countryCode, String countryName, SerializablePosition position, String type) {
// this(name, countryCode, position, 0);
// }
public String getName() {
return name;
@@ -48,80 +48,80 @@ public class PlacemarkImpl implements Placemark, Serializable {
public String getCountryCode() {
return countryCode;
}
public Position getPosition() {
return position.getPosition();
}
public long getPopulation() {
return population;
}
@Override
public Distance distanceFrom(Position position) {
return this.position.getPosition().getDistance(position);
}
@Override
public Distance distanceFrom(double latDeg, double lngDeg) {
Position p = new DegreePosition(latDeg, lngDeg);
return distanceFrom(p);
}
@Override
public String getCountryName() {
return CountryCodeFactory.INSTANCE.getFromTwoLetterISOName(countryCode).getName();
}
@Override
public String toString() {
StringBuilder b = new StringBuilder(Placemark.class.getSimpleName() + "[");
b.append(name + ", ");
b.append(countryCode + ", ");
b.append(position.toString() + ", ");
b.append(population + "]");
return b.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + (int) (population ^ (population >>> 32));
result = prime * result + ((position == null) ? 0 : position.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PlacemarkImpl other = (PlacemarkImpl) obj;
if (countryCode == null) {
if (other.countryCode != null)
return false;
} else if (!countryCode.equals(other.countryCode))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (population != other.population)
return false;
if (position == null) {
if (other.position != null)
return false;
} else if (!position.equals(other.position))
return false;
return true;
}
//
// public Position getPosition() {
// return position.getPosition();
// }
//
// public long getPopulation() {
// return population;
// }
//
//
// @Override
// public Distance distanceFrom(Position position) {
// return this.position.getPosition().getDistance(position);
// }
// @Override
// public Distance distanceFrom(double latDeg, double lngDeg) {
// Position p = new DegreePosition(latDeg, lngDeg);
// return distanceFrom(p);
// }
//
// @Override
// public String getCountryName() {
// return CountryCodeFactory.INSTANCE.getFromTwoLetterISOName(countryCode).getName();
// }
//
// @Override
// public String toString() {
// StringBuilder b = new StringBuilder(Placemark.class.getSimpleName() + "[");
// b.append(name + ", ");
// b.append(countryCode + ", ");
// b.append(position.toString() + ", ");
// b.append(population + "]");
//
// return b.toString();
// }
//
// @Override
// public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode());
// result = prime * result + ((name == null) ? 0 : name.hashCode());
// result = prime * result + (int) (population ^ (population >>> 32));
// result = prime * result + ((position == null) ? 0 : position.hashCode());
// return result;
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj)
// return true;
// if (obj == null)
// return false;
// if (getClass() != obj.getClass())
// return false;
// PlacemarkImpl other = (PlacemarkImpl) obj;
// if (countryCode == null) {
// if (other.countryCode != null)
// return false;
// } else if (!countryCode.equals(other.countryCode))
// return false;
// if (name == null) {
// if (other.name != null)
// return false;
// } else if (!name.equals(other.name))
// return false;
// if (population != other.population)
// return false;
// if (position == null) {
// if (other.position != null)
// return false;
// } else if (!position.equals(other.position))
// return false;
// return true;
// }
}
@@ -23,20 +23,20 @@ public class RacePlaceOrderImpl implements RacePlaceOrder, Serializable {
return Collections.unmodifiableCollection(places);
}
@Override
public String toString() {
StringBuilder b = new StringBuilder();
boolean first = true;
for (Placemark place : places) {
if (first) {
b.append(place.getCountryCode() + ", " + place.getName());
first = false;
} else {
b.append(" -> " + place.getCountryCode() + ", " + place.getName());
}
}
return b.toString();
}
// @Override
// public String toString() {
// StringBuilder b = new StringBuilder();
// boolean first = true;
// for (Placemark place : places) {
// if (first) {
// b.append(place.getCountryCode() + ", " + place.getName());
// first = false;
// } else {
// b.append(" -> " + place.getCountryCode() + ", " + place.getName());
// }
// }
// return b.toString();
// }
@Override
public int hashCode() {
@@ -965,62 +965,62 @@ public abstract class TrackedRaceImpl implements TrackedRace, CourseListener {
Placemark startBest = null;
Placemark finishBest = null;
//Get start postition
Waypoint start = getRace().getCourse().getFirstWaypoint();
Iterable<MarkPassing> startPassings = getMarkPassingsInOrder(start);
MarkPassing startPassing = startPassings.iterator().next();
Position startPosition = getApproximatePosition(start, startPassing.getTimePoint());
try {
//Get distance to nearest placemark and calculate the search radius
Placemark startNearest = ReverseGeocoder.INSTANCE.getPlacemarkNearest(startPosition);
Distance startNearestDistance = startNearest.distanceFrom(startPosition);
double startRadius = startNearestDistance.getKilometers() * GEONAMES_RADIUS_CACLCULATION_FACTOR;
//Get the estimated best start place
startBest = ReverseGeocoder.INSTANCE.getPlacemarkLast(startPosition, startRadius,
new Placemark.ByPopulationDistanceRatio(startPosition));
} catch (IOException e) {
logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
} catch (ParseException e) {
logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
}
//Get finish position
Waypoint finish = getRace().getCourse().getLastWaypoint();
Iterable<MarkPassing> finishPassings = getMarkPassingsInOrder(finish);
Iterator<MarkPassing> finishPassingsIterator = finishPassings.iterator();
MarkPassing finishPassing = null;
while (finishPassingsIterator.hasNext()) {
finishPassing = (MarkPassing) finishPassingsIterator.next();
}
Position finishPosition = getApproximatePosition(finish, finishPassing.getTimePoint());
if (startPosition.getDistance(finishPosition).getKilometers() > ReverseGeocoder.POSITION_CACHE_DISTANCE_LIMIT_IN_KM) {
try {
// Get distance to nearest placemark and calculate the search radius
Placemark finishNearest = ReverseGeocoder.INSTANCE.getPlacemarkNearest(finishPosition);
Distance finishNearestDistance = finishNearest.distanceFrom(finishPosition);
double finishRadius = finishNearestDistance.getKilometers() * GEONAMES_RADIUS_CACLCULATION_FACTOR;
// Get the estimated best finish place
finishBest = ReverseGeocoder.INSTANCE.getPlacemarkLast(finishPosition, finishRadius,
new Placemark.ByPopulationDistanceRatio(finishPosition));
} catch (IOException e) {
logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
} catch (ParseException e) {
logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
}
}
if (startBest != null) {
final List<Placemark> places = new ArrayList<Placemark>();
places.add(startBest);
if (!startBest.equals(finishBest)) {
places.add(finishBest);
}
order = new RacePlaceOrderImpl(places);
}
// //Get start postition
// Waypoint start = getRace().getCourse().getFirstWaypoint();
// Iterable<MarkPassing> startPassings = getMarkPassingsInOrder(start);
// MarkPassing startPassing = startPassings.iterator().next();
// Position startPosition = getApproximatePosition(start, startPassing.getTimePoint());
//
// try {
// //Get distance to nearest placemark and calculate the search radius
// Placemark startNearest = ReverseGeocoder.INSTANCE.getPlacemarkNearest(startPosition);
// Distance startNearestDistance = startNearest.distanceFrom(startPosition);
// double startRadius = startNearestDistance.getKilometers() * GEONAMES_RADIUS_CACLCULATION_FACTOR;
//
// //Get the estimated best start place
// startBest = ReverseGeocoder.INSTANCE.getPlacemarkLast(startPosition, startRadius,
// new Placemark.ByPopulationDistanceRatio(startPosition));
// } catch (IOException e) {
// logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
// } catch (ParseException e) {
// logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
// }
//
// //Get finish position
// Waypoint finish = getRace().getCourse().getLastWaypoint();
// Iterable<MarkPassing> finishPassings = getMarkPassingsInOrder(finish);
// Iterator<MarkPassing> finishPassingsIterator = finishPassings.iterator();
// MarkPassing finishPassing = null;
// while (finishPassingsIterator.hasNext()) {
// finishPassing = (MarkPassing) finishPassingsIterator.next();
// }
// Position finishPosition = getApproximatePosition(finish, finishPassing.getTimePoint());
//
// if (startPosition.getDistance(finishPosition).getKilometers() > ReverseGeocoder.POSITION_CACHE_DISTANCE_LIMIT_IN_KM) {
// try {
// // Get distance to nearest placemark and calculate the search radius
// Placemark finishNearest = ReverseGeocoder.INSTANCE.getPlacemarkNearest(finishPosition);
// Distance finishNearestDistance = finishNearest.distanceFrom(finishPosition);
// double finishRadius = finishNearestDistance.getKilometers() * GEONAMES_RADIUS_CACLCULATION_FACTOR;
//
// // Get the estimated best finish place
// finishBest = ReverseGeocoder.INSTANCE.getPlacemarkLast(finishPosition, finishRadius,
// new Placemark.ByPopulationDistanceRatio(finishPosition));
// } catch (IOException e) {
// logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
// } catch (ParseException e) {
// logger.throwing(TrackedRaceImpl.class.getName(), "getPlaceOrder()", e);
// }
// }
//
// if (startBest != null) {
// final List<Placemark> places = new ArrayList<Placemark>();
// places.add(startBest);
// if (!startBest.equals(finishBest)) {
// places.add(finishBest);
// }
// order = new RacePlaceOrderImpl(places);
// }
return order;
}
@@ -11,9 +11,9 @@ public class PlacemarkTest {
@Test
public void placemarkEqualsTest() {
Placemark p1 = new PlacemarkImpl("Kiel", "DE", "Germany", new SerializablePositionImpl(55, 10), "P");
Placemark p2 = new PlacemarkImpl("Kiel", "DE", "Germany", new SerializablePositionImpl(55, 10), "P");
Assert.assertEquals(p1, p2);
// Placemark p1 = new PlacemarkImpl("Kiel", "DE", "Germany", new SerializablePositionImpl(55, 10), "P");
// Placemark p2 = new PlacemarkImpl("Kiel", "DE", "Germany", new SerializablePositionImpl(55, 10), "P");
// Assert.assertEquals(p1, p2);
}
}
@@ -18,66 +18,66 @@ import com.sap.sailing.geocoding.ReverseGeocoder;
public class ReverseGeocoderTest {
private ReverseGeocoder geocoder = ReverseGeocoder.INSTANCE;
private static final Placemark KIEL = new PlacemarkImpl("Kiel", "DE", new SerializablePositionImpl(54.32132926107913, 10.1348876953125), 232758);
// private static final Placemark KIEL = new PlacemarkImpl("Kiel", "DE", new SerializablePositionImpl(54.32132926107913, 10.1348876953125), 232758);
private static final Position KIEL_POSITION = new DegreePosition(54.3231063453431, 10.12265682220459);
@Test
public void getPlacemarkSimpleTest() {
//Simple Test in Kiel center to check the connection and the parsing from JSONObject to Placemark
try {
Placemark kielReversed = geocoder.getPlacemarkNearest(KIEL_POSITION);
Assert.assertEquals(KIEL, kielReversed);
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParseException e) {
Assert.fail(e.getMessage());
}
// //Simple Test in Kiel center to check the connection and the parsing from JSONObject to Placemark
// try {
// Placemark kielReversed = geocoder.getPlacemarkNearest(KIEL_POSITION);
// Assert.assertEquals(KIEL, kielReversed);
// } catch (IOException e) {
// Assert.fail(e.getMessage());
// } catch (ParseException e) {
// Assert.fail(e.getMessage());
// }
}
@Test
public void getPlacemarkNearSimpleTest() {
try {
List<Placemark> placemarks = geocoder.getPlacemarksNear(KIEL_POSITION, 20);
Assert.assertFalse(placemarks.isEmpty());
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParseException e) {
Assert.fail(e.getMessage());
}
// try {
// List<Placemark> placemarks = geocoder.getPlacemarksNear(KIEL_POSITION, 20);
// Assert.assertFalse(placemarks.isEmpty());
// } catch (IOException e) {
// Assert.fail(e.getMessage());
// } catch (ParseException e) {
// Assert.fail(e.getMessage());
// }
}
@Test
public void getPlacemarkBestTest() {
Position abroad = new DegreePosition(54.43334, 10.299999);
Placemark firstByDistance = new PlacemarkImpl("Wendtorf", "DE", new SerializablePositionImpl(54.4166667, 10.3), 1139);
try {
Placemark p = geocoder.getPlacemarkLast(abroad, 20, new Placemark.ByPopulation());
Assert.assertEquals(KIEL, p);
p = geocoder.getPlacemarkFirst(abroad, 20, new Placemark.ByDistance(abroad));
Assert.assertEquals(firstByDistance, p);
p = geocoder.getPlacemarkLast(abroad, 20, new Placemark.ByPopulationDistanceRatio(abroad));
Assert.assertEquals(KIEL, p);
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParseException e) {
Assert.fail(e.getMessage());
}
// Position abroad = new DegreePosition(54.43334, 10.299999);
// Placemark firstByDistance = new PlacemarkImpl("Wendtorf", "DE", new SerializablePositionImpl(54.4166667, 10.3), 1139);
//
// try {
// Placemark p = geocoder.getPlacemarkLast(abroad, 20, new Placemark.ByPopulation());
// Assert.assertEquals(KIEL, p);
//
// p = geocoder.getPlacemarkFirst(abroad, 20, new Placemark.ByDistance(abroad));
// Assert.assertEquals(firstByDistance, p);
//
// p = geocoder.getPlacemarkLast(abroad, 20, new Placemark.ByPopulationDistanceRatio(abroad));
// Assert.assertEquals(KIEL, p);
// } catch (IOException e) {
// Assert.fail(e.getMessage());
// } catch (ParseException e) {
// Assert.fail(e.getMessage());
// }
}
@Test
public void getPlacemarkNearWithOffshorePosition() {
Position offshore = new DegreePosition(75.16330024622059, -0.087890625);
long radius = 300;
try {
List<Placemark> placemarks = geocoder.getPlacemarksNear(offshore, radius);
Assert.assertNull(placemarks);
} catch (IOException e) {
Assert.fail(e.getMessage());
} catch (ParseException e) {
Assert.fail(e.getMessage());
}
// Position offshore = new DegreePosition(75.16330024622059, -0.087890625);
// long radius = 300;
// try {
// List<Placemark> placemarks = geocoder.getPlacemarksNear(offshore, radius);
// Assert.assertNull(placemarks);
// } catch (IOException e) {
// Assert.fail(e.getMessage());
// } catch (ParseException e) {
// Assert.fail(e.getMessage());
// }
}
}
@@ -172,7 +172,7 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
long population = (Long) json.get("population");
if (name != null && lngDeg != null && latDeg != null) {
return new PlacemarkImpl(name, countryCode, position, population);
return new PlacemarkImpl(name, countryCode/*, position, population*/);
} else {
return null;
}
@@ -190,8 +190,8 @@ public class ReverseGeocoderImpl implements ReverseGeocoder {
* The results of the search
*/
private void cachePlacemarks(Position position, Double radius, List<Placemark> placemarks) {
Collections.sort(placemarks, new Placemark.ByDistance(position));
cache.put(position, new Triple<Position, Double, List<Placemark>>(position, radius, placemarks));
// Collections.sort(placemarks, new Placemark.ByDistance(position));
// cache.put(position, new Triple<Position, Double, List<Placemark>>(position, radius, placemarks));
}
/**