使用要素服务执行编辑

字数统计: 1k
阅读时长: 约 3 分钟
当前版本: 4.29

什么是要素服务?

要素服务是一种基于 web 的服务,允许用户访问、查询和编辑地理要素及其属性。要素服务使客户能够执行编辑以增强编辑体验,包括编辑关系类和非空间表中的数据。

为什么使用要素服务?

要素服务类仅使用一个 applyEdits() 操作就可以编辑多个图层。而要素图层FeatureLayer 类在每次 applyEdits() 操作时只能编辑一个图层。

加载要素服务类

要素服务类通过要素服务的 URL 地址定义。类实例化后,即可加载服务。示例:

js
    const featureService = new FeatureService({
        url: "https://test.cn/server/rest/services/testService/FeatureServer"
    });
    await featureService.load();

定义编辑参数

设置编辑参数时有一些选项可以设置。ServiceEditOptions 参数需要指定一个 图层 id (要素服务上图层的图层 id) 和 identifierFields (objectIdField 和 globalIdField)。需要为 applyEdits 方法按照要素服务结构指定正确的参数才能使编辑生效。以下是为指定的 layerId 上添加多边形要素的编辑配置。

js
    const layer = new FeatureLayer({
        url: "https://test.cn/server/rest/services/testService/FeatureServer/5"
    });
    layer.load();
    const edits =
    {
        id: layer.layerId,
        identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
        addFeatures: [
            new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
            new Graphic({
                geometry: new Polygon({
                    hasZ: true,
                    hasM: true,
                    rings: [
                        [
                            // first ring
                            [-97, 32, 35, 4],
                            [-97, 32, 35, 4],
                            [-97, 32, 35, 4],
                            [-97, 32, 35, 4] // same as first vertex
                        ],
                        [
                            // second ring
                            [-97, 32, 35],
                            [-97, 32, 35],
                            [-97, 32, 35],
                            [-97, 32, 35] // same as first vertex
                        ]
                    ],
                    spatialReference: {
                        wkid: 26911,
                        latestWkid: 26911,
                        vcsWkid: 115702,
                        latestVcsWkid: 115702
                    }
                }),
                attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                visible: true
            })
        ]
    }

编辑对象由 id、identifierFields 和 ServiceEditOptions 组成。编辑服务时,必须配置 ServiceEditOptions

applyEdits 方法

applyEdits 方法接受 ServiceEdits 和 ServiceEditOptions 的数组参数。支持编辑要素服务的多个图层。如下:

js
    const layer = new FeatureLayer({
        url: "https://test.cn/server/rest/services/testService/FeatureServer/5"
    });
    await layer.load();
    await featureService.applyEdits([
        {
            id: layer.layerId,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
                new Graphic({
                    geometry: new Polygon({
                        hasZ: true,
                        hasM: true,
                        rings: [
                            [
                                // first ring
                                [-97, 32, 35, 4],
                                [-97, 32, 35, 4],
                                [-97, 32, 35, 4],
                                [-97, 32, 35, 4] // same as first vertex
                            ],
                            [
                                // second ring
                                [-97, 32, 35],
                                [-97, 32, 35],
                                [-97, 32, 35],
                                [-97, 32, 35] // same as first vertex
                            ]
                        ],
                        spatialReference: {
                            wkid: 26911,
                            latestWkid: 26911,
                            vcsWkid: 115702,
                            latestVcsWkid: 115702
                        }
                    }),
                    attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                    visible: true
                })
            ]
        }
    ],
    {
        gdbVersion: layer.gdbVersion,
        globalIdUsed: false,
        honorSequenceOfEdits: false,
        usePreviousEditMoment: false,
        returnServiceEditsInSourceSR: false
    });

用户还支持更复杂的编辑。在此示例中,用户执行的操作包括 addFeaturesupdateFeaturesdeleteFeaturesaddAttachmentsupdateAttachmentsdeleteAttachments

js
    const mockAttachment: Attachment = {
        globalId: "123456789",
        name: "example.pdf",
        contentType: "application/pdf",
        data: "testData"
    };
    const mockFeatureIdentifier: FeatureIdentifier = {
        objectId: 2,
        globalId: "testGlobalId"
    };
    const attachmentEdit: AttachmentEdit = {
        feature: mockFeatureIdentifier,
        attachment: mockAttachment
    };
    await featureServiceGlobal.applyEdits([
        {
            id: layer.layerId,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true }),
                new Graphic({ attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ],
            updateFeatures: [
                new Graphic({
                    attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                    geometry: new Polyline({
                        hasZ: true,
                        hasM: true,
                        paths: [
                            [477710.14010000043, 3630581.9410999995, 0, null],
                            [477899.40060000028, 3630521.2847000007, 0, null]
                        ],
                        spatialReference: {
                            wkid: 26911,
                            latestWkid: 26911,
                            vcsWkid: 115702,
                            latestVcsWkid: 115702
                        }
                    })
                }),
                new Graphic({
                    attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" },
                    geometry: new Polyline({
                        hasZ: true,
                        hasM: true,
                        paths: [
                            [477710.14010000043, 3630581.9410999995, 0, null],
                            [477899.40060000028, 3630521.2847000007, 0, null]
                        ],
                        spatialReference: {
                            wkid: 26911,
                            latestWkid: 26911,
                            vcsWkid: 115702,
                            latestVcsWkid: 115702
                        }
                    })
                })
            ],
            deleteFeatures: [
                new Graphic({ attributes: { GLOBALID: "{A6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ],
            addAttachments: [attachmentEdit, attachmentEdit],
            updateAttachments: [attachmentEdit, attachmentEdit],
            deleteAttachments: ["myAttachment", "myOtherAttachment"]
        }
    ],
    {
        gdbVersion: layer.gdbVersion,
        globalIdUsed: true,
        honorSequenceOfEdits: true,
        usePreviousEditMoment: true,
        returnServiceEditsInSourceSR: true
    });

也可一次编辑多个图层。

js
    await featureServiceGlobal.applyEdits([
        {
            id: 0,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
        },
        {
            id: 1,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
        },
        {
            id: 2,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
        },
        {
            id: 3,
            identifierFields: { globalIdField: "GLOBALID", objectIdField: "OBJECTID" },
            addFeatures: [
                new Graphic({ attributes: { GLOBALID: "{C6CBBA9B-7B61-43A6-B81E-E82CA9723BC7}" }, visible: true })
            ]
        }
    ],
    {
        gdbVersion: layer.gdbVersion,
        globalIdUsed: false,
        honorSequenceOfEdits: false,
        usePreviousEditMoment: false,
        returnServiceEditsInSourceSR: false
    });