类:矢量源

ol/source/Vector~矢量源


import VectorSource from 'ol/source/Vector.js';

为矢量图层提供要素源。此源提供的矢量要素适用于编辑。有关针对渲染优化的矢量数据,请参阅 VectorTile

new VectorSource(options)

Name Type 描述
attributions AttributionLike | undefined

版权信息。

features Array.<FeatureType> | Collection<FeatureType> | undefined

要素。若提供为Collection, 源和集合中的要素将保持同步。

format FeatureFormat<FeatureType> | undefined

XHR要素加载器使用的要素格式url已设置。仅在条件满足时必需。url若已设置,则生效;否则忽略。

loader FeatureLoader<FeatureType> | undefined

加载器函数用于加载要素,例如从远程数据源。如果未设置并且url设置后,源将创建并使用XHR要素加载器。这'featuresloadend''featuresloaderror'事件仅在以下条件下触发:successfailure使用回调函数。

示例:

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 (defaults to true)

该数据源可能存在几何图形重叠。将其设置为false(例如,对于代表行政边界的多边形源或 TopoJSON 源)允许渲染器优化填充和描边操作。

strategy LoadingStrategy | undefined

所使用的加载策略。默认情况下all采用一次性加载所有要素的策略。

url string | FeatureUrlFunction | undefined

此选项用于指定数据源使用XHR加载器加载要素(请参阅xhr)。使用stringall从给定URL批量下载所有要素。使用FeatureUrlFunction通过其他加载策略构造URL。同时需要设置format。当使用默认的XHR要素加载器时,要素会在解析期间从数据投影转换到视图投影。如果远程数据源未正确声明投影,转换可能不准确。对于某些格式,可以通过设置格式类的 `dataProjection` 构造函数选项来覆盖默认投影(通常为 EPSG:4326)。请注意,如果数据源包含非要素数据(例如 GeoJSON 几何图形或 KML NetworkLink),这些内容将被忽略。请使用自定义加载器加载这些数据。

useSpatialIndex boolean (defaults to true)

默认情况下,使用R树作为空间索引。当要素被频繁移除和添加,且要素总数较少时,应将其设置为false可能提升性能。

请注意getFeaturesInExtent, getClosestFeatureToCoordinategetExtent无法使用时useSpatialIndex设置为false,和forEachFeatureInExtent将遍历所有要素。

当设置为false,这些要素将保留在Collection,可通过检索getFeaturesCollection.

wrapX boolean (defaults to true)

水平环绕世界。为使矢量编辑在跨越 -180° 和 180° 经线时正常工作,应将其设置为false由此产生的几何坐标将超出世界边界。

触发事件:

子类

继承

方法

addFeature(feature)

向源中添加单个要素。若需一次性添加一批要素,请改为调用 #addFeatures()。若具有相同 ID 的要素已存在,则该要素不会被添加到源中。此行为旨在避免在使用 bbox 或瓦片加载策略时出现要素重复。注意:若使用 Collection 来管理要素,此规则同样适用,即若在集合中添加了 ID 重复的要素,该要素将立即从集合中移除。

Name Type 描述
feature FeatureType

待添加的要素。

addFeatures(features)

向源添加一批要素。

Name Type 描述
features Array.<FeatureType>

待添加要素。

递增修订计数器并触发更改事件。

clear(fast)

从源中移除所有要素。

Name Type 描述
fast boolean | undefined

跳过调度removefeature事件.

dispatchEvent(event){boolean | undefined} inherited

触发一个事件并调用所有监听该类型事件的侦听器。事件参数可以是字符串或具有 type 属性的对象。

Name Type 描述
event BaseEvent | string

事件对象。

返回:
false 如果事件对象上调用了 preventDefault,或者任何侦听器返回 false。

forEachFeature(callback){T | undefined}

遍历源中的所有要素,并对每个要素调用提供的回调函数。如果回调返回任何真值,迭代将停止,并且函数将返回该值。注意:此函数仅遍历具有已定义几何的要素。

Name Type 描述
callback function

针对源上的每个要素调用。返回真值以停止迭代。

返回:
最后一次回调调用的返回值。

forEachFeatureInExtent(extent, callback){T | undefined}

遍历所有边界框与给定范围相交的要素(注意:要素的几何图形未必与该范围相交),并对每个要素调用回调函数。若回调函数返回一个“真值”,则迭代终止,且函数将返回该值。

若需获取几何与范围相交的要素,请改用 #forEachFeatureIntersectingExtent() 方法。

useSpatialIndex 设置为 false 时,该方法将遍历所有要素,等效于 #forEachFeature()

Name Type 描述
extent Extent

范围.

callback function

对每个边界框与所提供范围相交的要素进行调用。

返回:
最后一次回调调用的返回值。

forEachFeatureIntersectingExtent(extent, callback){T | undefined}

遍历所有几何与所提供范围相交的要素,对每个要素调用回调函数。如果回调返回真值,迭代将停止,且函数将返回该值。

若仅需检测边界框相交,请调用 #forEachFeatureInExtent() 方法。

Name Type 描述
extent Extent

范围.

callback function

调用几何与所提供范围相交的每个要素。

返回:
回调函数最后一次调用的返回值。

获取值。

Name Type 描述
key string

键名。

返回:
值。

获取数据源的归属函数。

返回:
归属功能。

getAttributionsCollapsible(){boolean} inherited

返回:
版权信息可折叠。

getClosestFeatureToCoordinate(coordinate, filter){FeatureType | null}

获取距离给定坐标最近的要素。

当数据源将useSpatialIndex设置为false,且该数据源中的要素类型为Feature时,此方法不可用。

Name Type 描述
coordinate Coordinate

坐标。

filter function | undefined

要素过滤功能。 过滤函数将接收一个参数,即该要素。feature应返回布尔值。默认不进行任何过滤。

返回:
最近的要素(如果未找到,则为 null)。

getExtent(extent){Extent}

获取源中当前要素的范围。

当数据源将 useSpatialIndex 设置为 false 时,此方法不可用。

Name Type 描述
extent Extent | undefined

目标范围:如果指定,则不会创建新范围,而是覆盖该范围的坐标。

返回:
范围.

getFeatureById(id){FeatureClassOrArrayOfRenderFeatures<FeatureType> | null}

通过要素标识符(即 feature.getId() 返回的值)获取要素。使用 RenderFeatures 时,getFeatureById() 能够返回一个 RenderFeatures 数组。这允许处理 GeometryCollection 几何体:格式读取器会为每个 GeometryCollection 成员创建一个 RenderFeature。请注意,索引将字符串和数字标识符视为相同。因此,source.getFeatureById(2) 将返回 id 为 '2'2 的要素。

Name Type 描述
id string | number

要素标识符。

返回:
要素(未找到时返回 null)。

getFeatures(){Array.<FeatureType>}

以随机顺序获取数据源上当前要素的快照。返回的数组是副本,其中的要素则是对数据源中要素的引用。

返回:
要素。

getFeaturesAtCoordinate(coordinate){Array.<FeatureType>}

获取几何与给定坐标相交的所有要素。

Name Type 描述
coordinate Coordinate

坐标

返回:
要素。

getFeaturesCollection(){Collection<FeatureType> | null}

获取与此数据源关联的要素集合。它将是 null,除非数据源配置时 useSpatialIndex 设为 falseCollection 设为 features

返回:
要素集。

getFeaturesInExtent(extent, projection){Array.<FeatureType>}

获取所有边界框与所提供范围相交的要素。请注意,这会以随机顺序返回与给定范围相交的所有要素数组(因此可能包括几何不与范围相交的要素)。

useSpatialIndex 设置为 false 时,此方法将返回所有要素。

Name Type 描述
extent Extent

范围.

projection Projection | undefined

包含满足以下条件的要素extent超出 x 轴边界projection并环绕全球。

返回:
要素。

getFormat(){FeatureFormat<FeatureType> | null}

获取与此数据源关联的格式。

返回:
} 要素格式。

getKeys(){Array.<string>} inherited

获取对象的属性名称列表

返回:
属性名称列表。

getProjection(){Projection | null} inherited

获取数据源的投影。

返回:
投影.

getProperties(){Object.<string, *>} inherited

获取包含所有属性名及其值的对象。

返回:
对象。

getRevision(){number} inherited

获取该对象的版本号。每次修改对象时,其版本号将递增。

返回:
修订。

获取源的状态,请参阅 State 了解可能的状态值。

返回:
状态。

getUrl(){string | FeatureUrlFunction | undefined}

获取与此源关联的URL。

返回:
URL。

hasFeature(feature){boolean}

若要素包含于源中,则返回true。

Name Type 描述
feature FeatureType

要素.

返回:
具有要素。

on(type, listener){EventsKey | Array<EventsKey>} inherited

监听特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组

listener function

监听器函数。

返回:
监听器的唯一键。若以事件类型数组作为第一个参数调用,则返回键的数组。

once(type, listener){EventsKey | Array<EventsKey>} inherited

一次性监听特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组。

listener function

监听器函数。

返回:
监听器的唯一键。若以事件类型数组作为第一个参数调用,则返回键的数组。

removeFeature(feature)

从源中移除单个要素。若要批量移除要素,请改用 #removeFeatures() 方法。

Name Type 描述
feature FeatureType

要删除的要素。

removeFeatures(features)

从数据源中批量删除要素。如果您想一次性删除所有要素,请改用 #clear() 方法。

Name Type 描述
features Array.<FeatureType>

要删除的要素。

removeLoadedExtent(extent)

从已加载范围列表中移除一个范围。

Name Type 描述
extent Extent

范围.

set(key, value, silent) inherited

设置一个值。

Name Type 描述
key string

键名。

value *

值。

silent boolean | undefined

在不触发事件的情况下进行更新。

setAttributions(attributions) inherited

设置源的归属。

Name Type 描述
attributions AttributionLike | undefined

版权归属。可以作为string, Array<string>, Attributionundefined.

setLoader(loader)

设置数据源的新加载器。下一个渲染周期将使用新的加载器。

Name Type 描述
loader FeatureLoader

待设置的加载器。

setProperties(values, silent) inherited

设置一组键值对。请注意,此操作将修改现有属性并添加新属性,但不会删除任何现有属性。

Name Type 描述
values Object.<string, *>

值。

silent boolean | undefined

无事件触发的更新

将源指向新的 URL。下一个渲染循环将使用新的 URL。

Name Type 描述
url string | FeatureUrlFunction

资源地址。

un(type, listener) inherited

取消监听特定类型的事件。

Name Type 描述
type string | Array.<string>

事件类型或事件类型数组。

listener function

监听器函数。

unset(key, silent) inherited

清除属性。

Name Type 描述
key string

键名。

silent boolean | undefined

在不触发事件的情况下取消设置。