0

I am trying to parse this WFS to create an Attribute Table with HTML having the fields as headers and their values as attributes.

I saved the GetFeature response in a variable (features) which I'd like to use for my purpose, but i have no idea how to go on.

I used the readFeatures() method which should return the features as an Array, but from the dev console I see a 0 length Array which I guess is not really what I need.

Here is my code (also the jsfiddle, please look in the console to see the result):

import WFS from 'ol/format/WFS';

export function testWFS() {
    var format, xmlDoc;

    var url = 'https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&REQUEST=Getfeature&TYPENAME=domini_sciabili,impianti_risalita,piste_sci';

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4) {
            format = new WFS({
                featureNS: 'http://www.qgis.org/gml',
            });
            xmlDoc = xmlhttp.response;
            console.log(xmlDoc)
            var features = format.readFeatures({
                source:xmlDoc,
                dataProjection:'EPSG:32632',
            });
            console.log(features)

        }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}
  • 1
    You should read the schema of the attributes and data types with DescribeFeatureType request first. – user30184 Nov 28 '18 at 21:59
  • @user30184 that is surely what I will do. This was a first non-parametric-at-all attempt to "manually" get some result just to have an idea to work on later. Or do you mean "I have to do this" anyway? Thanks for your feedback. – umbe1987 Nov 28 '18 at 22:09
  • 1
    source is a parameter of readFeatures, not an option format.readFeatures(xmlDoc, {dataProjection:'EPSG:32632'}) – Mike Nov 29 '18 at 11:43
  • @Mike Thanks! I was able to make it work, thanks to your observation and adding my EPSG 32632 with proj4. Will answer my own question asap. – umbe1987 Nov 29 '18 at 13:12
1

Thanks to @Mike observation and looking at this OL docs, I was able to read my WFS etFeature succesfully!

Other than using source as the parameter of readfeatures() method rather than an option, the trick was to let OL know about the ESPG 32632.

Here is my working code, hope it will be of interest for anybody.

(new working jsfiddle is here, it will probably change over time due to tests I am making)

import {WFS, GeoJSON} from 'ol/format.js';
import proj4 from 'proj4';
import {register} from 'ol/proj/proj4';
import {get as getProjection} from 'ol/proj';

export function testWFS() {

    // To use other projections, you have to register the projection in OpenLayers.
    // This can easily be done with [https://proj4js.org](proj4)
    //
    // By default OpenLayers does not know about the EPSG:21781 (Swiss) projection.
    // So we create a projection instance for EPSG:32632 and pass it to
    // register to make it available to the library for lookup by its
    // code.
    proj4.defs("EPSG:32632","+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs");
    register(proj4);

    var format, xmlDoc;
    const itaProjection = getProjection('EPSG:32632');

    var url = 'https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&REQUEST=Getfeature&TYPENAME=domini_sciabili,impianti_risalita,piste_sci';

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState === 4) {
            format = new WFS({
                featureNS: 'http://www.qgis.org/gml',
            });
            xmlDoc = xmlhttp.response;
            console.log(xmlDoc)
            var features = format.readFeatures(
                xmlDoc,
                {
                    dataProjection:itaProjection,
                }
            );
            console.log(features)

        }
    };

    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

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.