buildGeometry method

Geometry buildGeometry(
  1. List<Geometry> geomList
)

Build an appropriate Geometry, MultiGeometry, or GeometryCollection to contain the Geometrys in it. For example:

  • If geomList contains a single Polygon, the Polygon is returned.
  • If geomList contains several Polygons, a MultiPolygon is returned.
  • If geomList contains some Polygons and some LineStrings, a GeometryCollection is returned.
  • If geomList is empty, an empty GeometryCollection is returned

Note that this method does not "flatten" Geometries in the input, and hence if any MultiGeometries are contained in the input a GeometryCollection containing them will be returned.

@param geomList the Geometrys to combine @return a Geometry of the "smallest", "most type-specific" class that can contain the elements of geomList .

Implementation

Geometry buildGeometry(List<Geometry> geomList) {
  /**
   * Determine some facts about the geometries in the list
   */
  var geomClass = null;
  bool isHeterogeneous = false;
  bool hasGeometryCollection = false;
  for (Iterator i = geomList.iterator; i.moveNext();) {
    Geometry geom = i.current as Geometry;
    var partClass = geom.runtimeType;
    if (geomClass == null) {
      geomClass = partClass;
    }
    if (partClass != geomClass) {
      isHeterogeneous = true;
    }
    if (geom is GeometryCollection) hasGeometryCollection = true;
  }

  /**
   * Now construct an appropriate geometry to return
   */
  // for the empty geometry, return an empty GeometryCollection
  if (geomClass == null) {
    return createGeometryCollectionEmpty();
  }
  if (isHeterogeneous || hasGeometryCollection) {
    return createGeometryCollection(geomList);
  }
  // at this point we know the collection is hetereogenous.
  // Determine the type of the result from the first Geometry in the list
  // this should always return a geometry, since otherwise an empty collection would have already been returned
  Geometry geom0 = geomList[0]; //.iterator().next();
  bool isCollection = geomList.length > 1;
  if (isCollection) {
    if (geom0 is Polygon) {
      return createMultiPolygon(geomList.cast<Polygon>());
    } else if (geom0 is LineString) {
      return createMultiLineString(geomList.cast<LineString>());
    } else if (geom0 is Point) {
      return createMultiPoint(geomList.cast<Point>());
    }
    Assert.shouldNeverReachHere("Unhandled class: ${geom0.runtimeType}");
  }
  return geom0;
}