The RasterModelHeightProviderRasterModelHeightProviderRasterModelHeightProvider class allows you to retrieve height values from an elevation model.

Creating a RasterModelHeightProvider

When creating a RasterModelHeightProviderRasterModelHeightProviderRasterModelHeightProvider, you must provide a IRasterModelIRasterModelIRasterModel containing elevation data. Optionally, you can specify if you want to apply bilinear interpolation to the height values. You can also specify the coordinate reference in which the height provider operates. If you don’t provide any coordinate reference, the RasterModelHeightProviderRasterModelHeightProviderRasterModelHeightProvider uses the coordinate reference of the raster model.

A RasterModelHeightProviderRasterModelHeightProviderRasterModelHeightProvider can retrieve elevation data at a particular coordinate and at a particular density. The result is expressed as a height value above mean sea level.

Pixel density

When you retrieve elevation data, you must specify the maximum pixel density of the data you want. The pixel density represents the number of samples present in the data over a unit of area in a given coordinate reference. A PixelDensityPixelDensityPixelDensity can be created from a distance between samples (in meters), to simplify use cases like sampling along a line every N meters.

When it queries data from a multi-level model, the RasterModelHeightProviderRasterModelHeightProviderRasterModelHeightProvider looks for the most detailed level with a pixel density equal to or lower than the requested pixel density. If it doesn’t find data with a pixel density equal to or lower than the desired pixel density, it returns the least detailed data.

Program: Sampling the elevation along a line every 10km
auto heightProvider = RasterModelHeightProvider::newBuilder().rasterModel(elevationModel).interpolate(false).reference(wgs84).build();

auto sanSebastian = Coordinate{43.325638, -1.99256};
auto cadaques = Coordinate{42.288199, 3.298322};
auto line = GeometryFactory::createLine(wgs84, sanSebastian, cadaques, LineInterpolationType::Geodesic);

double sampleDistance = 10000.0; // 10 km
auto numberOfSamples = static_cast<size_t>(line->getLength2D() / sampleDistance);

std::vector<double> samples;
samples.reserve(numberOfSamples);
for (size_t i = 0; i < numberOfSamples; ++i) {
  auto t = static_cast<double>(i) / static_cast<double>(numberOfSamples - 1);
  auto point = line->computePoint(t);
  auto density = PixelDensity::fromSampleDistance(sampleDistance, point, wgs84);
  auto height = heightProvider.retrieveHeightAt(point, density);
  samples.push_back(height.value());
}
var heightProvider = RasterModelHeightProvider.NewBuilder()
    .RasterModel(elevationModel)
    .Interpolate(false)
    .Reference(wgs84)
    .Build();

var sanSebastian = new Coordinate(43.325638, -1.99256);
var cadaques = new Coordinate(42.288199, 3.298322);
var line = GeometryFactory.CreateLine(wgs84, sanSebastian, cadaques, LineInterpolationType.Geodesic);

var sampleDistance = 10000; // 10 km
var numberOfSamples = (int)(line.Length2D / sampleDistance);

var samples = new List<double>();
for (var i = 0; i < numberOfSamples; ++i)
{
    var t = (double)i / (numberOfSamples - 1);
    var point = line.ComputePoint(t);
    var density = PixelDensity.FromSampleDistance(sampleDistance, point, wgs84);
    var height = heightProvider.RetrieveHeightAt(point, density);
    samples.Add(height.Value);
}
var heightProvider = RasterModelHeightProvider.newBuilder()
                                              .rasterModel(elevationModel)
                                              .interpolate(false)
                                              .reference(wgs84)
                                              .build();

var sanSebastian = new Coordinate(43.325638, -1.99256);
var cadaques = new Coordinate(42.288199, 3.298322);
var line = GeometryFactory.createLine(wgs84, sanSebastian, cadaques, LineInterpolationType.Geodesic);

double sampleDistance = 10000; // 10 km
var numberOfSamples = (int) (line.getLength2D() / sampleDistance);

var samples = new ArrayList<Double>();
for (var i = 0; i < numberOfSamples; ++i) {
  var t = (double) i / (numberOfSamples - 1);
  var point = line.computePoint(t);
  var density = PixelDensity.fromSampleDistance(sampleDistance, point, wgs84);
  var height = heightProvider.retrieveHeightAt(point, density);
  samples.add(height);
}