開源GIS(十四)——openlayers通過geoserver中WFS更改要素

目錄

一、引言

二、WFS更改要素實現

三、WFS更改要素原理

1、請求xml

2、postman使用

3、要素修改

4、返回xml

四、總結


一、引言

 

       上文介紹了通過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要素更改原理;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章