You need to enable JavaScript to run this app. 使用nebula.gl的EditableGeoJsonLayer关闭多边形报非可迭代实例错误
优惠活动
大模型
产品
解决方案
定价
更多

使用nebula.gl的EditableGeoJsonLayer关闭多边形报非可迭代实例错误

nebula.gl EditableGeoJsonLayer 报错解决方案

多边形闭合时报Invalid attempt to spread non-iterable instance错误

根因

该错误来自nebula.gl内置的ImmutableFeatureCollection逻辑,代码尝试展开this.featureCollection.features字段时,该字段不是可迭代的数组类型,多是因为传入图层的GeoJSON数据的features字段被意外转为非数组格式。

修复步骤

  1. 确保初始定义的FeatureCollection格式标准,不要直接用any标注类型:
import type { FeatureCollection } from 'geojson';

const myFeatureCollection: FeatureCollection = {
    type: 'FeatureCollection',
    features: []
};
  1. 在onEdit回调中增加数据校验,强制保证写入state的features始终为数组:
const editableLayer = new (EditableGeoJsonLayer as any)({
    id: 'geojson',
    data: featureState.data,
    mode: DrawPolygonMode,
    selectedFeatureIndexes,
    onEdit: (updatedData: any) => {
        const safeUpdatedData = {
            ...updatedData,
            features: Array.isArray(updatedData.features) ? updatedData.features : []
        };
        setFeatureState({ data: safeUpdatedData });
    },
});

EditableGeoJsonLayer实例化需强转any的TypeScript错误

根因

当前版本nebula.gl的官方TypeScript类型定义与deck.gl的Layer构造函数定义存在兼容问题,类型校验不通过。

修复步骤

在项目类型声明目录中新增nebula.gl.d.ts全局类型补全文件,内容如下:

declare module '@nebula.gl/layers' {
    import { Layer, LayerProps } from '@deck.gl/core';
    import { FeatureCollection } from 'geojson';
    import { EditMode } from '@nebula.gl/edit-modes';

    interface EditableGeoJsonLayerProps extends LayerProps {
        data: FeatureCollection;
        mode: typeof EditMode;
        selectedFeatureIndexes?: number[];
        onEdit?: (updatedData: FeatureCollection, editContext: any) => void;
    }

    export class EditableGeoJsonLayer extends Layer<EditableGeoJsonLayerProps> {
        constructor(props: EditableGeoJsonLayerProps);
    }
}

添加完成后即可去掉as any强转,直接调用构造函数实例化图层。

额外注意事项

  • 确保nebula.gl与deck.gl的版本匹配:nebula.gl 0.2x版本对应deck.gl 8.x版本,不要跨大版本安装,避免出现未知兼容问题。

内容的提问来源于stack exchange,提问作者Neel Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 10:54:04