OpenLayer修改WFS中的要素

就像arcgis api一样既然我们加载要素图层服务,我们应该既然加载要素,有时候我们需要修改他,增加一些矢量数据或者修改矢量数据,在这里我写的如何去修改数据。代码参考扯淡大叔的修改WFS的代码,但是中间出现了了一些问题,在这里指出。

出现的问题如下图:
 

解决方案:

1、

 

 

 

 

 

 

2、

添加新规则

3、

选择工作空间---->选择图层----->Access mode----->Grant access to any role

一、完整demo

<!DOCTYPE html>
<html>
<head>
    <title>Modify Features</title>

    <link href="../script/ol4/ol.css" rel="stylesheet" />
    <script src="../script/ol4/ol.js"></script>
</head>
<body>
    <input id="save" type="button" value="保存" onclick="onSave();" />
    <div id="map" class="map"></div>
    <script>
        var raster = new ol.layer.Tile({
            source: new ol.source.OSM()
        });
        var modifiedFeatures = null;
        var wfsVectorLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                format: new ol.format.GeoJSON({
                    geometryName: 'geom' // 因为数据源里面字段geom存储的是geometry,所以需要指定
                }),
                url: 'http://localhost:8080/geoserver/cite/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=cite%3Aroa_4m&maxFeatures=1000&outputFormat=application%2Fjson'
            }),
            style: function (feature, resolution) {
                return new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: 'blue',
                        width: 5
                    })
                });
            }
        });
        var selectInteraction = new ol.interaction.Select({
            wrapX: false
        });
        var modifyInteraction = new ol.interaction.Modify({
            features: selectInteraction.getFeatures()
        });
        modifyInteraction.on('modifyend', function (e) {
            // 把修改完成的feature暂存起来
            modifiedFeatures = e.features;
        });
        var view = new ol.View({
            center: [0, 0],
            projection: 'EPSG:4326',
            zoom: 2
        });
        var map = new ol.Map({
            interactions: ol.interaction.defaults().extend([selectInteraction, modifyInteraction]),
            layers: [raster, wfsVectorLayer],
            target: 'map',
            view: view
        });
        // 保存已经编辑的要素
        function onSave() {
            if (modifiedFeatures && modifiedFeatures.getLength() > 0) {

                // 转换坐标
                var modifiedFeature = modifiedFeatures.item(0).clone();
                // 注意ID是必须,通过ID才能找到对应修改的feature
                modifiedFeature.setId(modifiedFeatures.item(0).getId());
                // 调换经纬度坐标,以符合wfs协议中经纬度的位置
                modifiedFeature.getGeometry().applyTransform(function (flatCoordinates, flatCoordinates2, stride) {
                    for (var j = 0; j < flatCoordinates.length; j += stride) {
                        var y = flatCoordinates[j];
                        var x = flatCoordinates[j + 1];
                        flatCoordinates[j] = x;
                        flatCoordinates[j + 1] = y;
                    }
                });
                modifyWfs([modifiedFeature]);
            }
        }
        // 把修改提交到服务器端
        function modifyWfs(features) {
            var WFSTSerializer = new ol.format.WFS();
            var featObject = WFSTSerializer.writeTransaction(null,
                features, null, {
                    featureType: 'roa_4m',//图层名称
                    featureNS: 'http://opengeospatial.net/cite',  // 注意这个值必须为创建工作区时的命名空间URI
                    srsName: 'EPSG:4326'
                });
            // 转换为xml内容发送到服务器端
            var serializer = new XMLSerializer();
            var featString = serializer.serializeToString(featObject);
            var request = new XMLHttpRequest();
            request.open('POST', 'http://localhost:8080/geoserver/cite/ows?service=WFS');
            // 指定内容为xml类型
            request.setRequestHeader('Content-Type', 'text/xml');
            request.send(featString);
        }
    </script>
</body>
</html>

二、效果图

三、在Udig中查看表中的数据是否改动

 

转载于:https://www.cnblogs.com/tuboshu/p/10752293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值