Pass the id field name as a new param
This commit is contained in:
@@ -104,10 +104,11 @@ class EsriJSON extends JSONFeature {
|
|||||||
/**
|
/**
|
||||||
* @param {Object} object Object.
|
* @param {Object} object Object.
|
||||||
* @param {import("./Feature.js").ReadOptions=} opt_options Read options.
|
* @param {import("./Feature.js").ReadOptions=} opt_options Read options.
|
||||||
|
* @param {string=} opt_idField Name of the field where to get the id from.
|
||||||
* @protected
|
* @protected
|
||||||
* @return {import("../Feature.js").default} Feature.
|
* @return {import("../Feature.js").default} Feature.
|
||||||
*/
|
*/
|
||||||
readFeatureFromObject(object, opt_options) {
|
readFeatureFromObject(object, opt_options, opt_idField) {
|
||||||
const esriJSONFeature = /** @type {EsriJSONFeature} */ (object);
|
const esriJSONFeature = /** @type {EsriJSONFeature} */ (object);
|
||||||
const geometry = readGeometry(esriJSONFeature.geometry, opt_options);
|
const geometry = readGeometry(esriJSONFeature.geometry, opt_options);
|
||||||
const feature = new Feature();
|
const feature = new Feature();
|
||||||
@@ -115,12 +116,12 @@ class EsriJSON extends JSONFeature {
|
|||||||
feature.setGeometryName(this.geometryName_);
|
feature.setGeometryName(this.geometryName_);
|
||||||
}
|
}
|
||||||
feature.setGeometry(geometry);
|
feature.setGeometry(geometry);
|
||||||
if (opt_options && opt_options.idField &&
|
|
||||||
esriJSONFeature.attributes[opt_options.idField]) {
|
|
||||||
feature.setId(/** @type {number} */(esriJSONFeature.attributes[opt_options.idField]));
|
|
||||||
}
|
|
||||||
if (esriJSONFeature.attributes) {
|
if (esriJSONFeature.attributes) {
|
||||||
feature.setProperties(esriJSONFeature.attributes, true);
|
feature.setProperties(esriJSONFeature.attributes, true);
|
||||||
|
const id = esriJSONFeature.attributes[opt_idField];
|
||||||
|
if (id !== undefined) {
|
||||||
|
feature.setId(/** @type {number} */(id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return feature;
|
return feature;
|
||||||
}
|
}
|
||||||
@@ -138,9 +139,8 @@ class EsriJSON extends JSONFeature {
|
|||||||
/** @type {Array<import("../Feature.js").default>} */
|
/** @type {Array<import("../Feature.js").default>} */
|
||||||
const features = [];
|
const features = [];
|
||||||
const esriJSONFeatures = esriJSONFeatureSet.features;
|
const esriJSONFeatures = esriJSONFeatureSet.features;
|
||||||
options.idField = object.objectIdFieldName;
|
|
||||||
for (let i = 0, ii = esriJSONFeatures.length; i < ii; ++i) {
|
for (let i = 0, ii = esriJSONFeatures.length; i < ii; ++i) {
|
||||||
features.push(this.readFeatureFromObject(esriJSONFeatures[i], options));
|
features.push(this.readFeatureFromObject(esriJSONFeatures[i], options, object.objectIdFieldName));
|
||||||
}
|
}
|
||||||
return features;
|
return features;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ describe('ol.format.EsriJSON', function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const featureCollectionEsriJSON = {
|
const featureCollectionEsriJSON = {
|
||||||
|
objectIdFieldName: 'prop0',
|
||||||
features: [pointEsriJSON, lineStringEsriJSON, polygonEsriJSON]
|
features: [pointEsriJSON, lineStringEsriJSON, polygonEsriJSON]
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -230,6 +231,10 @@ describe('ol.format.EsriJSON', function() {
|
|||||||
expect(features[0].getGeometry()).to.be.an(Point);
|
expect(features[0].getGeometry()).to.be.an(Point);
|
||||||
expect(features[1].getGeometry()).to.be.an(LineString);
|
expect(features[1].getGeometry()).to.be.an(LineString);
|
||||||
expect(features[2].getGeometry()).to.be.an(Polygon);
|
expect(features[2].getGeometry()).to.be.an(Polygon);
|
||||||
|
|
||||||
|
expect(features[0].getId()).to.eql('value0');
|
||||||
|
expect(features[1].getId()).to.eql('value0');
|
||||||
|
expect(features[2].getId()).to.eql('value0');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can read and transform a point', function() {
|
it('can read and transform a point', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user