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.
Symptom
- Console: Error: Invalid MultiPolygon coordinates: coordinates must be an array of Polygon coordinate arrays.
- Leaflet logs nothing and simply renders that feature not at all.
- QGIS reports that the GeoJSON is invalid and the geometry cannot be parsed.
Checklist
- Print geometry.coordinates and check whether level 2 holds arrays or numbers.
- Count nesting depth recursively and compare with the spec: Point 1, LineString 2, Polygon 3, MultiPolygon 4.
- Check whether the backend hand-assembled the geometry instead of using ST_AsGeoJSON.
- Open the file in QGIS or run turf.cleanCoords to see the rejection.
- 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
- Hand-editing geometry.type from Polygon to MultiPolygon without adding the extra array level.
- Adding two wrapper levels and turning each ring into a degenerate polygon.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). MultiPolygon missing one array level reports invalid geometry. https://www.jingweizhi.com/en/cookbook/geojson-multipolygon-depth.html (accessed 2026-11-03).