elevationAt method

int? elevationAt(
  1. double latitude,
  2. double longitude
)

Returns the elevation in meters for the given lat/lon.

The coordinates must lie within baseLat..baseLat+width and baseLon..baseLon+width.

Uses bilinear interpolation between surrounding samples.

Implementation

int? elevationAt(double latitude, double longitude) {
  if (rows == 0) {
    // file not found
    return null;
  }
  if (latitude < baseLat || latitude > baseLat + latHeight || longitude < baseLon || longitude > baseLon + lonWidth) {
    return null;
  }

  // HGT rows are north-to-south.
  // u, v are fractions of lat/lon coordinates inbetween the current file-boundaries
  final double u = (longitude - baseLon) / lonWidth;
  final double v = ((baseLat + latHeight) - latitude) / latHeight;

  // x,y are indices into the elevation data in double digits
  double x = u * (columns - 1);
  double y = v * (rows - 1);

  assert(x >= 0 && x < columns, 'x: $x, columns: $columns');
  assert(y >= 0 && y < rows, 'y: $y, rows: $rows');

  final q00 = elevation(x.round(), y.round());
  return q00;
}