I have a map service published to ArcGIS Server whose spatial reference is WGS 84 UTM Zone 6N (WKID 32606). One of the layers in the service has labeling information defined, and the
labelExpressionInfo.expression is set to:
Round(Length(Geometry($feature),"feet"),2). I load this layer into a map as a 2D
FeatureLayer. The spatial reference of the map is Web Mercator (WKID 3857).
The data being retrieved from the server is returned by ArcGIS Server projected into Web Mercator (WKID 3857). Nonetheless, labels do not show up, and the following error appears in the console:
Cannot work with geometry in this spatial reference. It is different to the execution spatial reference.
This should not be happening because the data queried from the service is already in the Web Mercator projection. The problem is that the API, when processing the FeatureSets returned from the server, assigns the layer's spatial reference (WKID 32606) to the data, even though the coordinates are Web Mercator.
The most convenient place I've found to insert a fix for this is in the esri.views.2d.layers.features.support.FeatureSetReaderJSON module, in the "fromFeatureSet" method. The published implementation looks like this:
static fromFeatureSet(a, f) {
a = q.convertFromFeatureSet(a, f.objectIdField);
return l.fromOptimizedFeatureSet(a, f)
}
The "a" parameter is the
FeatureSet returned from the query, still in JSON format. The "f" parameter is an object containing various settings from the layer, including the spatial reference.
Basically, I clone the "f" object, and set its spatial reference to the same as the FeatureSet:
static fromFeatureSet(a, f) {
var g = {};
Object.getOwnPropertyNames(f).concat(Object.getOwnPropertyNames(Object.getPrototypeOf(f))).forEach(function(h) {
if (typeof f[h] != "function")
g[h] = f[h];
});
if (a.spatialReference)
g.spatialReference = f.spatialReference.constructor.fromJSON(a.spatialReference);
f = g;
a = q.convertFromFeatureSet(a, f.objectIdField);
return l.fromOptimizedFeatureSet(a, f)
}
As can be seen, I've added lines 2-9, and everything else is the same. With this fix in place, no errors occur and the labels show up properly. I was using 4.31 for this, but other versions may be affected as well.