raster method

HgtRastering? raster(
  1. PixelProjection projection
)

Implementation

HgtRastering? raster(PixelProjection projection) {
  if (rows == 0) {
    return null;
  }
  HgtRastering? hgtRastering = _zoomRasterings[projection.scalefactor.zoomlevel];
  if (hgtRastering != null) {
    if (hgtRastering.xPositions.length < 2 || hgtRastering.yPositions.length < 2) return null;
    return hgtRastering;
  }

  MapRectangle rectangle = MapRectangle(
    projection.longitudeToPixelX(baseLon.toDouble()),
    projection.latitudeToPixelY((baseLat + latHeight).toDouble()),
    projection.longitudeToPixelX((baseLon + lonWidth).toDouble()),
    projection.latitudeToPixelY(baseLat.toDouble()),
  );

  double latStep = latHeight / (rows - 1);
  double lonStep = rectangle.getWidth() / (columns - 1);

  double factorX = max(4 / lonStep * MapsforgeSettingsMgr().getDeviceScaleFactor(), 1);
  double factorY = max(4 / rectangle.getHeight() * (columns - 1) * MapsforgeSettingsMgr().getDeviceScaleFactor(), 1);
  //print("Factor for $projection: $factor, $baseLat, $baseLon, $lonWidth, $latHeight, $rows, $columns");
  if (factorX > 1) {
    lonStep *= factorX;
  }
  if (factorY > 1) {
    latStep *= factorY;
  }

  List<double> xPositions = [];
  for (double x = rectangle.left; x <= rectangle.right; x += lonStep) {
    xPositions.add(x);
  }
  // possible precision error may prevent the last column. We need the last column for visual perfect borders to the next file
  if (xPositions.last != rectangle.right) {
    if (xPositions.last > rectangle.right - lonStep / 2) xPositions.removeLast();
    xPositions.add(rectangle.right);
  }
  // assert(
  //   (xPositions.length * factor).floor() <= columns,
  //   "xPositions.length: ${xPositions.length}, factor: $factor, _hgtFile.columns: ${columns}, right: ${rectangle.right}, lastPos: ${xPositions.last}, lonStep: $lonStep",
  // );

  List<double> yPositions = [];
  for (double lat = (baseLat + latHeight).toDouble(); lat >= (baseLat).toDouble(); lat -= latStep) {
    double yPosition = projection.latitudeToPixelY(lat);
    yPositions.add(yPosition);
  }
  if (yPositions.last != rectangle.bottom) {
    if (yPositions.last >= rectangle.bottom - latStep / 2) yPositions.removeLast();
    yPositions.add(rectangle.bottom);
  }
  //    assert((yPositions.length * factor).floor() <= rows, "yPositions.length: ${yPositions.length}, factor: $factor, hgtFile.rows: $rows");

  Int16List elevations = _elevations;
  if (factorX > 1 || factorY > 1) {
    elevations = Int16List(xPositions.length * yPositions.length);
    for (int y = 0; y < yPositions.length; ++y) {
      for (int x = 0; x < xPositions.length; ++x) {
        int realX = (x * factorX).floor();
        if (x == xPositions.length - 1) realX = columns - 1;
        int realY = (y * factorY).floor();
        if (y == yPositions.length - 1) realY = rows - 1;
        elevations[y * xPositions.length + x] = elevation(realX, realY);
      }
    }
  }

  hgtRastering = HgtRastering(xPositions, yPositions, elevations);
  _zoomRasterings[projection.scalefactor.zoomlevel] = hgtRastering;
  if (hgtRastering.xPositions.length < 2 || hgtRastering.yPositions.length < 2) return null;
  return hgtRastering;
}