function getCleanedFeature()

in modules/editor/src/lib/importer.ts [55:101]


function getCleanedFeature(feature: Feature): Feature {
  const { id } = feature;
  // reduce null-checking
  const properties = feature.properties || {};

  let geometry = feature.geometry;
  // @ts-expect-error no overlap, optimize
  if (geometry.type === 'GeometryCollection' && geometry.geometries.length === 1) {
    // There's only one geometry
    // @ts-expect-error no overlap, optimize
    geometry = geometry.geometries[0];
    // @ts-expect-error no overlap, optimize
  } else if (geometry.type === 'GeometryCollection' && geometry.geometries.length > 1) {
    // @ts-expect-error no overlap, optimize
    const types = new Set(geometry.geometries.map((g) => g.type));
    if (types.size === 1) {
      // See if it can be combined into a Multi* geometry
      const type = types.values().next().value;
      if (type === 'Polygon') {
        // Combine all the Polygons into a single MultiPolygon
        geometry = {
          type: 'MultiPolygon',
          // @ts-expect-error no overlap, optimize
          coordinates: geometry.geometries.map((g) => g.coordinates),
        };
      } else if (type === 'LineString') {
        // Combine all the LineStrings into a single MultiLineString
        geometry = {
          type: 'MultiLineString',
          // @ts-expect-error no overlap, optimize
          coordinates: geometry.geometries.map((g) => g.coordinates),
        };
      }
    } else {
      // Mixed geometry types, we don't yet handle it
      throw Error('GeometryCollection geometry type not yet supported');
    }
  }

  // @ts-expect-error narrow types
  return {
    type: 'Feature',
    id,
    geometry,
    properties,
  };
}