微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算 Ramer-Douglas-Peucker 容差的最佳方法

如何解决计算 Ramer-Douglas-Peucker 容差的最佳方法

我正在使用 Ramer Douglas Peucker 算法的实现来减少地图路线的点数。例如,如果我有超过 500 个点,我想以一个容差运行算法,将点数减少到小于 500,同时尽可能接近它。到目前为止,我尝试过的效率非常低的方法如下:

simp = coordSimplify(data.tableData,0)
while (simp.length > 400) {
    i += 0.0001;
    simp = coordSimplify(data.tableData,i);
}

但我意识到这会大大减慢整个过程。

我怎样才能使整个过程更有效率?我在考虑某种二进制斩波算法,但我不确定每次如何计算上下边界。

TIA

解决方法

建议按照以下方式尝试一些事情,这实际上是一种二分搜索,寻找落在 epsilon 目标点长度范围内的 simpTargetLo 值(使用 https://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm 中的术语)和simpTargetHi

(请注意,我没有对此进行测试,因为我无法访问 coordSimplify(),因此可能存在一些语法错误,但逻辑应该是合理的。)

// 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.` );
}

请注意,这种缩小到解决方案的方法取决于初始 epsilonLoepsilonHi 的正确选择,此外还假设 coordSimplify() 本质上是相当连续的...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。