0

我一直在尝试在系统的 openlayers3 中实现 wfs-t 服务。我可以看到

<wfs:FeatureCollection xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:sf="http://www.openplans.org/spearfish" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd http://www.openplans.org/spearfish http://localhost:9999/geoserver/sf/wfs?service=WFS&version=1.0.0&request=DescribeFeatureType&typeName=sf%3AAPRoad50MMI2015">
<gml:boundedBy>
<gml:Box srsName="EPSG:4326">
<gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" ">76.771293,12.625601 84.727878,19.166098</gml:coordinates>
</gml:Box>

我使用 featureID 从我的地理服务器中删除的 XML 是:

<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.1.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><Delete typeName="feature:jktouristspots50poi2015" xmlns:feature="http://172.31.4.67:9999/geoserver/mywork"><Filter xmlns="http://www.opengis.net/ogc"><FeatureId fid="jktouristspots50poi2015.55"/></Filter></Delete></Transaction>

1.我的geoserver详情:

Build Information GeoServer Version 2.11.1 Git Revision 0628848420a522efc1c6bbd90cb3346038929f6a Build Date 18-May-2017 22:36 GeoTools Version 17.1 (rev 86d8efab5104df08c81acf7b2f2342e08c9dea4b) GeoWebCache Version 1.11.0 (rev 1.11.x/02b79570ae64d548f9fb39bcab5edc9cb4d0c992)

对于上面的 WFS 请求,我设置了我的 ajax 函数,如下所示:

<script type="text/javascript">
var formatWFS = new ol.format.WFS();

var formatGML = new ol.format.GML({
    featureNS: 'http://localhost:9999/geoserver/mywork',
    featureType: 'jktouristspots50poi2015',
    srsName: 'EPSG:4326'
});
alert("GML",formatGML);

var xs = new XMLSerializer();

var sourceWFS = new ol.source.Vector({
     loader: function (extent, resolution, projection) {
                var url = 'http://localhost:9999/geoserver/mywork/wfs?service=WFS&' +
                    'version=1.1.0&request=GetFeature&typename=mywork:jktouristspots50poi2015&' +
                    'outputFormat=text/javascript&srsname=EPSG:4326&' +
                    'bbox=' + extent.join(',') + ',EPSG:4326';
                    alert(url);
                $.ajax({
                    url: url,
                    dataType: 'jsonp',
                    jsonpCallback: 'callback:loadFeatures',
                    jsonp: 'format_options'
                }).done(function (response) {
            alert("i am done");
        });
            },
            strategy: ol.loadingstrategy.bbox
});

 window.loadFeatures = function (response) {
            layerWFS
                .getSource()
                .addFeatures(new ol.format.GeoJSON().readFeatures(response));
        };
var layerWFS = new ol.layer.Vector({
    source: sourceWFS
});

var interaction;

var interactionSelectPointerMove = new ol.interaction.Select({
    condition: ol.events.condition.pointerMove
});

var interactionSelect = new ol.interaction.Select({
    style: new ol.style.Style({
        stroke: new ol.style.Stroke({
            color: '#FF2828'
        })
    })
});

var interactionSnap = new ol.interaction.Snap({
    source: layerWFS.getSource()
});

var map = new ol.Map({
    target: 'map',
    controls: [],
    interactions: [
        interactionSelectPointerMove,
        new ol.interaction.MouseWheelZoom(),
        new ol.interaction.DragPan()
    ],
    layers: [
        new ol.layer.Tile({
               title: 'India',
            source: new ol.source.TileWMS({
              url: 'http://ndem.nrsc.gov.in/geoserver_v3/ndem50k/wms',
              params: {LAYERS: 'ndem50k:stateadmin50census2011'}
            })
        }),
        layerWFS
    ],
    view: new ol.View({
        projection: 'EPSG:4326',
          center: [82.491,21.899],
          zoom: 5
    })
});

//wfs-t
var dirty = {};
var transactWFS = function (mode, f) {
    var node;
    switch (mode) {
        case 'insert':
            node = formatWFS.writeTransaction([f], null, null, formatGML);
            alert("after insert node",node);
            break;
        case 'update':
            node = formatWFS.writeTransaction(null, [f], null, formatGML);
            break;
        case 'delete':
            node = formatWFS.writeTransaction(null, null, [f], formatGML);
            break;
    }
    var payload = xs.serializeToString(node);
<!-- '<Transaction xmlns="http://www.opengis.net/wfs" service="WFS" version="1.1.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><Delete typeName="feature:jktouristspots50poi2015" xmlns:feature="http://172.31.4.67:9999/geoserver/mywork"><Filter xmlns="http://www.opengis.net/ogc"><FeatureId fid="jktouristspots50poi2015.55"/></Filter></Delete></Transaction>'; -->

    alert(payload);
    $.ajax('http://localhost:9999/geoserver/mywork/ows', {
        service: 'WFS',
        type: 'POST',
        dataType: 'xml',
        processData: true,
        contentType: 'text/xml',
        data: {data:payload}
    }).done(function() {
         alert("gekki hi ");
         console.log("indside done");
        sourceWFS.clear();
    });
    alert("out i am"+payload);
};


$('button').click(function () {
alert();
    $(this).siblings().removeClass('btn-active');
    $(this).addClass('btn-active');
    map.removeInteraction(interaction);
    interactionSelect.getFeatures().clear();
    map.removeInteraction(interactionSelect);

    switch ($(this).attr('id')) {

        case 'btnEdit':
            map.addInteraction(interactionSelect);
            interaction = new ol.interaction.Modify({
                features: interactionSelect.getFeatures()
            });
            alert("am edit "+interaction);
            map.addInteraction(interaction);
            map.addInteraction(interactionSnap);
            dirty = {};
            interactionSelect.getFeatures().on('add', function (e) {
                e.element.on('change', function (e) {
                //alert("i am add ");
                    dirty[e.target.getId()] = true;
                });
            });
            interactionSelect.getFeatures().on('remove', function (e) {
                var f = e.element;
                if (dirty[f.getId()]) {
                alert("i am in remove");
                    delete dirty[f.getId()];
                    var featureProperties = f.getProperties();
                    delete featureProperties.boundedBy;
                    var clone = new ol.Feature(featureProperties);
                    clone.setId(f.getId());
                    transactWFS('update', clone);
                }
            });
            break;

        case 'btnPoint':
            interaction = new ol.interaction.Draw({
                type: 'Point',
                source: layerWFS.getSource()
            });
            map.addInteraction(interaction);
            interaction.on('drawend', function (e) {
                transactWFS('insert', e.feature);
            });
            break;

        case 'btnLine':
        alert("line feature draw");
            interaction = new ol.interaction.Draw({
                type: 'LineString',
                source: layerWFS.getSource()
            });
            map.addInteraction(interaction);
            interaction.on('drawend', function (e) {
            var myAttrValue = prompt("Enter Attribute", "");
            var myFeature= e.feature;
            if (myAttrValue != null) {
             myFeature.set('column', myAttrValue);
             }
                transactWFS('insert', myFeature);
            });
            break;

        case 'btnArea':
            interaction = new ol.interaction.Draw({
                type: 'Polygon',
                source: layerWFS.getSource()
            });
            interaction.on('drawend', function (e) {
                transactWFS('insert', e.feature);
            });
            map.addInteraction(interaction);
            break;

        case 'btnDelete':
            interaction = new ol.interaction.Select();
            interaction.getFeatures().on('add', function (e) {
                transactWFS('delete', e.target.item(0));
                interactionSelectPointerMove.getFeatures().clear();
                interaction.getFeatures().clear();
            });
            map.addInteraction(interaction);
            break;

        default:
            break;
    }
});
</script>

如何在地理服务器中启用全局设置,什么是 ows?如果我通过现有路径,它也会抛出错误,例如:

我在 GEOSERVER 中的工作空间路径:C:\Program Files (x86)\GeoServer 2.11.1\data_dir\workspaces\mywork\jktourisam geoserevr 中的工作空间路径wfs-t.jpg带有 wfs-t 选项的地图上的 wfs 图层

postgislayer.html:1 XMLHttpRequest 无法加载http://localhost:9999/geoserver/mywork/wfs。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://localhost ”。响应具有 HTTP 状态代码 403。

我尝试了很多方法,我找不到解决方案.......

我能够在我的地图上查看添加的 WFS 矢量图层。我的代码中是否有任何参数错误?同时,虽然我已经从我的地理服务器使用相同的图层 url 测试了 WFS-T,但在那里我也无法删除和插入地理服务器和 postgres 的功能,但我可以通过使用地图上的多边形、线和点特征在地图上绘制并且可以检索功能ID?我还想在我的代码中实现 CQL 和 ECQL 过滤器,请提供任何参考链接以供学习。谢谢

意思是虽然从 ajax 调用(POST)警告成功也不起作用,我认为这是因为我通过了错误的 url?这是对的吗?请指导我在geoserver中设置环境以执行wfs-t ..

4

1 回答 1

0

您需要在容器中启用 CORS。如果您使用 Jetty 作为容器,请参阅此问题,如果您使用 tomcat ,请参阅此问题。

于 2018-06-21T10:03:43.303 回答