openlayers通过geoserver中WFS更改要素
目录
一、引言
上文介绍了通过WFS服务进行添加,在很多情况下我们会对当前添加的要素进行修改,本篇将进行介绍,与添加大同小异,不过还是要有细节需要关注。
二、WFS更改要素实现
代码前提是使用interaction的modify对象,“modifyend”事件获取feature对象后,进行保存。
if (modifyFeatures && modifyFeatures.getLength() > 0) {
var modifyFeature = modifyFeatures.item(0).clone();
modifyFeature.setId(modifyFeatures.item(0).getId());
modifyFeature.set('Text', '修改后的text');
/* modifyFeature.set('the_geom', modifyFeature.getGeometry());
modifyFeature.unset("geometry");*/
modifyFeature.setGeometryName("the_geom");
var format = new ol.format.WFS();
var xml = format.writeTransaction(null, [modifyFeature], null, {
featureNS: 'http://geoserver.org/nyc',
featurePrefix: "xcy",//工作空间名称
featureType: "polygon"//图层名称
});
var serializer = new XMLSerializer();
var featString = serializer.serializeToString(xml);
$.ajax({
url: "http://localhost:8080/geoserver/wfs",
type: "POST",
data: featString,
contentType: 'text/xml',
success: function (req) {
//select.getFeatures().clear();
console.log(req);
}
});
}
注意在将旧feature.clone()之后一定要setId(),因为clone函数中没有clone feature的id,不然无法添加;
这里还需要注意的是一定要设置setGeometryName为the_geom,而不是像在上篇文章添加在feature.set(“the_geom”,geometry)。
我也很纳闷,同样是识别feature中的几何字段,为什么添加的时候是geometry中的key(如图第二个框),更改的时候是feature中的GeometryName的value(如图第一个框),反正不这样设置就添加不上,如果细究的去看看wfs内部实现了,有时间去瞄瞄。
三、WFS更改要素原理
1、请求xml
<?xml version="1.0" encoding="utf-8"?>
<Transaction xmlns="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" service="WFS" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
<Update xmlns:xcy="http://geoserver.org/nyc" typeName="xcy:polygon">
<Property>
<Name>the_geom</Name>
<Value>
<MultiPolygon xmlns="http://www.opengis.net/gml">
<polygonMember>
<Polygon>
<exterior>
<LinearRing>
<posList srsDimension="2">508797.40571958 299931.13585535 508825.47244091 299926.35854108 508818.3064695 299913.81809113 508799.3465035 299894.41025191 508788.29896426 299901.57622331 508726.34316985 299918.59540539 508768.1446696922 299948.45361956727 508797.40571958 299931.13585535</posList>
</LinearRing>
</exterior>
</Polygon>
</polygonMember>
</MultiPolygon>
</Value>
</Property>
<Property>
<Name>Text</Name>
<Value>修改后的text</Value>
</Property>
<Filter xmlns="http://www.opengis.net/ogc">
<FeatureId fid="polygon.571"/>
</Filter>
</Update>
</Transaction>
2、postman使用
3、要素修改
4、返回xml
<?xml version="1.0" encoding="UTF-8"?>
<wfs:TransactionResponse xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:ows="http://www.opengis.net/ows" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1.0" xsi:schemaLocation="http://www.opengis.net/wfs http://localhost:8080/geoserver/schemas/wfs/1.1.0/wfs.xsd">
<wfs:TransactionSummary>
<wfs:totalInserted>0</wfs:totalInserted>
<wfs:totalUpdated>1</wfs:totalUpdated>
<wfs:totalDeleted>0</wfs:totalDeleted>
</wfs:TransactionSummary>
<wfs:TransactionResults/>
<wfs:InsertResults>
<wfs:Feature>
<ogc:FeatureId fid="none"/>
</wfs:Feature>
</wfs:InsertResults>
</wfs:TransactionResponse>
四、总结
- WFS要素更改实现;
- WFS要素更改原理;
转载自:https://blog.csdn.net/xcymorningsun/article/details/84658388