cropWay method

Wayholder? cropWay(
  1. Wayholder wayholder,
  2. BoundingBox boundingBox,
  3. int maxZoomlevel
)

Crops the ways within a wayholder to the given boundingBox.

This method processes the inner and outer ways of the wayholder, optimizing them to include only the segments that are visible within the bounding box. It also simplifies the geometry if it contains too many points.

Returns a new Wayholder with the cropped ways, or null if no part of the way is within the bounding box.

Implementation

///
  /// This method processes the inner and outer ways of the wayholder, optimizing
  /// them to include only the segments that are visible within the bounding box.
  /// It also simplifies the geometry if it contains too many points.
  ///
  /// Returns a new [Wayholder] with the cropped ways, or `null` if no part of
  /// the way is within the bounding box.
  Wayholder? cropWay(Wayholder wayholder, BoundingBox boundingBox, int maxZoomlevel) {
List<Waypath> inner = wayholder.innerRead.map((test) => _optimizeWaypoints(test, boundingBox, maxZoomlevel)).toList()
  ..removeWhere((Waypath test) => test.isEmpty);
List<Waypath> closedOuters = wayholder.closedOutersRead.map((test) => _optimizeWaypoints(test, boundingBox, maxZoomlevel)).toList()
  ..removeWhere((Waypath test) => test.isEmpty);
List<Waypath> openOuters = wayholder.openOutersRead.map((test) => _optimizeWaypoints(test, boundingBox, maxZoomlevel)).toList()
  ..removeWhere((Waypath test) => test.isEmpty);

if (inner.isEmpty && closedOuters.isEmpty && openOuters.isEmpty) return null;

if (closedOuters.isEmpty && openOuters.isEmpty) {
  // only inner is set, move the first inner to the respective outer
  Waypath waypath = inner.first;
  inner.remove(waypath);
  if (waypath.isClosedWay()) {
    closedOuters.add(waypath);
  } else {
    openOuters.add(waypath);
  }
}

// return a new wayholder instance
return wayholder.cloneWith(inner: inner, closedOuters: closedOuters, openOuters: openOuters);
  }