In this chapter, we will cover the following topics:
This chapter talks about vector layers. In addition to raster imagery, vector information is the other important type of data that we can work with in a GIS system. Throughout the coming recipes, we'll summarize the most common and important concepts that you may need to work with in OpenLayers.
We will make some AJAX requests during this chapter. If you're following along with this book's source code, then be sure to download and install Node.js (https://nodejs.org) and follow the README.md instructions.
In GIS, a real-world phenomenon is represented by the concept of a feature. This can be a place, such as a city or a village; it can be a road or a railway, it can be a region, a lake, the border of a country, or something entirely arbitrary.
Features can have a set of attributes, such as population, length, and so on. These can be represented visually through the use of points, lines, polygons, and so on, using some visual style: color, radius, width, and so on.
As you can see, there are many concepts to take into account when working with vector information. Fortunately, OpenLayers provides an extensive range of feature-related classes to work with. We will learn more about these in this chapter.
The base class for vector layers is ol.layer.Vector, which defines the common properties and methods. Most of these are inherited from ol.layer.Base. The vector layer's properties and methods share close similarities with the ol.layer.Tile class, which we've explored in detail in Chapter 2, Adding Raster Layers.
The vector layer class requires a source of ol.source.Vector, in the same way as the raster layers do. The vector source expects a set of features, which can come in numerous formats (GeoJSON, GML, or an OpenLayers-specific type of ol.geom, and so on). These features are converted to the ol.Feature types when added to the layer.
Each feature can contain custom attribute properties, and it will typically have a single geometry. Features can also be individually styled, or they can inherit styles through cascading methods. We'll explore many of the methods that the ol.Feature class has to offer.
In addition to representation onscreen, we need to take into account the data source. OpenLayers offers classes to read/write features from/to many sources or protocols in different formats, such as GML, KML, GeoJSON, WKT, and so on.
Additionally, the vector layer can utilize different feature loading strategies, such as load all features onto the map in one go, load features based on the current extent, and so on.
Without further ado, let's discover and embrace the capabilities of vector layers and features.