Skip to content
Snippets Groups Projects
Commit 1a0f91f9 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Reconsidered handling of duplicates, to allow closed paths.

parent 870103f1
No related branches found
No related tags found
No related merge requests found
......@@ -41,36 +41,34 @@ public class GeoLocations implements Iterable<LatLong> {
this.path = path;
}
public Comparator<? super LatLong> latLongComparator(LatLong latLong) {
public Comparator<? super LatLong> closestComparator(LatLong latLong) {
return (latLong1, latLong2) -> (int) Math.signum(latLong.distance(latLong1) - latLong.distance(latLong2));
}
public Collection<LatLong> findLocationsNearby(LatLong latLong, double distance) {
return locations.stream()
.filter(latLong2 -> latLong.distance(latLong2) <= distance)
.sorted(latLongComparator(latLong))
.filter(latLong2 -> distance == 0.0 ? latLong2.equals(latLong) : latLong.distance(latLong2) <= distance)
.sorted(closestComparator(latLong))
.collect(Collectors.toList());
}
public LatLong findNearestLocation(LatLong latLong) {
Optional<LatLong> min = locations.stream()
.min(latLongComparator(latLong));
.min(closestComparator(latLong));
return min.isPresent() ? min.get() : null;
}
//
public void addLocation(LatLong latLong) {
if (! locations.contains(latLong)) {
locations.add(latLong);
}
}
public void removeLocations(LatLong latLong, double distance) {
Iterator<LatLong> it = locations.iterator();
while (it.hasNext()) {
LatLong latLong2 = it.next();
if (latLong.distance(latLong2) <= distance) {
if (distance == 0.0 ? latLong2.equals(latLong) : latLong.distance(latLong2) <= distance) {
it.remove();
}
}
......
......@@ -10,10 +10,6 @@ public class LatLong {
this.longitude = longitude;
}
public double distance(LatLong loc) {
return distance(this.latitude, this.longitude, loc.latitude, loc.longitude);
}
public final static String SEPARATOR = ",";
@Override
......@@ -84,6 +80,9 @@ public class LatLong {
/*:: :*/
/*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
public static double distance(double lat1, double lon1, double lat2, double lon2) {
if (lon1 == lon2 && lat1 == lat2) {
return 0.0;
}
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
......@@ -99,6 +98,10 @@ public class LatLong {
return distance(latLong1.latitude, latLong1.longitude, latLong2.latitude, latLong2.longitude);
}
public double distance(LatLong latLong2) {
return distance(latitude, longitude, latLong2.latitude, latLong2.longitude);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
......
......@@ -20,7 +20,7 @@ public class GeoLocationsTest {
public void testGeoLocations() {
Assert.assertEquals(0, geoLocations.size());
Assert.assertEquals(1, new GeoLocations(new LatLong(0, 0)).size());
Assert.assertEquals(1, new GeoLocations(new LatLong(0, 0), new LatLong(0, 0)).size());
Assert.assertEquals(2, new GeoLocations(new LatLong(0, 0), new LatLong(0, 0)).size());
Assert.assertEquals(2, new GeoLocations(new LatLong(0, 0), new LatLong(1, 1)).size());
}
......@@ -46,11 +46,11 @@ public class GeoLocationsTest {
assertGeoLocations(latLong1);
geoLocations.addLocation(new LatLong(0, 0));
assertGeoLocations(latLong1);
assertGeoLocations(latLong1, latLong1);
LatLong latLong2 = new LatLong(1, 1);
geoLocations.addLocation(latLong2);
assertGeoLocations(latLong1, latLong2);
assertGeoLocations(latLong1, latLong1, latLong2);
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment