Make the vector source geometry generic

This commit is contained in:
Stéphane Brunner
2019-02-05 13:01:56 +01:00
parent 051cc68f24
commit 970c1bcb66
2 changed files with 46 additions and 43 deletions
+6 -5
View File
@@ -57,10 +57,11 @@ import BaseObject, {getChangeEventType} from './Object.js';
* ``` * ```
* *
* @api * @api
* @template {import("./geom/Geometry.js").default} Geometry
*/ */
class Feature extends BaseObject { class Feature extends BaseObject {
/** /**
* @param {import("./geom/Geometry.js").default|Object<string, *>=} opt_geometryOrProperties * @param {Geometry|Object<string, *>=} opt_geometryOrProperties
* You may pass a Geometry object directly, or an object literal containing * You may pass a Geometry object directly, or an object literal containing
* properties. If you pass an object literal, you may include a Geometry * properties. If you pass an object literal, you may include a Geometry
* associated with a `geometry` key. * associated with a `geometry` key.
@@ -106,7 +107,7 @@ class Feature extends BaseObject {
if (opt_geometryOrProperties) { if (opt_geometryOrProperties) {
if (typeof /** @type {?} */ (opt_geometryOrProperties).getSimplifiedGeometry === 'function') { if (typeof /** @type {?} */ (opt_geometryOrProperties).getSimplifiedGeometry === 'function') {
const geometry = /** @type {import("./geom/Geometry.js").default} */ (opt_geometryOrProperties); const geometry = /** @type {Geometry} */ (opt_geometryOrProperties);
this.setGeometry(geometry); this.setGeometry(geometry);
} else { } else {
/** @type {Object<string, *>} */ /** @type {Object<string, *>} */
@@ -140,13 +141,13 @@ class Feature extends BaseObject {
* Get the feature's default geometry. A feature may have any number of named * Get the feature's default geometry. A feature may have any number of named
* geometries. The "default" geometry (the one that is rendered by default) is * geometries. The "default" geometry (the one that is rendered by default) is
* set when calling {@link module:ol/Feature~Feature#setGeometry}. * set when calling {@link module:ol/Feature~Feature#setGeometry}.
* @return {import("./geom/Geometry.js").default|undefined} The default geometry for the feature. * @return {Geometry|undefined} The default geometry for the feature.
* @api * @api
* @observable * @observable
*/ */
getGeometry() { getGeometry() {
return ( return (
/** @type {import("./geom/Geometry.js").default|undefined} */ (this.get(this.geometryName_)) /** @type {Geometry|undefined} */ (this.get(this.geometryName_))
); );
} }
@@ -218,7 +219,7 @@ class Feature extends BaseObject {
/** /**
* Set the default geometry for the feature. This will update the property * Set the default geometry for the feature. This will update the property
* with the name returned by {@link module:ol/Feature~Feature#getGeometryName}. * with the name returned by {@link module:ol/Feature~Feature#getGeometryName}.
* @param {import("./geom/Geometry.js").default|undefined} geometry The new geometry. * @param {Geometry|undefined} geometry The new geometry.
* @api * @api
* @observable * @observable
*/ */
+40 -38
View File
@@ -35,12 +35,13 @@ import RBush from '../structs/RBush.js';
* @classdesc * @classdesc
* Events emitted by {@link module:ol/source/Vector} instances are instances of this * Events emitted by {@link module:ol/source/Vector} instances are instances of this
* type. * type.
* @template {import("../geom/Geometry.js").default} Geometry
*/ */
export class VectorSourceEvent extends Event { export class VectorSourceEvent extends Event {
/** /**
* @param {string} type Type. * @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) { constructor(type, opt_feature) {
@@ -48,7 +49,7 @@ export class VectorSourceEvent extends Event {
/** /**
* The feature being added or removed. * The feature being added or removed.
* @type {import("../Feature.js").default|undefined} * @type {import("../Feature.js").default<Geometry>|undefined}
* @api * @api
*/ */
this.feature = opt_feature; 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 * by this source are suitable for editing. See {@link module:ol/source/VectorTile~VectorTile} for
* vector data that is optimized for rendering. * vector data that is optimized for rendering.
* *
* @fires VectorSourceEvent * @fires VectorSourceEvent<Geometry>
* @api * @api
* @template {import("../geom/Geometry.js").default} Geometry
*/ */
class VectorSource extends Source { class VectorSource extends Source {
/** /**
@@ -215,7 +217,7 @@ class VectorSource extends Source {
/** /**
* @private * @private
* @type {RBush<import("../Feature.js").default>} * @type {RBush<import("../Feature.js").default<Geometry>>}
*/ */
this.featuresRtree_ = useSpatialIndex ? new RBush() : null; this.featuresRtree_ = useSpatialIndex ? new RBush() : null;
@@ -227,21 +229,21 @@ class VectorSource extends Source {
/** /**
* @private * @private
* @type {!Object<string, import("../Feature.js").default>} * @type {!Object<string, import("../Feature.js").default<Geometry>>}
*/ */
this.nullGeometryFeatures_ = {}; this.nullGeometryFeatures_ = {};
/** /**
* A lookup of features by id (the return from feature.getId()). * A lookup of features by id (the return from feature.getId()).
* @private * @private
* @type {!Object<string, import("../Feature.js").default>} * @type {!Object<string, import("../Feature.js").default<Geometry>>}
*/ */
this.idIndex_ = {}; this.idIndex_ = {};
/** /**
* A lookup of features without id (keyed by getUid(feature)). * A lookup of features without id (keyed by getUid(feature)).
* @private * @private
* @type {!Object<string, import("../Feature.js").default>} * @type {!Object<string, import("../Feature.js").default<Geometry>>}
*/ */
this.undefIdIndex_ = {}; this.undefIdIndex_ = {};
@@ -253,7 +255,7 @@ class VectorSource extends Source {
/** /**
* @private * @private
* @type {Collection<import("../Feature.js").default>} * @type {Collection<import("../Feature.js").default<Geometry>>}
*/ */
this.featuresCollection_ = null; 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, * 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 * meaning that if a feature with a duplicate id is added in the collection, it will
* be removed from it right away. * 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 * @api
*/ */
addFeature(feature) { addFeature(feature) {
@@ -296,7 +298,7 @@ class VectorSource extends Source {
/** /**
* Add a feature without firing a `change` event. * Add a feature without firing a `change` event.
* @param {import("../Feature.js").default} feature Feature. * @param {import("../Feature.js").default<Geometry>} feature Feature.
* @protected * @protected
*/ */
addFeatureInternal(feature) { addFeatureInternal(feature) {
@@ -328,7 +330,7 @@ class VectorSource extends Source {
/** /**
* @param {string} featureKey Unique identifier for the feature. * @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 * @private
*/ */
setupChangeEvents_(featureKey, feature) { setupChangeEvents_(featureKey, feature) {
@@ -343,7 +345,7 @@ class VectorSource extends Source {
/** /**
* @param {string} featureKey Unique identifier for the feature. * @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 * @return {boolean} The feature is "valid", in the sense that it is also a
* candidate for insertion into the Rtree. * candidate for insertion into the Rtree.
* @private * @private
@@ -368,7 +370,7 @@ class VectorSource extends Source {
/** /**
* Add a batch of features to the 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 * @api
*/ */
addFeatures(features) { addFeatures(features) {
@@ -379,7 +381,7 @@ class VectorSource extends Source {
/** /**
* Add features without firing a `change` event. * 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 * @protected
*/ */
addFeaturesInternal(features) { 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 * @private
*/ */
bindFeaturesCollection_(collection) { bindFeaturesCollection_(collection) {
let modifyingCollection = false; let modifyingCollection = false;
listen(this, VectorEventType.ADDFEATURE, listen(this, VectorEventType.ADDFEATURE,
/** /**
* @param {VectorSourceEvent} evt The vector source event * @param {VectorSourceEvent<Geometry>} evt The vector source event
*/ */
function(evt) { function(evt) {
if (!modifyingCollection) { if (!modifyingCollection) {
@@ -438,7 +440,7 @@ class VectorSource extends Source {
}); });
listen(this, VectorEventType.REMOVEFEATURE, listen(this, VectorEventType.REMOVEFEATURE,
/** /**
* @param {VectorSourceEvent} evt The vector source event * @param {VectorSourceEvent<Geometry>} evt The vector source event
*/ */
function(evt) { function(evt) {
if (!modifyingCollection) { if (!modifyingCollection) {
@@ -454,7 +456,7 @@ class VectorSource extends Source {
function(evt) { function(evt) {
if (!modifyingCollection) { if (!modifyingCollection) {
modifyingCollection = true; modifyingCollection = true;
this.addFeature(/** @type {import("../Feature.js").default} */ (evt.element)); this.addFeature(/** @type {import("../Feature.js").default<Geometry>} */ (evt.element));
modifyingCollection = false; modifyingCollection = false;
} }
}, this); }, this);
@@ -465,7 +467,7 @@ class VectorSource extends Source {
function(evt) { function(evt) {
if (!modifyingCollection) { if (!modifyingCollection) {
modifyingCollection = true; modifyingCollection = true;
this.removeFeature(/** @type {import("../Feature.js").default} */ (evt.element)); this.removeFeature(/** @type {import("../Feature.js").default<Geometry>} */ (evt.element));
modifyingCollection = false; modifyingCollection = false;
} }
}, this); }, this);
@@ -518,7 +520,7 @@ class VectorSource extends Source {
* stop and the function will return the same value. * stop and the function will return the same value.
* Note: this function only iterate through the feature that have a defined geometry. * 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. * on the source. Return a truthy value to stop iteration.
* @return {T|undefined} The return value from the last call to the callback. * @return {T|undefined} The return value from the last call to the callback.
* @template T * @template T
@@ -540,7 +542,7 @@ class VectorSource extends Source {
* value. * value.
* *
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate. * @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. * whose goemetry contains the provided coordinate.
* @return {T|undefined} The return value from the last call to the callback. * @return {T|undefined} The return value from the last call to the callback.
* @template T * @template T
@@ -571,7 +573,7 @@ class VectorSource extends Source {
* features, equivalent to {@link module:ol/source/Vector~VectorSource#forEachFeature #forEachFeature()}. * features, equivalent to {@link module:ol/source/Vector~VectorSource#forEachFeature #forEachFeature()}.
* *
* @param {import("../extent.js").Extent} extent Extent. * @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. * whose bounding box intersects the provided extent.
* @return {T|undefined} The return value from the last call to the callback. * @return {T|undefined} The return value from the last call to the callback.
* @template T * @template T
@@ -595,7 +597,7 @@ class VectorSource extends Source {
* {@link module:ol/source/Vector~VectorSource#forEachFeatureInExtent #forEachFeatureInExtent()} method instead. * {@link module:ol/source/Vector~VectorSource#forEachFeatureInExtent #forEachFeatureInExtent()} method instead.
* *
* @param {import("../extent.js").Extent} extent Extent. * @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. * whose geometry intersects the provided extent.
* @return {T|undefined} The return value from the last call to the callback. * @return {T|undefined} The return value from the last call to the callback.
* @template T * @template T
@@ -604,7 +606,7 @@ class VectorSource extends Source {
forEachFeatureIntersectingExtent(extent, callback) { forEachFeatureIntersectingExtent(extent, callback) {
return this.forEachFeatureInExtent(extent, 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. * @return {T|undefined} The return value from the last call to the callback.
*/ */
function(feature) { function(feature) {
@@ -623,7 +625,7 @@ class VectorSource extends Source {
* Get the features collection associated with this source. Will be `null` * Get the features collection associated with this source. Will be `null`
* unless the source was configured with `useSpatialIndex` set to `false`, or * unless the source was configured with `useSpatialIndex` set to `false`, or
* with an {@link module:ol/Collection} as `features`. * 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 * @api
*/ */
getFeaturesCollection() { getFeaturesCollection() {
@@ -633,7 +635,7 @@ class VectorSource extends Source {
/** /**
* Get all features on the source in random order. * 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 * @api
*/ */
getFeatures() { getFeatures() {
@@ -647,7 +649,7 @@ class VectorSource extends Source {
} }
} }
return ( 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. * Get all features whose geometry intersects the provided coordinate.
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate. * @param {import("../coordinate.js").Coordinate} coordinate Coordinate.
* @return {Array<import("../Feature.js").default>} Features. * @return {Array<import("../Feature.js").default<Geometry>>} Features.
* @api * @api
*/ */
getFeaturesAtCoordinate(coordinate) { getFeaturesAtCoordinate(coordinate) {
@@ -675,7 +677,7 @@ class VectorSource extends Source {
* This method is not available when the source is configured with * This method is not available when the source is configured with
* `useSpatialIndex` set to `false`. * `useSpatialIndex` set to `false`.
* @param {import("../extent.js").Extent} extent Extent. * @param {import("../extent.js").Extent} extent Extent.
* @return {Array<import("../Feature.js").default>} Features. * @return {Array<import("../Feature.js").default<Geometry>>} Features.
* @api * @api
*/ */
getFeaturesInExtent(extent) { getFeaturesInExtent(extent) {
@@ -689,10 +691,10 @@ class VectorSource extends Source {
* This method is not available when the source is configured with * This method is not available when the source is configured with
* `useSpatialIndex` set to `false`. * `useSpatialIndex` set to `false`.
* @param {import("../coordinate.js").Coordinate} coordinate Coordinate. * @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} * 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. * 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 * @api
*/ */
getClosestFeatureToCoordinate(coordinate, opt_filter) { getClosestFeatureToCoordinate(coordinate, opt_filter) {
@@ -712,7 +714,7 @@ class VectorSource extends Source {
const filter = opt_filter ? opt_filter : TRUE; const filter = opt_filter ? opt_filter : TRUE;
this.featuresRtree_.forEachInExtent(extent, this.featuresRtree_.forEachInExtent(extent,
/** /**
* @param {import("../Feature.js").default} feature Feature. * @param {import("../Feature.js").default<Geometry>} feature Feature.
*/ */
function(feature) { function(feature) {
if (filter(feature)) { if (filter(feature)) {
@@ -759,7 +761,7 @@ class VectorSource extends Source {
* `source.getFeatureById(2)` will return a feature with id `'2'` or `2`. * `source.getFeatureById(2)` will return a feature with id `'2'` or `2`.
* *
* @param {string|number} id Feature identifier. * @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 * @api
*/ */
getFeatureById(id) { getFeatureById(id) {
@@ -803,7 +805,7 @@ class VectorSource extends Source {
* @private * @private
*/ */
handleFeatureChange_(event) { 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 featureKey = getUid(feature);
const geometry = feature.getGeometry(); const geometry = feature.getGeometry();
if (!geometry) { if (!geometry) {
@@ -851,7 +853,7 @@ class VectorSource extends Source {
/** /**
* Returns true if the feature is contained within the 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. * @return {boolean} Has feature.
* @api * @api
*/ */
@@ -933,7 +935,7 @@ class VectorSource extends Source {
* Remove a single feature from the source. If you want to remove all features * 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 * at once, use the {@link module:ol/source/Vector~VectorSource#clear #clear()} method
* instead. * instead.
* @param {import("../Feature.js").default} feature Feature to remove. * @param {import("../Feature.js").default<Geometry>} feature Feature to remove.
* @api * @api
*/ */
removeFeature(feature) { removeFeature(feature) {
@@ -952,7 +954,7 @@ class VectorSource extends Source {
/** /**
* Remove feature without firing a `change` event. * Remove feature without firing a `change` event.
* @param {import("../Feature.js").default} feature Feature. * @param {import("../Feature.js").default<Geometry>} feature Feature.
* @protected * @protected
*/ */
removeFeatureInternal(feature) { removeFeatureInternal(feature) {
@@ -973,7 +975,7 @@ class VectorSource extends Source {
/** /**
* Remove a feature from the id index. Called internally when the feature id * Remove a feature from the id index. Called internally when the feature id
* may have changed. * 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. * @return {boolean} Removed the feature from the index.
* @private * @private
*/ */