Class: VectorSource

ol/source/Vector~VectorSource


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

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

new VectorSource(options)

Name Type Description
attributions AttributionLike | undefined

归属信息。

features Array.<要素Type> | Collection<要素Type> | undefined

要素集合。如果作为 Collection 提供,数据源中的要素和集合将保持同步。

format 要素Format<要素Type> | undefined

设置 url 时 XHR 要素加载器使用的要素格式。如果设置了 url 则必需,否则忽略。

loader 要素Loader<要素Type> | 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({
  loader: async (extent, resolution, projection) => {
     const url = 'https://ahocevar.com/geoserver/wfs?service=WFS&' +
         'version=1.1.0&request=Get要素&typename=osm:water_areas&' +
         'outputFormat=application/json' +
         'bbox=' + extent.join(',') + ',' + projection.getCode();
     const response = await fetch(url);
     if (!response.ok) {
       throw new Error('Network response was not ok');
     }
     const json = await response.json();
     const features = new GeoJSON().read要素s(json, {
       featureProjection: projection,
     });
     return features;
   },
   strategy: bbox,
 });

当您想要重试失败的请求时,请使用

vector数据源。removeLoadedExtent(extent);
vector数据源。changed();
overlaps boolean (defaults to true)

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

strategy LoadingStrategy | undefined

要使用的加载策略。默认使用 all 策略,即一次性加载所有要素的策略。

url string | featureUrlFunction | undefined

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

默认情况下,使用 RTree 作为空间索引。当要素频繁添加和移除,且要素总数较少时,将此设置为 false 可能会提高性能。

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

设置为 false 时,要素将保存在 Collection 中,可通过 getFeaturesCollection 获取。

wrapX boolean (defaults to true)

水平包裹世界。要使跨越 -180° 和 180° 经线的矢量编辑正常工作,应将其设置为 false。生成的几何坐标将超出世界边界。

触发事件:

Subclasses

Extends

Methods

add要素(feature)

向数据源添加单个要素。如果要一次添加一批要素,请改用 #add要素s()。如果具有相同 id 的要素已存在,则不会将该要素添加到数据源。此行为的原因是避免使用 bbox 或瓦片加载策略时要素重复。注意:如果要素使用 Collection,这也适用,意味着如果添加了重复 id 的要素到集合中,它将立即被移除。

Name Type Description
feature 要素Type

要添加的要素。

add要素s(features)

向数据源添加一批要素。

Name Type Description
features Array.<要素Type>

要素s to add.

增加修订计数器并分发 'change' 事件。

clear(fast)

从数据源移除所有要素。

Name Type Description
fast boolean | undefined

跳过分发 removefeature 事件。

dispatchEvent(event){boolean | undefined} inherited

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

Name Type Description
event BaseEvent | string

事件对象。

返回值:
如果有人在事件对象上调用了 preventDefault 或任何监听器返回 false,则返回 false

forEach要素(callback){T | undefined}

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

Name Type Description
callback function

对数据源中的每个要素调用。返回真值以停止遍历。

返回值:
The return value from the last call to the callback.

forEachFeatureInExtent(extent, callback){T | undefined}

遍历所有边界框与提供范围相交的要素(注意要素的几何对象可能不与范围相交),对每个要素调用回调函数。如果回调返回"真值",迭代将停止并返回相同的值。

如果您对几何图形与范围相交的要素感兴趣,请改用 #forEach要素IntersectingExtent() 方法。

useSpatialIndex 设置为 false 时,此方法将遍历所有要素,等同于 #forEach要素()

Name Type Description
extent Extent

范围。

callback function

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

返回值:
The return value from the last call to the callback.

forEach要素IntersectingExtent(extent, callback){T | undefined}

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

如果您只想测试边界框相交,请改用 #forEachFeatureInExtent() 方法。

Name Type Description
extent Extent

范围。

callback function

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

返回值:
The return value from the last call to the callback.

获取一个值。

Name Type Description
key string

键名。

返回值:
值。

获取数据源的归属函数。

返回值:
Attribution function.

getAttributionsCollapsible(){boolean} inherited

返回值:
归属信息是否可折叠。

getClosestFeatureToCoordinate(coordinate, filter){要素Type | null}

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

当数据源配置 useSpatialIndexfalse 且此数据源中的要素类型为 要素 时,此方法不可用。

Name Type Description
coordinate Coordinate

坐标。

filter function | undefined

要素过滤函数。过滤函数将接收一个参数 feature,并应返回布尔值。默认不进行过滤。

返回值:
Closest feature (or null if none found).

getExtent(extent){Extent | null}

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

当数据源配置 useSpatialIndexfalse 时,此方法将返回 null

Name Type Description
extent Extent | undefined

目标范围。如果提供,将不创建新范围,而是覆盖该范围的坐标。

返回值:
范围。

get要素ById(id){要素ClassOrArrayOf渲染要素s<要素Type> | null}

通过标识符获取要素(feature.getId() 返回的值)。当使用 渲染要素 时,get要素ById() 可以返回 渲染要素 数组。这允许处理 GeometryCollection 几何图形,其中格式读取器为每个 GeometryCollection 成员创建一个 渲染要素。注意索引将字符串和数字标识符视为相同。因此 source.get要素ById(2) 将返回 id 为 '2'2 的要素。

Name Type Description
id string | number

要素标识符。

返回值:
The feature (or null if not found).

getFeatures(){Array.<要素Type>}

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

返回值:
要素集合。

getFeaturesAtCoordinate(coordinate){Array.<要素Type>}

获取几何图形与提供的坐标相交的所有要素。

Name Type Description
coordinate Coordinate

坐标。

返回值:
要素集合。

getFeaturesCollection(){Collection<要素Type> | null}

获取与此数据源关联的要素集合。除非数据源配置时将 useSpatialIndex 设置为 false,或使用 Collection 作为 features,否则将为 null。

返回值:
The collection of features.

getFeaturesInExtent(extent, projection){Array.<要素Type>}

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

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

Name Type Description
extent Extent

范围。

projection Projection | undefined

包含范围超出投影 x 轴边界并环绕世界的要素。

返回值:
要素集合。

getFormat(){要素Format<要素Type> | null}

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

返回值:
} The feature format.

getKeys(){Array.<string>} inherited

获取对象属性名列表。

返回值:
属性名列表。

getProjection(){Projection | null} inherited

获取数据源的投影。

返回值:
投影。

getProperties(){NoInfer.<Properties>} inherited

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

返回值:
对象。

getRevision(){number} inherited

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

返回值:
版本号。

获取数据源的状态,可能的状态参见 State。

返回值:
状态。

getUrl(){string | featureUrlFunction | undefined}

获取url associated with this source.

返回值:
The url.

has要素(feature){boolean}

返回要素是否包含在数据源中。

Name Type Description
feature 要素Type

要素。

返回值:
Has feature.

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

监听特定类型的事件。

Name Type Description
type string | Array.<string>

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

listener function

监听器函数。

返回值:
监听器的唯一键。如果第一个参数是事件类型数组,则返回键的数组。

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

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

Name Type Description
type string | Array.<string>

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

listener function

监听器函数。

返回值:
监听器的唯一键。如果第一个参数是事件类型数组,则返回键的数组。

remove要素(feature)

从数据源移除单个要素。如果要批量移除要素,请改用 #remove要素s() 方法。

Name Type Description
feature 要素Type

要移除的要素。

remove要素s(features)

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

Name Type Description
features Array.<要素Type>

要素s to remove.

removeLoadedExtent(extent)

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

Name Type Description
extent Extent

范围。

set(key, value, silent) inherited

设置一个值。

Name Type Description
key string

键名。

value *

值。

silent boolean | undefined

更新但不触发事件。

setAttributions(attributions) inherited

设置数据源的归属信息。

Name Type Description
attributions AttributionLike | undefined

归属信息。可以作为字符串、Array<string>、Attribution 或 undefined 传入。

setLoader(loader)

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

Name Type Description
loader 要素Loader

要设置的加载器。

setProperties(values, silent) inherited

设置一组键值对。 注意,这会更改任何现有属性并添加新属性(不会移除任何现有属性)。

Name Type Description
values Partial.<NoInfer.<Properties>>

值。

silent boolean | undefined

更新但不触发事件。

将数据源指向新的 URL。下一个渲染周期将使用新的 URL。

Name Type Description
url string | featureUrlFunction

Url.

un(type, listener) inherited

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

Name Type Description
type string | Array.<string>

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

listener function

监听器函数。

unset(key, silent) inherited

取消设置一个属性。

Name Type Description
key string

键名。

silent boolean | undefined

取消设置但不触发事件。