Make the vector source geometry generic
This commit is contained in:
@@ -35,12 +35,13 @@ import RBush from '../structs/RBush.js';
|
||||
* @classdesc
|
||||
* Events emitted by {@link module:ol/source/Vector} instances are instances of this
|
||||
* type.
|
||||
* @template {import("../geom/Geometry.js").default} Geometry
|
||||
*/
|
||||
export class VectorSourceEvent extends Event {
|
||||
|
||||
/**
|
||||
* @param {string} type Type.
|
||||
* @param {import("../Feature.js").default=} opt_feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>=} opt_feature Feature.
|
||||
*/
|
||||
constructor(type, opt_feature) {
|
||||
|
||||
@@ -48,7 +49,7 @@ export class VectorSourceEvent extends Event {
|
||||
|
||||
/**
|
||||
* The feature being added or removed.
|
||||
* @type {import("../Feature.js").default|undefined}
|
||||
* @type {import("../Feature.js").default<Geometry>|undefined}
|
||||
* @api
|
||||
*/
|
||||
this.feature = opt_feature;
|
||||
@@ -154,8 +155,9 @@ export class VectorSourceEvent extends Event {
|
||||
* by this source are suitable for editing. See {@link module:ol/source/VectorTile~VectorTile} for
|
||||
* vector data that is optimized for rendering.
|
||||
*
|
||||
* @fires VectorSourceEvent
|
||||
* @fires VectorSourceEvent<Geometry>
|
||||
* @api
|
||||
* @template {import("../geom/Geometry.js").default} Geometry
|
||||
*/
|
||||
class VectorSource extends Source {
|
||||
/**
|
||||
@@ -215,7 +217,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {RBush<import("../Feature.js").default>}
|
||||
* @type {RBush<import("../Feature.js").default<Geometry>>}
|
||||
*/
|
||||
this.featuresRtree_ = useSpatialIndex ? new RBush() : null;
|
||||
|
||||
@@ -227,21 +229,21 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {!Object<string, import("../Feature.js").default>}
|
||||
* @type {!Object<string, import("../Feature.js").default<Geometry>>}
|
||||
*/
|
||||
this.nullGeometryFeatures_ = {};
|
||||
|
||||
/**
|
||||
* A lookup of features by id (the return from feature.getId()).
|
||||
* @private
|
||||
* @type {!Object<string, import("../Feature.js").default>}
|
||||
* @type {!Object<string, import("../Feature.js").default<Geometry>>}
|
||||
*/
|
||||
this.idIndex_ = {};
|
||||
|
||||
/**
|
||||
* A lookup of features without id (keyed by getUid(feature)).
|
||||
* @private
|
||||
* @type {!Object<string, import("../Feature.js").default>}
|
||||
* @type {!Object<string, import("../Feature.js").default<Geometry>>}
|
||||
*/
|
||||
this.undefIdIndex_ = {};
|
||||
|
||||
@@ -253,7 +255,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {Collection<import("../Feature.js").default>}
|
||||
* @type {Collection<import("../Feature.js").default<Geometry>>}
|
||||
*/
|
||||
this.featuresCollection_ = null;
|
||||
|
||||
@@ -285,7 +287,7 @@ class VectorSource extends Source {
|
||||
* Note: this also applies if an {@link module:ol/Collection} is used for features,
|
||||
* meaning that if a feature with a duplicate id is added in the collection, it will
|
||||
* be removed from it right away.
|
||||
* @param {import("../Feature.js").default} feature Feature to add.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature to add.
|
||||
* @api
|
||||
*/
|
||||
addFeature(feature) {
|
||||
@@ -296,7 +298,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Add a feature without firing a `change` event.
|
||||
* @param {import("../Feature.js").default} feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature.
|
||||
* @protected
|
||||
*/
|
||||
addFeatureInternal(feature) {
|
||||
@@ -328,7 +330,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* @param {string} featureKey Unique identifier for the feature.
|
||||
* @param {import("../Feature.js").default} feature The feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature The feature.
|
||||
* @private
|
||||
*/
|
||||
setupChangeEvents_(featureKey, feature) {
|
||||
@@ -343,7 +345,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* @param {string} featureKey Unique identifier for the feature.
|
||||
* @param {import("../Feature.js").default} feature The feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature The feature.
|
||||
* @return {boolean} The feature is "valid", in the sense that it is also a
|
||||
* candidate for insertion into the Rtree.
|
||||
* @private
|
||||
@@ -368,7 +370,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Add a batch of features to the source.
|
||||
* @param {Array<import("../Feature.js").default>} features Features to add.
|
||||
* @param {Array<import("../Feature.js").default<Geometry>>} features Features to add.
|
||||
* @api
|
||||
*/
|
||||
addFeatures(features) {
|
||||
@@ -379,7 +381,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Add features without firing a `change` event.
|
||||
* @param {Array<import("../Feature.js").default>} features Features.
|
||||
* @param {Array<import("../Feature.js").default<Geometry>>} features Features.
|
||||
* @protected
|
||||
*/
|
||||
addFeaturesInternal(features) {
|
||||
@@ -420,14 +422,14 @@ class VectorSource extends Source {
|
||||
|
||||
|
||||
/**
|
||||
* @param {!Collection<import("../Feature.js").default>} collection Collection.
|
||||
* @param {!Collection<import("../Feature.js").default<Geometry>>} collection Collection.
|
||||
* @private
|
||||
*/
|
||||
bindFeaturesCollection_(collection) {
|
||||
let modifyingCollection = false;
|
||||
listen(this, VectorEventType.ADDFEATURE,
|
||||
/**
|
||||
* @param {VectorSourceEvent} evt The vector source event
|
||||
* @param {VectorSourceEvent<Geometry>} evt The vector source event
|
||||
*/
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
@@ -438,7 +440,7 @@ class VectorSource extends Source {
|
||||
});
|
||||
listen(this, VectorEventType.REMOVEFEATURE,
|
||||
/**
|
||||
* @param {VectorSourceEvent} evt The vector source event
|
||||
* @param {VectorSourceEvent<Geometry>} evt The vector source event
|
||||
*/
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
@@ -454,7 +456,7 @@ class VectorSource extends Source {
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
modifyingCollection = true;
|
||||
this.addFeature(/** @type {import("../Feature.js").default} */ (evt.element));
|
||||
this.addFeature(/** @type {import("../Feature.js").default<Geometry>} */ (evt.element));
|
||||
modifyingCollection = false;
|
||||
}
|
||||
}, this);
|
||||
@@ -465,7 +467,7 @@ class VectorSource extends Source {
|
||||
function(evt) {
|
||||
if (!modifyingCollection) {
|
||||
modifyingCollection = true;
|
||||
this.removeFeature(/** @type {import("../Feature.js").default} */ (evt.element));
|
||||
this.removeFeature(/** @type {import("../Feature.js").default<Geometry>} */ (evt.element));
|
||||
modifyingCollection = false;
|
||||
}
|
||||
}, this);
|
||||
@@ -518,7 +520,7 @@ class VectorSource extends Source {
|
||||
* stop and the function will return the same value.
|
||||
* Note: this function only iterate through the feature that have a defined geometry.
|
||||
*
|
||||
* @param {function(import("../Feature.js").default): T} callback Called with each feature
|
||||
* @param {function(import("../Feature.js").default<Geometry>): T} callback Called with each feature
|
||||
* on the source. Return a truthy value to stop iteration.
|
||||
* @return {T|undefined} The return value from the last call to the callback.
|
||||
* @template T
|
||||
@@ -540,7 +542,7 @@ class VectorSource extends Source {
|
||||
* value.
|
||||
*
|
||||
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
|
||||
* @param {function(import("../Feature.js").default): T} callback Called with each feature
|
||||
* @param {function(import("../Feature.js").default<Geometry>): T} callback Called with each feature
|
||||
* whose goemetry contains the provided coordinate.
|
||||
* @return {T|undefined} The return value from the last call to the callback.
|
||||
* @template T
|
||||
@@ -571,7 +573,7 @@ class VectorSource extends Source {
|
||||
* features, equivalent to {@link module:ol/source/Vector~VectorSource#forEachFeature #forEachFeature()}.
|
||||
*
|
||||
* @param {import("../extent.js").Extent} extent Extent.
|
||||
* @param {function(import("../Feature.js").default): T} callback Called with each feature
|
||||
* @param {function(import("../Feature.js").default<Geometry>): T} callback Called with each feature
|
||||
* whose bounding box intersects the provided extent.
|
||||
* @return {T|undefined} The return value from the last call to the callback.
|
||||
* @template T
|
||||
@@ -595,7 +597,7 @@ class VectorSource extends Source {
|
||||
* {@link module:ol/source/Vector~VectorSource#forEachFeatureInExtent #forEachFeatureInExtent()} method instead.
|
||||
*
|
||||
* @param {import("../extent.js").Extent} extent Extent.
|
||||
* @param {function(import("../Feature.js").default): T} callback Called with each feature
|
||||
* @param {function(import("../Feature.js").default<Geometry>): T} callback Called with each feature
|
||||
* whose geometry intersects the provided extent.
|
||||
* @return {T|undefined} The return value from the last call to the callback.
|
||||
* @template T
|
||||
@@ -604,7 +606,7 @@ class VectorSource extends Source {
|
||||
forEachFeatureIntersectingExtent(extent, callback) {
|
||||
return this.forEachFeatureInExtent(extent,
|
||||
/**
|
||||
* @param {import("../Feature.js").default} feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature.
|
||||
* @return {T|undefined} The return value from the last call to the callback.
|
||||
*/
|
||||
function(feature) {
|
||||
@@ -623,7 +625,7 @@ class VectorSource extends Source {
|
||||
* Get the features collection associated with this source. Will be `null`
|
||||
* unless the source was configured with `useSpatialIndex` set to `false`, or
|
||||
* with an {@link module:ol/Collection} as `features`.
|
||||
* @return {Collection<import("../Feature.js").default>} The collection of features.
|
||||
* @return {Collection<import("../Feature.js").default<Geometry>>} The collection of features.
|
||||
* @api
|
||||
*/
|
||||
getFeaturesCollection() {
|
||||
@@ -633,7 +635,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Get all features on the source in random order.
|
||||
* @return {Array<import("../Feature.js").default>} Features.
|
||||
* @return {Array<import("../Feature.js").default<Geometry>>} Features.
|
||||
* @api
|
||||
*/
|
||||
getFeatures() {
|
||||
@@ -647,7 +649,7 @@ class VectorSource extends Source {
|
||||
}
|
||||
}
|
||||
return (
|
||||
/** @type {Array<import("../Feature.js").default>} */ (features)
|
||||
/** @type {Array<import("../Feature.js").default<Geometry>>} */ (features)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -655,7 +657,7 @@ class VectorSource extends Source {
|
||||
/**
|
||||
* Get all features whose geometry intersects the provided coordinate.
|
||||
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
|
||||
* @return {Array<import("../Feature.js").default>} Features.
|
||||
* @return {Array<import("../Feature.js").default<Geometry>>} Features.
|
||||
* @api
|
||||
*/
|
||||
getFeaturesAtCoordinate(coordinate) {
|
||||
@@ -675,7 +677,7 @@ class VectorSource extends Source {
|
||||
* This method is not available when the source is configured with
|
||||
* `useSpatialIndex` set to `false`.
|
||||
* @param {import("../extent.js").Extent} extent Extent.
|
||||
* @return {Array<import("../Feature.js").default>} Features.
|
||||
* @return {Array<import("../Feature.js").default<Geometry>>} Features.
|
||||
* @api
|
||||
*/
|
||||
getFeaturesInExtent(extent) {
|
||||
@@ -689,10 +691,10 @@ class VectorSource extends Source {
|
||||
* This method is not available when the source is configured with
|
||||
* `useSpatialIndex` set to `false`.
|
||||
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
|
||||
* @param {function(import("../Feature.js").default):boolean=} opt_filter Feature filter function.
|
||||
* @param {function(import("../Feature.js").default<Geometry>):boolean=} opt_filter Feature filter function.
|
||||
* The filter function will receive one argument, the {@link module:ol/Feature feature}
|
||||
* and it should return a boolean value. By default, no filtering is made.
|
||||
* @return {import("../Feature.js").default} Closest feature.
|
||||
* @return {import("../Feature.js").default<Geometry>} Closest feature.
|
||||
* @api
|
||||
*/
|
||||
getClosestFeatureToCoordinate(coordinate, opt_filter) {
|
||||
@@ -712,7 +714,7 @@ class VectorSource extends Source {
|
||||
const filter = opt_filter ? opt_filter : TRUE;
|
||||
this.featuresRtree_.forEachInExtent(extent,
|
||||
/**
|
||||
* @param {import("../Feature.js").default} feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature.
|
||||
*/
|
||||
function(feature) {
|
||||
if (filter(feature)) {
|
||||
@@ -759,7 +761,7 @@ class VectorSource extends Source {
|
||||
* `source.getFeatureById(2)` will return a feature with id `'2'` or `2`.
|
||||
*
|
||||
* @param {string|number} id Feature identifier.
|
||||
* @return {import("../Feature.js").default} The feature (or `null` if not found).
|
||||
* @return {import("../Feature.js").default<Geometry>} The feature (or `null` if not found).
|
||||
* @api
|
||||
*/
|
||||
getFeatureById(id) {
|
||||
@@ -803,7 +805,7 @@ class VectorSource extends Source {
|
||||
* @private
|
||||
*/
|
||||
handleFeatureChange_(event) {
|
||||
const feature = /** @type {import("../Feature.js").default} */ (event.target);
|
||||
const feature = /** @type {import("../Feature.js").default<Geometry>} */ (event.target);
|
||||
const featureKey = getUid(feature);
|
||||
const geometry = feature.getGeometry();
|
||||
if (!geometry) {
|
||||
@@ -851,7 +853,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Returns true if the feature is contained within the source.
|
||||
* @param {import("../Feature.js").default} feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature.
|
||||
* @return {boolean} Has feature.
|
||||
* @api
|
||||
*/
|
||||
@@ -933,7 +935,7 @@ class VectorSource extends Source {
|
||||
* Remove a single feature from the source. If you want to remove all features
|
||||
* at once, use the {@link module:ol/source/Vector~VectorSource#clear #clear()} method
|
||||
* instead.
|
||||
* @param {import("../Feature.js").default} feature Feature to remove.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature to remove.
|
||||
* @api
|
||||
*/
|
||||
removeFeature(feature) {
|
||||
@@ -952,7 +954,7 @@ class VectorSource extends Source {
|
||||
|
||||
/**
|
||||
* Remove feature without firing a `change` event.
|
||||
* @param {import("../Feature.js").default} feature Feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature Feature.
|
||||
* @protected
|
||||
*/
|
||||
removeFeatureInternal(feature) {
|
||||
@@ -973,7 +975,7 @@ class VectorSource extends Source {
|
||||
/**
|
||||
* Remove a feature from the id index. Called internally when the feature id
|
||||
* may have changed.
|
||||
* @param {import("../Feature.js").default} feature The feature.
|
||||
* @param {import("../Feature.js").default<Geometry>} feature The feature.
|
||||
* @return {boolean} Removed the feature from the index.
|
||||
* @private
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user