OpenLayers4 – Reading Features from GML

gmlopenlayers

I have an XML data. I simply need to get coordinates of a feature from XML to zoom the map on it.

But when I try to read it I get nothing. What am I doing wrong? Have any idea?

//got a text  from a request to gis server as below:
 var str = '<?xml version="1.0" encoding="UTF-8"?><wfs:FeatureCollection 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:wfs="http://www.opengis.net/wfs/2.0" 
 xmlns="http://www.w3.org/2001/XMLSchema" 
 xmlns:xlink="http://www.w3.org/1999/xlink" 
 xmlns:gml="http://www.opengis.net/gml" 
 xmlns:gmd="http://www.isotc211.org/2005/gmd" 
 xmlns:mapVHU.rsc.12058="http://www.gisinfo.net/bsd/mapVHU.rsc.12058" 
 numberMatched="1" numberReturned="1" 
 xsi:schemaLocation="http://www.gisinfo.net/bsd/mapVHU.rsc.12058 
 mapVHU.xsd"> <gml:boundedBy> <gml:Envelope 
 srsName="urn:ogc:def:crs:EPSG:4326"> <gml:lowerCorner>41.02808922 
 -180.15870228</gml:lowerCorner> <gml:upperCorner>82.01560154 
 180.06887075</gml:upperCorner> </gml:Envelope> </gml:boundedBy> 
 <wfs:member> <mapVHU.rsc.12058:SYSTEM gml:id="allVHU.9"> 
 <gml:name>ВЫДЕЛЕНИЕ ЛИНЕЙНЫХ</gml:name> 
 <mapVHU.rsc.12058:SYSTEMCode>1000000001</mapVHU.rsc.12058:SYSTEMCode> 
 <gml:LineString srsName="urn:ogc:def:crs:EPSG:4326"> <gml:posList 
 srsDimension="2" count="58"> 43.92811586 45.63285837 43.91771241 
 45.55451322 43.84148340 45.34714622 43.80674441 45.09444822 43.84967541 
 44.91506822 43.92780941 44.62431522 43.92499941 44.34742122 43.87101840      
 43.74673321 43.88635440 43.51159021 43.87947041 43.40681922 43.84722896 
 43.36282796 43.85090581 43.36418306 43.87214417 43.37795231 43.89952296 
 43.40357221 43.91278368 43.42997706 43.92479151 43.45814980 43.93508821 
 43.47985288 43.94980785 43.50694254 43.96791550 43.54702526 43.97443033 
 43.57746422 43.98643243 43.59882346 44.00585334 43.62000321 44.01833104 
 43.64275075 44.02412704 43.64545032 44.02758242 43.66669646 44.02509628 
 43.70285931 44.02410582 43.79336135 44.04966196 43.84050505 44.08608963 
 43.88658105 44.14628088 43.92243434 44.17613869 43.94777575 44.19335338 
 43.95549728 44.24353493 44.02750536 44.26770706 44.10141275 44.29641904 
 44.16546897 44.34186963 44.26883336 44.38990965 44.31490970 44.42471828 
 44.36705473 44.43238883 44.41278945 44.42675582 44.44787523 44.42546751 
 44.46989389 44.40656549 44.57422217 44.39542809 44.60976242 44.39963246 
 44.66451116 44.41165852 44.70935827 44.40991200 44.75382525 44.39559293 
 44.78485524 44.38560314 44.84055077 44.34979044 44.95503270 44.31666166 
 45.04203254 44.21718468 45.23173612 44.15116032 45.33297681 44.12446238 
 45.41820344 44.11970305 45.48074964 44.08822204 45.51087142 43.99643708 
 45.55871524 43.94313541 45.61438893 43.92811586 45.63285837 
 </gml:posList> <gml:metaDataProperty> <gml:GenericMetaData> <MapID/> 
 </gml:GenericMetaData> </gml:metaDataProperty> </gml:LineString> 
 </mapVHU.rsc.12058:SYSTEM> </wfs:member> </wfs:FeatureCollection>'

When I try to read it like this example – https://jsfiddle.net/d08s532r/27/

 //try to parse
 var format = new ol.format.GML(); 
 var feature = format.readFeatures(str, 
 {featureName:'member',featureProjection: 'EPSG:4326'})
 //resulting array is empty
 console.log("feature:");
 console.log(feature);

 //need to get coordinates but can't do it 
 var ext = feature[0].getGeometries().getCoordinates();   

I tried to parse the text from server into xml and read it, but result was the same. Is it some problem with my xml data?

Best Answer

It will not work as you are using WFS 2.0 and default support for the feature parser is WFS 1.1.0 (you can choose also WFS 1.0.0) as stated in the WFS doc You may not see why I speak about WFS version. It's because each WFS version support a GML version by default

  • WFS 1.0.0 contains GML 2.1.2 (both WFS & GML version supported in OpenLayers)
  • WFS 1.1.0 contains GML 3.1.1 (both WFS & GML version supported in OpenLayers)
  • WFS 2.0.0 contains GML 3.2.1 (both WFS & GML version unsupported in OpenLayers)

You may want to cheat to avoid the issue with WFS 2.0.0 version:

  • using "outputFormat=application/json" that return a GeoJSON instead of an unreadable GML like in this official demo. This solution will work out of the box for GeoServer or for Mapserver, play with configuration in the mapfile
  • change WFS version from 2.0.0 to 1.1.0 in your WFS call and OpenLayers will parse correctly the features
  • change outpuformat to get features with a GML format supported by OpenLayers (for ArcGIS Server, for GeoServer)

You can go the 1st way or the 3rd if using ArcGis Server 10.5 or more (I've seen your issue on OpenLayers repo so I know more or less your context). Otherwise, you will have to go the 2nd way or you may also consider consuming ArcGIS REST Feature Service

Related Question