4

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?

2 Answers 2

4

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.

1

What you experiencing in the dev console, is the result of compiling the -- otherwise very large and segmented -- code base into a single, few hundred KBs JS file. For example ol.layer.Vector can become A in the compiling process to save 14 characters every time ol.layer.Vector is called or defined in the source code.

To answer your question, I did not find such option, which restricts the geometry type of a layer, like geometryType in OL2. Nor a method, which iterates through every feature, and decides if a layer has a specific type, or stores a collection of geometries. As a result, you have to write a small routine, which checks every feature in a layer. You can get the features from the layer's source, which returns an array of features:

var arrOfFeatures = layer.getSource().getFeatures();

Then you can check the type of every feature's geometry:

arrOfFeatures[i].getGeometry().getType();

which returns a string with one of the supported types. As a sidenote, an ol.Feature can exist without a geometry, thus if you decide to write a general routine, you should handle "empty" features as well.

3
  • The problem is when I have no features. 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? Commented Jul 27, 2016 at 10:18
  • I'm not sure if querying the type of a layer is possible with WFS, although I did not read the specification thoroughly, especially the 2.0.0 one. If you can do such a query, that is definitely an option. Or you can write your own getGeometryType method as outlined in my answer. Commented Jul 27, 2016 at 10:45
  • Layers may not have geometry type associated as layers may have multi type geometries. Means one layer may hold lines, polygons and points within the same DB table. Of course you can limit the geometry type, each table holding, within the DB using metadata etc (depends on the DB you use on the back end) and so you can make a DB query to verify the geometry type this table accepts.
    – pavlos
    Commented Jul 27, 2016 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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