Feature<T extends Geometry>.build constructor

Feature<T extends Geometry>.build({
  1. Object? id,
  2. WriteGeometries? geometry,
  3. Map<String, dynamic>? properties,
  4. Box? bounds,
  5. Map<String, dynamic>? custom,
})

Builds a feature from id, geometry and properties.

An optional id, when given, should be either a string or an integer number.

An optional geometry is a callback function providing the content of geometry objects. The geometry of T named "geometry" or the first geometry of T without name is stored as the primary geometry. Any other geometries are ignored (in this Feature implementation).

An optional properties defines feature properties as a map with data similar to a JSON Object.

An optional bounds can used set a minimum bounding box for a feature.

Use an optional custom parameter to set any custom or "foreign member" properties.

Examples:

// a feature with an id and a point geometry (2D coordinates)
Feature.build(
  id: '1',
  geometry: (geom) => geom.point([10.0, 20.0].xy),
);

// a feature with properties and a line string geometry (3D coordinates)
Feature.build(
  geometry: (geom) => geom.lineString(
    // three (x, y, z) positions
    [10.0, 20.0, 30.0, 12.5, 22.5, 32.5, 15.0, 25.0, 35.0]
        .positions(Coords.xyz),
  ),
  // properties for a feature containing JSON Object like data
  properties: {
    'textProp': 'this is property value',
    'intProp': 10,
    'doubleProp': 29.5,
    'arrayProp': ['foo', 'bar'],
  },
);

Implementation

factory Feature.build({
  Object? id,
  WriteGeometries? geometry,
  Map<String, dynamic>? properties,
  Box? bounds,
  Map<String, dynamic>? custom,
}) {
  // optional geometry to be built
  T? primaryGeometry;

  // use geometry builder to build primary geometry
  if (geometry != null) {
    // first try to get first geometry named "geometry"
    GeometryBuilder.build(
      geometry,
      to: (Geometry geometry, {String? name}) {
        if (name == 'geometry' && geometry is T) {
          // set "primary" geometry if not yet set
          primaryGeometry ??= geometry;
        }
      },
    );
    // if not found, then try to get first unnamed geometry
    if (primaryGeometry == null) {
      GeometryBuilder.build(
        geometry,
        to: (Geometry geometry, {String? name}) {
          if (name == null && geometry is T) {
            // set "primary" geometry if not yet set
            primaryGeometry ??= geometry;
          }
        },
      );
    }
  }

  // create a feature
  return Feature(
    id: id,
    geometry: primaryGeometry,
    properties: properties,
    bounds: bounds,
    custom: custom,
  );
}