[GIS] How to get the geometry type of a WFS vector layer in OpenLayers 3

openlayersvector-layerwfs

I've been trying to get the geometry type of a vector layer OpenLayers 3 with no success. I know that the geometry type can be obtained from a feature, but in my case I have some layers without features (empty layers to start drawing, I need to choose the propper draw interaction).

I've found nothing in the docs, and looking at the ol.layer.Vector and the ol.source.Vector objects in the Chrome console, seems to be some obscure methods and properties like 'A', 'B', 'Fa', 'U', etc. I've been exploring them but I couldn't find wich one has the geometry type, if there is.

I must be missing something… Any clue?

EDIT

And I've realised I'm missing an important thing. The source of the layer is always an WFS layer. I was thinking that the vector layer object was bound to a geometry type (like in most GIS software), and is not, it can contain mixed geometry types. And source object maybe can contain anything too, so that's why there is nothing like a 'getGeometryType' method in ol3? Should I query directly the WFS server?

Best Answer

The geometry type is information you need to retrieve from the WFS server. OpenLayers cannot know. The way to do this is a DescribeFeatureType request. The response is an XML Schema, which will contain the data types for geometry and attributes. For the geometry, you will find something like

<xsd:element maxOccurs="1" minOccurs="0" name="the_geom" nillable="true" type="gml:MultiSurfacePropertyType"/>

In your application, you will need to map from the available GML geometry types to one of the WFS relevant geometry types that OpenLayers knows (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon). In the above snippet, it would be MultiPolygon.

Instead of parsing the XSD manually, you can also try JSONIX and its OGC Schemas extension to get the DescribeFeatureType response as JavaScript object.