| 411 | |
| 412 | |
| 413 | void GDALGrid::update(size_t i, size_t j, double val, double dist) |
| 414 | { |
| 415 | // Once we determine that a point is close enough to a cell to count it, |
| 416 | // this function does the actual math. We use the value of the |
| 417 | // point (val) and its distance from the cell center (dist). There's |
| 418 | // a little math that needs to be done once all points are added. See |
| 419 | // finalize() for that. |
| 420 | |
| 421 | // See |
| 422 | // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance |
| 423 | // https://en.wikipedia.org/wiki/Inverse_distance_weighting |
| 424 | |
| 425 | double& count = m_count->at(i, j); |
| 426 | count++; |
| 427 | |
| 428 | if (m_pctls.size()) |
| 429 | m_valBins[m_count->indexAt(i, j)].push_back(val); |
| 430 | |
| 431 | if (m_min) |
| 432 | { |
| 433 | double& min = m_min->at(i, j); |
| 434 | min = (std::min)(val, min); |
| 435 | } |
| 436 | |
| 437 | if (m_max) |
| 438 | { |
| 439 | double& max = m_max->at(i, j); |
| 440 | max = (std::max)(val, max); |
| 441 | } |
| 442 | |
| 443 | if (m_mean) |
| 444 | { |
| 445 | double& mean = m_mean->at(i, j); |
| 446 | double delta = val - mean; |
| 447 | |
| 448 | mean += delta / count; |
| 449 | if (m_stdDev) |
| 450 | { |
| 451 | double& stdDev = m_stdDev->at(i, j); |
| 452 | stdDev += delta * (val - mean); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (m_idw) |
| 457 | { |
| 458 | double& idw = m_idw->at(i, j); |
| 459 | double& idwDist = m_idwDist->at(i, j); |
| 460 | |
| 461 | // If the distance is 0, we set the idwDist to nan to signal that |
| 462 | // we should ignore the distance and take the value as is. |
| 463 | if (!std::isnan(idwDist)) |
| 464 | { |
| 465 | if (dist == 0) |
| 466 | { |
| 467 | idw = val; |
| 468 | idwDist = std::numeric_limits<double>::quiet_NaN(); |
| 469 | } |
| 470 | else |