Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Changed LocationItem.distanceFrom method signature to take a Location…
Browse files Browse the repository at this point in the history
…Item, not PointType
  • Loading branch information
watou committed Nov 15, 2015
1 parent ef8c4d5 commit dca3dc1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public void testDistance() {
LocationItem locationBerlin = new LocationItem("berlin");
locationBerlin.setState(pointBerlin);

DecimalType distance = locationParis.distanceFrom(pointParis);
DecimalType distance = locationParis.distanceFrom(locationParis);
assertEquals(distance.intValue(),0);

double parisBerlin = locationParis.distanceFrom(pointBerlin).doubleValue();
double parisBerlin = locationParis.distanceFrom(locationBerlin).doubleValue();
assertEquals(parisBerlin,878400,50);

double gravParis = pointParis.getGravity().doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,16 @@ public List<Class<? extends Command>> getAcceptedCommandTypes() {
}

/**
* Compute the distance with another Point type,
* http://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java
* Return the distance from another LocationItem.
* @return distance between the two points in meters
*/
public DecimalType distanceFrom(PointType away){

double dist = -1;

if ((away != null) && (this.state instanceof PointType)) {

PointType me = (PointType) this.state;

double dLat = Math.pow(Math.sin(Math.toRadians(away.getLatitude().doubleValue() - me.getLatitude().doubleValue()) / 2),2);
double dLng = Math.pow(Math.sin(Math.toRadians(away.getLongitude().doubleValue() - me.getLongitude().doubleValue()) / 2),2);
double a = dLat + Math.cos(Math.toRadians(me.getLatitude().doubleValue()))
* Math.cos(Math.toRadians(away.getLatitude().doubleValue())) * dLng;

dist = PointType.WGS84_a * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
public DecimalType distanceFrom(LocationItem awayItem) {
if (awayItem != null && awayItem.state instanceof PointType && this.state instanceof PointType) {
PointType thisPoint = (PointType)this.state;
PointType awayPoint = (PointType)awayItem.state;
return thisPoint.distanceFrom(awayPoint);
}

return new DecimalType(dist);
return new DecimalType(-1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public PointType(String value) {
if (StringUtils.isNotBlank(value)) {
String[] elements = value.split(",");
if (elements.length >= 2) {
canonicalize(new DecimalType(elements[0]), new DecimalType(
elements[1]));
canonicalize(new DecimalType(elements[0]), new DecimalType(elements[1]));
if (elements.length == 3) {
setAltitude(new DecimalType(elements[2]));
}
Expand Down Expand Up @@ -127,8 +126,8 @@ public DecimalType getGravity() {
* @see <a href="https://en.wikipedia.org/wiki/Haversine_formula">Haversine formula</a>
*/
public DecimalType distanceFrom(PointType otherPoint) {
double dLong = Math.toRadians(otherPoint.longitude.doubleValue() - this.longitude.doubleValue());
double dLat = Math.toRadians(otherPoint.latitude.doubleValue() - this.latitude.doubleValue());
double dLong = Math.toRadians(otherPoint.longitude.doubleValue() - this.longitude.doubleValue());
double a = Math.pow(Math.sin(dLat / 2D), 2D) + Math.cos(Math.toRadians(this.latitude.doubleValue()))
* Math.cos(Math.toRadians(otherPoint.latitude.doubleValue()))
* Math.pow(Math.sin(dLong / 2D), 2D);
Expand Down

0 comments on commit dca3dc1

Please sign in to comment.