Javascript 计算Ramer-Douglas-Peucker公差的最佳方法
我正在使用Ramer-Douglas Peucker算法的一个实现来减少地图路线的点数。例如,如果我有500个以上的点,我希望在运行算法时使用一个公差,该公差将点计数减少到500以下,同时尽可能接近它。到目前为止,我尝试过的效率极低的方法如下:Javascript 计算Ramer-Douglas-Peucker公差的最佳方法,javascript,douglas-peucker,Javascript,Douglas Peucker,我正在使用Ramer-Douglas Peucker算法的一个实现来减少地图路线的点数。例如,如果我有500个以上的点,我希望在运行算法时使用一个公差,该公差将点计数减少到500以下,同时尽可能接近它。到目前为止,我尝试过的效率极低的方法如下: simp = coordSimplify(data.tableData, 0) while (simp.length > 400) { i += 0.0001; simp = coordSimplify(data.tableData
simp = coordSimplify(data.tableData, 0)
while (simp.length > 400) {
i += 0.0001;
simp = coordSimplify(data.tableData, i);
}
但我意识到这将大大减缓整个过程
我怎样才能使整个过程更有效率?我在考虑某种二进制切分算法,但我不确定每次如何计算上界和下界
TIA建议尝试以下方法,这是一种有效的二进制搜索,搜索
epsilon
值(使用术语at),该值位于simpTargetLo
和simpTargetHi
的目标点长度范围内
(注意,我没有测试过这个,因为我没有访问coordSimplify()
,所以可能会有一些语法错误,但逻辑应该是合理的。)
//设置可接受的结果。
设simpTargetLo=490;
设simpTargetHi=510;
//设置初始ε范围,该范围需要超出估计的解决方案。
设ε=0;
设ε=1;
设ε;
//计算初始低simp值和高simp值。
设simlo=coordSimplify(data.tableData,epsilonLo);
设simpHi=coordSimplify(data.tableData,epsilonHi);
让simpMid;
//确保初始simp值超出目标范围。
if(!(simpLo.length)你看过人们在github上的实现吗?这里和那里有一点-为什么?github
是有史以来最好的编程资源之一。我曾经找到一个完整的(!)“故障单系统”在那里,一家公司已经工作了一年多……完成并完善了它……然后在github上发布了它!你应该仔细寻找与你所想的类似的“现有技术”,因为“有人已经做过了”他们可能会把它放在github或sourceforge上。™我知道如何使用Github。
// Set the acceptable result.
let simpTargetLo = 490;
let simpTargetHi = 510;
// Set the initial epsilon range which needs to be outside the estimated solution.
let epsilonLo = 0;
let epsilonHi = 1;
let epsilonMid;
// Calculate the initial low and high simp values.
let simpLo = coordSimplify(data.tableData, epsilonLo);
let simpHi = coordSimplify(data.tableData, epsilonHi);
let simpMid;
// Ensure that the initial simp values fall outside the target range.
if ( !( simpLo.length <= simpTargetLo && simpTargetHi <= simpHi.length ) ) {
throw new Error( `Initial epsilon need expanding.\n epsilonLo ${epsilonLo} returns ${simpLo.length}\n epsilonHi ${epsilonHi} returns ${simpHi.length}` );
}
// Finally, let's ensure we don't get into an infinite loop in the event that
// their is no solution or the solution oscillates outside the target range.
let iterations = 0;
let maxIterations = 100;
do {
// Calculate the simp at the midpoint of the low and high epsilon.
epsilonMid = ( epsilonLo + epsilonHi ) / 2;
simpMid = coordSimplify(data.tableData, epsilonMid );
// Narrow the epsilon low and high range if the simp result is still outside
// both the target low and high.
if ( simpMid.length < simpTargetLo ) {
epsilonLo = epsilonMid;
} else if ( simpTargetHi < simpMid.length ) {
epsilonHi = epsilonMid;
} else {
// Otherwise, we have a solution!
break;
}
iterations++;
while( iterations < maxIterations );
if ( iterations < maxIterations ) {
console.log( `epsilon ${epsilonMid} returns ${simpMid.length}` );
} else {
console.log( `Unable to find solution.` );
}