lineStringToPolygon function

Feature<Polygon> lineStringToPolygon(
  1. GeoJSONObject line,
  2. bool autoComplete,
  3. bool orderCoords, {
  4. Map<String, dynamic>? properties,
})

Converts LineString to Polygon Takes a optional boolean autoComplete that auto completes LineStrings Takes an optional orderCoords that sorts LineStrings to place outer ring at the first Position of the coordinates.

Implementation

Feature<Polygon> lineStringToPolygon(
  GeoJSONObject line,
  bool autoComplete,
  bool orderCoords, {
  Map<String, dynamic>? properties,
}) {
  properties = properties ?? (line is Feature ? line.properties ?? {} : {});

  if (line is Feature && line.geometry != null) {
    return lineStringToPolygon(line.geometry!, autoComplete, orderCoords);
  }

  if (line is GeometryType && line.coordinates.isEmpty) {
    throw Exception("line must contain coordinates");
  }

  if (line is LineString) {
    var coords =
        autoComplete ? _autoCompleteCoords(line.coordinates) : line.coordinates;

    return Feature(
        geometry: Polygon(coordinates: [coords]), properties: properties);
  } else if (line is MultiLineString) {
    List<List<Position>> multiCoords = [];
    num largestArea = 0;

    for (var coord in line.coordinates) {
      if (autoComplete) {
        coord = _autoCompleteCoords(coord);
      }

      // Largest LineString to be placed in the first position of the coordinates array
      if (orderCoords) {
        var area = _calculateArea(bbox(LineString(coordinates: coord)));
        if (area > largestArea) {
          multiCoords.insert(0, coord);
          largestArea = area;
        } else {
          multiCoords.add(coord);
        }
      } else {
        multiCoords.add(coord);
      }
    }
    return Feature(
        geometry: Polygon(coordinates: multiCoords), properties: properties);
  } else {
    throw Exception("Geometry type  ${line.type}  is not supported");
  }
}