一、需求
WFS请求中有个Transaction方法,该方法支持对要素的增删改。可以通过Update、Insert和Delete实现对要素的增删改操作。下面构建一个插入数据的例子。
这里用的是oracle spatial+geoserver+mapbox gl。
二、实例
首先,在oracle中新建一张空间表,注册到元数据表中,并构建空间索引。
//注册到元数据表
INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)
VALUES ('roadline', 'geom', MDSYS.SDO_DIM_ARRAY (
MDSYS.SDO_DIM_ELEMENT('X', 95.0, 122.0, 0.0000001),
MDSYS.SDO_DIM_ELEMENT('Y', 21.5, 44.0, 0.0000001)), 4326);
//构建索引
create index idx_roadline on roadline(geom) indextype is mdsys.spatial_index;
然后通过geoserver将表格发布为图层服务。
最后就是在js中对wfs进行ajax请求,实现往表中插入数据。
function insertData(finaltr){
var insertstr = "";
for(var i = 0; i < finaltr.features.length; i++){
var coors = finaltr.features[i].geometry.coordinates;
var coorstr = "";
for ( var j = 0; j < coors.length; j++){
coorstr = coorstr + coors[j] + " ";
}
insertstr = insertstr + '<wfs:Insert><roadline><GEOM>' +
'<gml:MultiLineString srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">' +
'<gml:lineStringMember><gml:LineString><gml:coordinates decimal="." cs="," ts=" ">' + coorstr +
'</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString>' +
'</GEOM><ID>' + finaltr.features[i].properties.id + '</ID><NAME>' + finaltr.features[i].properties.name +
'</NAME><LEVELS>' + finaltr.features[i].properties.levels + '</LEVELS><ATTRIBUTE>' + finaltr.features[i].properties.attribute +
'</ATTRIBUTE><DIRECTION>' + finaltr.features[i].properties.direction + '</DIRECTION><OBJECTID>' + finaltr.features[i].properties.objectid +
'</OBJECTID><DIR>' + finaltr.features[i].properties.dir + '</DIR><LENGTH>' + finaltr.features[i].properties.length +
'</LENGTH><UP_DOWN>' + finaltr.features[i].properties.up_down + '</UP_DOWN><ROAD_ID>' + finaltr.features[i].properties.road_id +
'</ROAD_ID><ROAD_NAME>' + finaltr.features[i].properties.road_name +'</ROAD_NAME><TYPE_CODE>' + finaltr.features[i].properties.type_code +
'</TYPE_CODE><REGION_ID>' + finaltr.features[i].properties.region_id + '</REGION_ID>' +
'</dz_roadline></wfs:Insert>';
}
var header = '<wfs:Transaction service="WFS" version="1.1.0" ' +
'xmlns:wfs="http://www.opengis.net/wfs" ' +
'xmlns:gml="http://www.opengis.net/gml">'
var endstr = '</wfs:Transaction>';
var data = header + insertstr + endstr;
$.ajax({
type: 'POST',
url: geoJsonUrl,
data: data,
dataType: 'json',
contentType:"text/plain;charset=UTF-8",
success: function(data){
console.log(data)
},
error:function(data){
console.log(data);
},
});
}
三、问题
如果geoserver报错read-only,那么应该是权限的问题,打开geoserver管理界面,点击左侧的Security中的Data栏,点击Rule path中的..w,勾选Grant access to any role下面的单选框,点击保存,清理缓存,再是修改后点击提交,保存成功。