Проблема Simplify-Java (от hgoebl) со списком сокращенных баллов всегда размером 2
Я пытаюсь реализовать алгоритм сокращения с https://github.com/hgoebl/simplify-java
Я просмотрел его тестовый код и попытался придумать правильную логику.
Я беру список Location
объекты, превращая их в Point
, запустив алгоритм сокращения, затем преобразовав уменьшенные точки обратно в список Location
объекты.
Проблема здесь:
float[][] simplified = simplify.simplify(points2D, 10000.0f, true);
Это всегда получается с размером 2. Очевидно, я делаю что-то не так, но я не уверен, что. Можете ли вы определить, что не так с моей реализацией?
Подход № 1 терпит неудачу
public static ArrayList<Location> reducePath(List<Location> allLocations, double tolerance)
{
// All the points in rows, with columns latitude and longitude
float[][] points2D = new float[allLocations.size()][2];
// Convert Location to Point
int i = 0;
for (Location loc:allLocations)
{
points2D[i][0] = (float)loc.getLatitude();
points2D[i][1] = (float)loc.getLongitude();
i++;
}
PointExtractor<float[]> pointExtractor = new PointExtractor<float[]>()
{
@Override
public double getX(float[] point)
{
return point[0];
}
@Override
public double getY(float[] point)
{
return point[1];
}
};
Timber.tag("Thin").d("2D array size " + points2D.length);
// This is required for the Simplify initalization
// An empty array is explicity required by the Simplify library
Simplify<float[]> simplify = new Simplify<float[]>(new float[0][0], pointExtractor);
float[][] simplified = simplify.simplify(points2D, 10000.0f, true);
Timber.tag("Thin").d("Simplified with size " + simplified.length);
ArrayList<Location> reducedPoints = new ArrayList<>();
// Convert points back to location
for(float[] point:simplified)
{
Location loc = new Location("");
loc.setLatitude(point[0]);
loc.setLongitude(point[1]);
reducedPoints.add(loc);
}
return reducedPoints;
}
Подход № 2 также терпит неудачу, я также попробовал этот подход:
public static ArrayList<Location> reducePath(List<Location> allLocations, double tolerance)
{
// All the points in rows, with columns latitude and longitude
float[][] points2D = new float[allLocations.size()][2];
// This is required for the Simplify initalization
// An empty array is explicity required by the Simplify library
Simplify<MyPoint> simplify = new Simplify<MyPoint>(new MyPoint[0]);
MyPoint[] allpoints = new MyPoint[allLocations.size()];
// Convert Location to Point
int i = 0;
for (Location loc:allLocations)
{
points2D[i][0] = (float)loc.getLatitude();
points2D[i][1] = (float)loc.getLongitude();
MyPoint p = new MyPoint(loc.getLatitude(), (float)loc.getLongitude());
allpoints[i] = p;
i++;
}
Timber.tag("Thin").d("All points array size " + allpoints.length);
MyPoint[] simplified = simplify.simplify(allpoints, 1.0, false);
Timber.tag("Thin").d("Simplified with size " + simplified.length);
ArrayList<Location> reducedPoints = new ArrayList<>();
// Convert points back to location
for(MyPoint point:simplified)
{
Location loc = new Location("");
loc.setLatitude(point.getX());
loc.setLongitude(point.getY());
reducedPoints.add(loc);
}
return reducedPoints;
}
private static class MyPoint implements Point
{
double x;
double y;
private MyPoint(double x, double y)
{
this.x = x;
this.y = y;
}
@Override
public double getX()
{
return x;
}
@Override
public double getY()
{
return y;
}
@Override
public String toString()
{
return "{" + "x=" + x + ", y=" + y + '}';
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyPoint myPoint = (MyPoint) o;
if (Double.compare(myPoint.x, x) != 0) return false;
if (Double.compare(myPoint.y, y) != 0) return false;
return true;
}
}
Оба из этих гипер уменьшают мои очки до первой и последней локации.
Любые советы высоко ценится.
РЕШЕНИЕ
Благодаря совету у меня есть решение, которое теперь работает отлично. Вот окончательный код:
/**
* For use when making LatLng coordiantes whole intergers
* So the comparator can use values >1.
*/
private static int DELTA_SCALAR = 1000000;
/**
* Is used as the threshold for deciding what points are
* removed when using the path reduction. This value
* was found from running many tests and deciding on the
* best value that worked for GPS Paths.
*/
private static float EPSILON_TOLERANCE = 400.0f;
/**
* Reduces number of points while maintaining the path.
* @param allLocations
* @return ArrayList of all important locations
*/
public static ArrayList<Location> reducePath(ArrayList<Location> allLocations)
{
// The values must correspond to a delta > 1. So the scalar brings up the
// decimal values of LatLng positions to be whole numbers. The point extractor
// is used by the Simplify framework to get the X and Y values.
PointExtractor<Location> pointExtractor = new PointExtractor<Location>()
{
@Override
public double getX(Location location)
{
return location.getLatitude() * DELTA_SCALAR;
}
@Override
public double getY(Location location)
{
return location.getLongitude() * DELTA_SCALAR;
}
};
// This is required for the Simplify initalization
// An empty array is explicity required by the Simplify library
Simplify<Location> simplify = new Simplify<Location>(new Location[0], pointExtractor);
Location[] allLocationsArray = new Location[allLocations.size()];
allLocations.toArray(allLocationsArray);
Location[] simplifiedArray = simplify.simplify(allLocationsArray, EPSILON_TOLERANCE, true);
return new ArrayList<Location>(Arrays.asList(simplifiedArray));
}
1 ответ
В документации о simpify-java должно быть упомянуто, что упрощение работает только с координатами x, y (и z), имеющие дельта-значения больше 1.
Типичные координаты GPS - что-то вроде 47.998554556, а следующая точка имеет 47.998554599. Разница намного меньше 1, и, следовательно, квадрат равен 0. При таких значениях алгоритм и допуск не могут работать. В результате все точки между первым и последним устраняются.
Я обновил README.
Центральная точка решения заключается в смещении значений широты и долготы в диапазон чисел, чтобы упрощение могло работать эффективно. Наилучшим вариантом может быть предоставление PointExtractor:
private static PointExtractor<LatLng> latLngPointExtractor =
new PointExtractor<LatLng>() {
@Override
public double getX(LatLng point) {
return point.getLat() * 1000000;
}
@Override
public double getY(LatLng point) {
return point.getLng() * 1000000;
}
};
В случае значений Lat/Lon, вы должны поэкспериментировать с tolerance
значение. При преобразовании Lat/Lon путем умножения на 1e6
(часто встречается как lat6, lng6 afterwords), я испытал лучшие значения допусков в диапазоне от 5 до 50 (YMMV).
Вы можете найти более подробную информацию в README, а также есть TestCase с запущенным кодом. Надеюсь, это поможет!