Atlaslog
Atlaslog / Field Notes / Loading & display
Field note · Loading & display

MultiPolygon missing one array level reports invalid geometry

Quick answer

GeoJSON requires four nesting levels for MultiPolygon, three for Polygon. A backend that retypes a single Polygon to MultiPolygon produces only three levels, so turf throws and QGIS refuses to parse the file.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: turf.js 7.x · Mapbox GL JS 2.15 · Leaflet 1.9.x
Symptom
Checklist
  1. Print geometry.coordinates and check whether level 2 holds arrays or numbers.
  2. Count nesting depth recursively and compare with the spec: Point 1, LineString 2, Polygon 3, MultiPolygon 4.
  3. Check whether the backend hand-assembled the geometry instead of using ST_AsGeoJSON.
  4. Open the file in QGIS or run turf.cleanCoords to see the rejection.
  5. Confirm the inner content really is polygons and not rings before adding a wrapper.
Fix

Use ST_Multi in SQL so PostGIS emits the four-level structure, or wrap the existing three-level coordinates in one more array on the client: coordinates: [geom.coordinates].

Code
function depth(a) {
  return Array.isArray(a) ? 1 + Math.max(...a.map(depth)) : 0;
}
depth(fc.features[0].geometry.coordinates);   // 3 -> 应为 4
console.log(fc.features[0].geometry.coordinates[0]);
// 正确:[ [ [116.1,39.8], [116.2,39.8], ... ] ]
-- 正确:交给 PostGIS 生成 MultiPolygon
SELECT jsonb_build_object(
  'type', 'Feature',
  'properties', jsonb_build_object('name', name),
  'geometry', ST_AsGeoJSON(ST_Multi(geom), 15)::jsonb
)
FROM districts;

-- 若只有一个面,ST_Multi 也会生成合法的 MultiPolygon
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). MultiPolygon missing one array level reports invalid geometry. https://www.jingweizhi.com/en/cookbook/geojson-multipolygon-depth.html (accessed 2026-11-03).