WFS1.1.0 Protocol (CRUD) + openlayers4.3.1 Construction + geoserver2.15.1 distal mounting data source configuration deployment + shpfile

WFS Profile

1.WFS ie, Web Feature Service, the full name WebFeatureService. Under GIS, geographic features support for insert, update, delete, search and discovery services.

2 belonging to the communication protocol in OGC standards. GIS services as well as under the OGC standards: WMS, WMTS, WCS and so on.

3. Service according to HTTP client requests return GML (Geography Markup Language, Geography Markup Language) data. WFS corresponds to the common desktop program conditions in the query function, by OGC WFS structure query Filter, support queries based on spatial geometric relationships, query attribute domain-based, of course, common queries based on spatial relations and attribute domains.

 

Soliloquize

Internet has articles on wfs to use it, fragmented, and some on insert, about delete, is not particularly full. These days there is a demand work, setting up the geoserver as soon as possible, fully developed and tested CRUD. Where the query using a get request, other uses of the post. This article have to bring their own, provide a more complete solution, if they can help others, it could not be better.

 

GeoServer installation, deployment

1. In accordance dependent

Dependent on JDK, so to follow this dependence. I installed the 1.8 version, specific version 8u73. After installation, you can not configure the environment variables.

 

2. Install

Installation geoserver2.15.1. Note that the default port number is 8080, proposed changes to the other, to prevent conflicts. Account number and password can use the default. 

                                                       

3. Start

After installation, start the server. Shortcut Start menu bar can be used, you can also use the script file directory.

 

4. Log

After landing the interface:

5. New Workspace

Click on the left "workspace" to add a new work area. name and namespace url, very important, do not free to fill.

6. New Data Source

Click on the left "data storage" to add a new data source, select shpfile type. Point to your shp file. Just choose their new work area of ​​the data source name is not very important. Because final data source name using your shp file name.

7. Preview

创建完毕后,点击左侧的"Layer Preview",在列表中可以看到自己创建的数据源。点击openlayers,可以预览。

8.开启跨域权限

由于GIS应用基本上都构建于其他的服务器,这里必须开启跨域才能访问。打开如下路径的web.xml

 

  需要Uncomment如下代码。可以直接在文本编辑器中搜索Uncomment。

-------------------------------------------------------------------分割线---------------------------------------------------------------------

 9.开启读写权限

回到geoserver配置页,左侧Security→Data,在rule path中,找到*.*.W,然后勾选Grant access to any role。至此全部配置完毕

 

WFS请求

  下面以点要素为例,介绍相关协议。

 1.查询

1     var xhr = new XMLHttpRequest();
2 
3     xhr.open("GET", "http://localhost:9000/geoserver/zzjz/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=zzjz%3AExport_Output&maxFeatures=50&outputFormat=application%2Fjson", true);
4 
5     xhr.onreadystatechange = function () {
6      //请求后的处理
7     };
8 
9     xhr.send();

 

这里的zzjz是工作区名称,typeName是工作区和数据源名称, 以冒号相连,outputFormat是要求服务器返回JSON格式的结果。

请求实际内容:

 

请求成功会见到如下返回response:

 

 

2.新增

 1     var format = new ol.format.WFS();
 2 
 3     var xml = format.writeTransaction([feature], null, null, {
 4         featureNS: "www.zzjz.com",
 5         featurePrefix: "ZZJZ",
 6         featureType: "Export_Output",
 7         srsName: "EPSG:3857"
 8     });
 9 
10     var serializer = new XMLSerializer();
11     var featString = serializer.serializeToString(xml);
12 
13     featString = featString.replace("geometry", "the_geom");
14     featString = featString.replace("geometry", "the_geom");
15     featString = featString.replace("pos", 'coordinates decimal="." cs=" "');
16     featString = featString.replace("pos", 'coordinates');
17     featString = featString.replace(" undefined", '');
18 
19     var xhrInsert = new XMLHttpRequest();
20     xhrInsert.open("POST", "http://localhost:9000/geoserver/zzjz/ows?service=WFS", true);
21     xhrInsert.setRequestHeader('Content-Type', 'text/xml');
22     xhrInsert.send(featString);
23     xhrInsert.onreadystatechange = function () {
24       //请求后的处理  
25     }

 

 这里使用了ol的方法,将feature要素序列化。需要注意的是,需要将geometry标签替换为the_geom标签,将pos标签替换为coordinates标签,同时将高程数据删除。

 

3.删除

 1     var 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">';
 2     xml += '<Delete typeName="ZZJZ:Export_Output" xmlns:ZZJZ="www.zzjz.com">';
 3     xml += '<Filter xmlns="http://www.opengis.net/ogc">'//www.zzjz.com
 4     xml += '<PropertyIsEqualTo>'
 5     xml += '<PropertyName>' + propertySTR + '</PropertyName>'
 6     xml += '<Literal>' + feature.getProperties()[propertySTR] + '</Literal>'
 7     xml += '</PropertyIsEqualTo>'
 8     xml += '</Filter>'
 9     xml += '</Delete>'
10     xml += '</Transaction>'
11     var xhrDelete = new XMLHttpRequest();
12     xhrDelete.open("POST", "http://localhost:9000/geoserver/zzjz/ows?service=WFS", t
13     xhrDelete.setRequestHeader('Content-Type', 'text/xml');
14     xhrDelete.send(xml);
15     xhrDelete.onreadystatechange = function () {
16     }

 

  这里使用了自己构建的GML文本,Filter指向删除条件,PropertyName指向属性名称,Literal指向属性值

 

4.更新(位置信息)

 1     var 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">';
 2     xml += '<Update typeName="ZZJZ:Export_Output" xmlns:ZZJZ="www.zzjz.com">';
 3     xml += '<Property>'
 4     xml += '<Name>the_geom</Name>';
 5     xml += '<Value>'
 6     xml += '<Point xmlns="http://www.opengis.net/gml" srsName="EPSG:3857">';
 7     xml += '<coordinates decimal="." cs=",">'
 8     xml += feature.getGeometry().getCoordinates().toString();
 9     xml += '</coordinates>'
10     xml += '</Point>'
11     xml += '</Value>'
12     xml += '</Property>'
13     xml += '<Filter xmlns="http://www.opengis.net/ogc">'//www.zzjz.com
14     xml += '<PropertyIsEqualTo>'
15     xml += '<PropertyName>' + propertySTR + '</PropertyName>'
16     xml += '<Literal>' + feature.getProperties()[propertySTR] + '</Literal>'
17     xml += '</PropertyIsEqualTo>'
18     xml += '</Filter>'
19     xml += '</Update>'
20     xml += '</Transaction>'
21     var xhrUpdate = new XMLHttpRequest();
22     xhrUpdate.open("POST", "http://localhost:9000/geoserver/zzjz/ows?service=WFS", true);
23     xhrUpdate.setRequestHeader('Content-Type', 'text/xml');
24     xhrUpdate.send(xml);
25     xhrUpdate.onreadystatechange = function () {
26     }

 

 更新位置信息,需要在Update标签下设置两个属性:Property、Filter。一个用于更新属性,一个用于设置条件。Property中的Name固定为“the_geom”,Value为要素信息。

 

5.更新(属性信息)

 1     var 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">';
 2     xml += '<Update typeName="ZZJZ:Export_Output" xmlns:ZZJZ="www.zzjz.com">';
 3     xml += '<Property>'
 4     xml += '<Name>OBJECTID</Name>';
 5     xml += '<Value>'
 6     xml += feature.getProperties()["OBJECTID"]
 7     xml += '</Value>'
 8     xml += '</Property>'
 9     xml += '<Property>'
10     xml += '<Name>heitht</Name>';
11     xml += '<Value>'
12     xml += feature.getProperties()["heitht"]
13     xml += '</Value>'
14     xml += '</Property>'
15     xml += '<Property>'
16     xml += '<Name>remark</Name>';
17     xml += '<Value>'
18     xml += feature.getProperties()["remark"]
19     xml += '</Value>'
20     xml += '</Property>'
21     xml += '<Filter xmlns="http://www.opengis.net/ogc">'//www.zzjz.com
22     xml += '<PropertyIsEqualTo>'
23     xml += '<PropertyName>' + propertySTR + '</PropertyName>'
24     xml += '<Literal>' + feature.getProperties()[propertySTR] + '</Literal>'
25     xml += '</PropertyIsEqualTo>'
26     xml += '</Filter>'
27     xml += '</Update>'
28     xml += '</Transaction>'
29     var xhrUpdate = new XMLHttpRequest();
30     xhrUpdate.open("POST", "http://localhost:9000/geoserver/zzjz/ows?service=WFS", true);
31     xhrUpdate.setRequestHeader('Content-Type', 'text/xml');
32     xhrUpdate.send(xml);
33     // xhrUpdate.send(featString);
34     xhrUpdate.onreadystatechange = function () {
35     }

 

  更新属性信息,需要在Update标签下设置两个属性:Property、Filter。一个用于更新属性,一个用于设置条件。Property中的Name为属性名称,Value为属性值。

 

关于Openlayers

Openlayers4.3.1的版本由于工作需要不能更换,所以构建出的文本信息与当前geoserver默认的协议格式有些不同,所以目前只在insert操作时可以使用ol的接口,直接生成文本。其他操作都必须修改大量请求内容。

个人觉得,不依赖接口反而更容易理解协议规范。

 

 

 

 

Guess you like

Origin www.cnblogs.com/xianerwonder/p/10980569.html