Openlayers-3 WFS-T(Post 功能通过地理服务器发布到 postgis)
Openlayers-3 WFS-T (Post feature to postgis via geoserver)
我在通过地理服务器将 ol3 的功能发布到 postgis 数据库时遇到问题 wfs.When 我 运行 这段代码,我只能插入 gid(pk) 和 bin 列,但是 the_geom(几何)列为空。
function addInteraction() {
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
});
draw.on('drawend', function(evt) {
var feature = evt.feature;
feature.set('bin', 0);
var fid = feature.getId();
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "nyc_buildings"
});
$.ajax({
type: "POST",
url: "http://192.168.4.33:9090/geoserver/wfs",
data: new XMLSerializer().serializeToString(node),
contentType: 'text/xml',
success: function(data) {
var result = format.readTransactionResponse(data);
feature.setId(result.insertIds[0]);
},
error: function(e) {
var errorMsg = e? (e.status + ' ' + e.statusText) : "";
bootbox.alert('Error saving this feature to GeoServer.<br><br>'
+ errorMsg);
},
context: this
});
});
map.addInteraction(draw);
}
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-transaction.xsd http://192.168.4.33:9090/geoserver/grp/wfs/DescribeFeatureType?typename=fiware:nyc_buildings">
<Insert>
<nyc_buildings>
<geometry>
<Polygon xmlns="http://www.opengis.net/gml" srsName="EPSG:3857">
<exterior>
<LinearRing srsName="EPSG:3857">
<posList>-12682023.77343518 4567060.841291264 -11077457.675672762 2571137.15870874 -9629434.611838378 5819405.112715591 -12682023.77343518 4567060.841291264
</posList>
</LinearRing>
</exterior>
</Polygon>
</geometry>
<bin>0</bin>
</nyc_buildings>
</Insert>
</Transaction>
我知道它有效的 xml 是:
<wfs:Transaction
service="WFS"
version="1.1.0"
xmlns:fiware="fiware"
xmlns:wfs="http://www.opengis.net/wfs"
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.1.0/WFS-transaction.xsd
http://192.168.4.33:9090/geoserver/grp/wfs /DescribeFeatureType?typename=fiware:nyc_buildings">
<wfs:Insert>
<fiware:nyc_buildings>
<fiware:bin>0</fiware:bin>
<fiware:the_geom>
<gml:MultiSurface srsDimension="2" srsName="http://www.opengis.net/gml/srs/epsg.xml#2908">
<gml:surfaceMember>
<gml:Polygon srsDimension="2">
<gml:exterior>
<gml:LinearRing srsDimension="2">
<gml:posList>988431.501 208900.429 988414.001 208910.222 988393.197 208921.866 988439.703 209005.415 988460.579 208993.729 988481.799 208981.856 988462.619 208948.07 988456.73 208951.37 988442.511 208925.97 988448.961 208922.361 988439.27 208904.93 988435.53 208898.25 988431.501 208900.429</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
</gml:MultiSurface>
</fiware:the_geom>
</fiware:nyc_buildings>
</wfs:Insert>
有什么想法吗?
我想通了,
因为列的名称是 geometry 我必须设置相同的属性 name.So 我所做的是:
draw.on('drawend', function(evt) {
var feature = evt.feature;
feature.set('geometry', feature.getGeometry());
var fid = feature.getId();
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "nyc_buildings"
});
这里是关于 wfs-t 的更完整的代码:
var format = new ol.format.WFS({featureNS:"fiware",featureType:'fw_core',schemaLocation:"http://www.opengis.net/wfs \
http://schemas.opengis.net/wfs/1.1.0/WFS-transaction.xsd \
http://192.168.4.33:9090/geoserver/grp/wfs/DescribeFeatureType?typename=fiware:fw_core"});
function addInteraction() {
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
});
draw.on('drawend', function(evt) {
// create a unique id
// it is later needed to delete features
// give the feature this id
var feature = evt.feature;
feature.set('geometry', feature.getGeometry());
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "fiware:fw_core"
});
$.ajax({
type: "POST",
url: "http://192.168.4.33:9090/geoserver/wfs",
data: new XMLSerializer().serializeToString(node),
contentType: 'text/xml',
success: function(data) {
var result = format.readTransactionResponse(data);
feature.setId(result.insertIds[0]);
},
error: function(e) {
var errorMsg = e? (e.status + ' ' + e.statusText) : "";
bootbox.alert('Error saving this feature to GeoServer.<br><br>'
+ errorMsg);
},
context: this
});
});
map.addInteraction(draw);
}
此外,在 Geoserver 上定义矢量图层时,在发布选项卡中,您必须定义要用作几何列的列。
另一件事,根据 OpenLayers 3 版本,您可能需要在这一行中使用 node.impl 而不仅仅是 node代码:
new XMLSerializer().serializeToString(node.impl)
希望对您有所帮助!
我在通过地理服务器将 ol3 的功能发布到 postgis 数据库时遇到问题 wfs.When 我 运行 这段代码,我只能插入 gid(pk) 和 bin 列,但是 the_geom(几何)列为空。
function addInteraction() {
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
});
draw.on('drawend', function(evt) {
var feature = evt.feature;
feature.set('bin', 0);
var fid = feature.getId();
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "nyc_buildings"
});
$.ajax({
type: "POST",
url: "http://192.168.4.33:9090/geoserver/wfs",
data: new XMLSerializer().serializeToString(node),
contentType: 'text/xml',
success: function(data) {
var result = format.readTransactionResponse(data);
feature.setId(result.insertIds[0]);
},
error: function(e) {
var errorMsg = e? (e.status + ' ' + e.statusText) : "";
bootbox.alert('Error saving this feature to GeoServer.<br><br>'
+ errorMsg);
},
context: this
});
});
map.addInteraction(draw);
}
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-transaction.xsd http://192.168.4.33:9090/geoserver/grp/wfs/DescribeFeatureType?typename=fiware:nyc_buildings">
<Insert>
<nyc_buildings>
<geometry>
<Polygon xmlns="http://www.opengis.net/gml" srsName="EPSG:3857">
<exterior>
<LinearRing srsName="EPSG:3857">
<posList>-12682023.77343518 4567060.841291264 -11077457.675672762 2571137.15870874 -9629434.611838378 5819405.112715591 -12682023.77343518 4567060.841291264
</posList>
</LinearRing>
</exterior>
</Polygon>
</geometry>
<bin>0</bin>
</nyc_buildings>
</Insert>
</Transaction>
我知道它有效的 xml 是:
<wfs:Transaction
service="WFS"
version="1.1.0"
xmlns:fiware="fiware"
xmlns:wfs="http://www.opengis.net/wfs"
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.1.0/WFS-transaction.xsd
http://192.168.4.33:9090/geoserver/grp/wfs /DescribeFeatureType?typename=fiware:nyc_buildings">
<wfs:Insert>
<fiware:nyc_buildings>
<fiware:bin>0</fiware:bin>
<fiware:the_geom>
<gml:MultiSurface srsDimension="2" srsName="http://www.opengis.net/gml/srs/epsg.xml#2908">
<gml:surfaceMember>
<gml:Polygon srsDimension="2">
<gml:exterior>
<gml:LinearRing srsDimension="2">
<gml:posList>988431.501 208900.429 988414.001 208910.222 988393.197 208921.866 988439.703 209005.415 988460.579 208993.729 988481.799 208981.856 988462.619 208948.07 988456.73 208951.37 988442.511 208925.97 988448.961 208922.361 988439.27 208904.93 988435.53 208898.25 988431.501 208900.429</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
</gml:MultiSurface>
</fiware:the_geom>
</fiware:nyc_buildings>
</wfs:Insert>
有什么想法吗?
我想通了, 因为列的名称是 geometry 我必须设置相同的属性 name.So 我所做的是:
draw.on('drawend', function(evt) {
var feature = evt.feature;
feature.set('geometry', feature.getGeometry());
var fid = feature.getId();
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "nyc_buildings"
});
这里是关于 wfs-t 的更完整的代码:
var format = new ol.format.WFS({featureNS:"fiware",featureType:'fw_core',schemaLocation:"http://www.opengis.net/wfs \
http://schemas.opengis.net/wfs/1.1.0/WFS-transaction.xsd \
http://192.168.4.33:9090/geoserver/grp/wfs/DescribeFeatureType?typename=fiware:fw_core"});
function addInteraction() {
draw = new ol.interaction.Draw({
features: featureOverlay.getFeatures(),
type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
});
draw.on('drawend', function(evt) {
// create a unique id
// it is later needed to delete features
// give the feature this id
var feature = evt.feature;
feature.set('geometry', feature.getGeometry());
var node = format.writeTransaction([feature], null, null, {
gmlOptions: {srsName: "EPSG:3857"},
featureNS: "fiware",
featureType: "fiware:fw_core"
});
$.ajax({
type: "POST",
url: "http://192.168.4.33:9090/geoserver/wfs",
data: new XMLSerializer().serializeToString(node),
contentType: 'text/xml',
success: function(data) {
var result = format.readTransactionResponse(data);
feature.setId(result.insertIds[0]);
},
error: function(e) {
var errorMsg = e? (e.status + ' ' + e.statusText) : "";
bootbox.alert('Error saving this feature to GeoServer.<br><br>'
+ errorMsg);
},
context: this
});
});
map.addInteraction(draw);
}
此外,在 Geoserver 上定义矢量图层时,在发布选项卡中,您必须定义要用作几何列的列。
另一件事,根据 OpenLayers 3 版本,您可能需要在这一行中使用 node.impl 而不仅仅是 node代码:
new XMLSerializer().serializeToString(node.impl)
希望对您有所帮助!