10

I can get a WMS GetCapabilities response via AJAX call and I can convert response to JSON using ol.format.WMSCapabilities tool.

But I need to make a WFS GetCapabilities query via a AJAX request and get a JSON result.

How can I do it?

My GetCapabilities operation metadata is like this:

enter image description here

2

4 Answers 4

4

For versions of the WFS interface standard prior to version 2.0, the only format supported as a response was XML.

From WFS version 2.0 whilst a WFS GetCapabilites defaults to XML a request can now include an AcceptFormats parameter which must include the "text/xml" value, but may include other vendor specified formats.

So you could make a request like:

http://aWFSsever/wfs?request=GetCapabilities&service=WFS&AcceptFormats=application/json,text/xml&

If the server supports WFS 2.0 it will give you a JSON response if it supports it as a GetCapabilites response format, otherwise it will give you XML. If the server is not WFS 2.0 it will ignore the AcceptFormats parameter and give you XML.

7
  • I updated the post with an image of Operations metadata
    – barteloma
    Aug 11, 2017 at 7:54
  • So in this case you can only get XML from the service.
    – nmtoken
    Aug 11, 2017 at 7:57
  • What can I do to support application/json? Installing another geoserver version or change existing geoserver settings?
    – barteloma
    Aug 11, 2017 at 8:01
  • 1
    Not sure if it's possible using geoserver at the moment. That's probably a question worth asking on the geoserver-dev mail list. Otherwise, if you don't need to use WFS 2, you can try the route suggested by @bartvde
    – nmtoken
    Aug 11, 2017 at 8:16
  • 1
    See the capabilities of demo.geo-solutions.it/geoserver/…. The response shows ows:Operation name="GetCapabilities" - ows:Parameter name="AcceptFormats" - ows:Value>text/xml</ows:Value. GeoServer does not support any additional formats for GetCapabilities. You must contact (or contract) developers.
    – user30184
    Aug 11, 2017 at 9:09
2

You can use JSONIX and ogc-schemas for this

const wfsContext = new Jsonix.Context([OWS_1_0_0, Filter_1_1_0, SMIL_2_0, SMIL_2_0_Language, XLink_1_0, GML_3_1_1, WFS_1_1_0]);
const wfsUnmarshaller = wfsContext.createUnmarshaller();

var info = wfsUnmarshaller.unmarshalDocument(xmlhttp.responseXML).value;

See here for more example code: https://github.com/boundlessgeo/sdk/blob/master/src/services/WFSService.js#L80

2

Maybe you can make use of this code on GitHub:

https://github.com/w8r/wms-capabilities

npm install wms-capabilities --save
var WMSCapabilities = require('wms-capabilities');
...
new WMSCapabilities().parse(xmlString);
//or
new WMSCapabilities(xmlString).toJSON();`
1

Here are the detailed steps based on the solution suggested by @bartvde.

Requirements

This solution is supported by the modules jsonix and ogc-schemas.

Setup

mkdir -p ~dev/jsonix
cd ~dev/jsonix
npm init
npm install jsonix
npm install ogc-schemas

Example data

Get a GetCapabilities document from a known server or from your own server, like:

curl -o getcapabilities.xml "https://wssig4.azores.gov.pt/geoserver/ows?request=GetCapabilities&service=WFS&version=2.0.0"

You can also use the test document node_modules/ogc-schemas/tests/WFS/2.0/GetCapabilities-01.xml provided by ogc-schemas.

Minimal code

Write a minimal index.js file.

var Jsonix = require('jsonix').Jsonix;

var XLink_1_0 = require('w3c-schemas').XLink_1_0;
var OWS_1_1_0 = require('ogc-schemas').OWS_1_1_0;
var WFS_2_0 = require('ogc-schemas').WFS_2_0;
var Filter_2_0 = require('ogc-schemas').Filter_2_0;

var context =  new Jsonix.Context([XLink_1_0, OWS_1_1_0, WFS_2_0, Filter_2_0]);
var unmarshaller = context.createUnmarshaller();

// you can use node_modules/ogc-schemas/tests/WFS/2.0/GetCapabilities-01.xml for testing
unmarshaller.unmarshalFile("getcapabilities.xml", function(result) {
    // console.log(result);
    // console.log(result.value.featureTypeList.featureType);
    result.value.featureTypeList.featureType.forEach(function (element, index, array) {
        console.log(element.name.localPart);
    });
});

Running

node index.js | head
pooc_smg_sul_adutoras
pobhl_scd_acessosplanoagua
pobhl_fur_acessosplanoagua
pooc_gra_adutora
pooc_flo_adutora
pooc_cor_adutora
pooc_smg_norte_adutoras
pooc_pic_adutoras
pooc_ter_adutoras
pooc_fai_adutoras

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.