I have an ArcGIS Arcade script that uses JavaScript syntax.
- The script loops through GIS polylines and updates a coordinate.
- The coordinate is called an "M" coordinate (aka a "Measure-value"). M coordinates are similar to X and Y coordinates, but are used for specialized linear referencing purposes.
//var geom = Dictionary(Text(Geometry($feature)));
//var paths = geom['paths'];
var paths = [[[0,5,null],[10,10, null],[30,0, null],[50,10, null],[60,10, null]]] //input data for Code Review testing purposes
var geom_updated = false;
var length = 0;
for (var path_idx in paths) {
for (var vertex_idx in paths[path_idx]) {
//Set the first vertex's M-value to 0.
if (vertex_idx == 0) {
paths[0][0][-1] = 0;
geom_updated = true;
//For the rest of the vertices, set the M-value to the cumulative length of the line.
} else {
//Set the startpoint and endpoint of the current segment of the polyline.
//Note: The geometry type, and its subtypes (Point and Polyline) are immutable: https://community.esri.com/t5/arcgis-pro-questions/are-arcade-geometry-subtypes-immutable-i-e-point/m-p/1150097#M52278
var startpoint = Point({ 'x': paths[path_idx][vertex_idx - 1][0], 'y': paths[path_idx][vertex_idx - 1][1], 'spatialReference': { 'wkid': 26917 } });
var endpoint = Point({ 'x': paths[path_idx][vertex_idx][0], 'y': paths[path_idx][vertex_idx][1], 'spatialReference': { 'wkid': 26917 } });
//The Distance() function returns the planar distance between two geometries in the given units. This is a planar measurement using Cartesian mathematics. https://developers.arcgis.com/arcade/function-reference/geometry_functions/#distance
//I think the Pythagorean theorem would produce the same result: https://stackoverflow.com/questions/20916953/get-distance-between-two-points-in-canvas
length = length + Distance(startpoint, endpoint);
paths[path_idx][vertex_idx][-1] = length;
geom_updated = true;
}
}
}
if (!geom_updated) {
return
}
return {
//"result": {"geometry": Polyline(geom)}
"result": { "geometry": paths }
};
//Output: [[[0,5,0],[10,10,11.18],[30,0,33.54],[50,10,55.90],[60,10,65.90]]]
Related: ArcGIS Arcade Function Reference
How can the script be improved?
Polyline(geom)
, makes no sense. Basicallygeom
is not defined. Can I assume you meanpaths
? Yes, that must be it. \$\endgroup\$