Module: ol/source/Vector

ol/source/Vector


Classes

VectorSourceEvent
VectorSource

Type Definitions

LoadingStrategy()

A function that takes an Extent and a resolution as arguments, and returns an array of Extent with the extents to load. Usually this is one of the standard ol/loadingstrategy strategies.

Options{Object}

Properties:
Name Type Argument Default Description
attributions AttributionLike <optional>

Attributions.

features Array<Feature> | Collection<Feature> <optional>

Features. If provided as Collection, the features in the source and the collection will stay in sync.

format FeatureFormat <optional>

The feature format used by the XHR feature loader when url is set. Required if url is set, otherwise ignored.

loader FeatureLoader <optional>

The loader function used to load features, from a remote source for example. If this is not set and url is set, the source will create and use an XHR feature loader. The 'featuresloadend' and 'featuresloaderror' events will only fire if the success and failure callbacks are used.

Example:

import Vector from 'ol/source/Vector.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import {bbox} from 'ol/loadingstrategy.js';

const vectorSource = new Vector({
  format: new GeoJSON(),
  loader: function(extent, resolution, projection, success, failure) {
     const proj = projection.getCode();
     const url = 'https://ahocevar.com/geoserver/wfs?service=WFS&' +
         'version=1.1.0&request=GetFeature&typename=osm:water_areas&' +
         'outputFormat=application/json&srsname=' + proj + '&' +
         'bbox=' + extent.join(',') + ',' + proj;
     const xhr = new XMLHttpRequest();
     xhr.open('GET', url);
     const onError = function() {
       vectorSource.removeLoadedExtent(extent);
       failure();
     }
     xhr.onerror = onError;
     xhr.onload = function() {
       if (xhr.status == 200) {
         const features = vectorSource.getFormat().readFeatures(xhr.responseText);
         vectorSource.addFeatures(features);
         success(features);
       } else {
         onError();
       }
     }
     xhr.send();
   },
   strategy: bbox,
 });
overlaps boolean <optional>
true

This source may have overlapping geometries. Setting this to false (e.g. for sources with polygons that represent administrative boundaries or TopoJSON sources) allows the renderer to optimise fill and stroke operations.

strategy LoadingStrategy <optional>

The loading strategy to use. By default an all strategy is used, a one-off strategy which loads all features at once.

url string | FeatureUrlFunction <optional>

Setting this option instructs the source to load features using an XHR loader (see xhr). Use a string and an all for a one-off download of all features from the given URL. Use a FeatureUrlFunction to generate the url with other loading strategies. Requires format to be set as well. When default XHR feature loader is provided, the features will be transformed from the data projection to the view projection during parsing. If your remote data source does not advertise its projection properly, this transformation will be incorrect. For some formats, the default projection (usually EPSG:4326) can be overridden by setting the dataProjection constructor option on the format. Note that if a source contains non-feature data, such as a GeoJSON geometry or a KML NetworkLink, these will be ignored. Use a custom loader to load these.

useSpatialIndex boolean <optional>
true

By default, an RTree is used as spatial index. When features are removed and added frequently, and the total number of features is low, setting this to false may improve performance.

Note that getFeaturesInExtent, getClosestFeatureToCoordinate and getExtent cannot be used when useSpatialIndex is set to false, and forEachFeatureInExtent will loop through all features.

When set to false, the features will be maintained in an Collection, which can be retrieved through getFeaturesCollection.

wrapX boolean <optional>
true

Wrap the world horizontally. For vector editing across the -180° and 180° meridians to work properly, this should be set to false. The resulting geometry coordinates will then exceed the world bounds.