The following code was working fine in 3.0 and 2.9 bot not anymore in 3.1. Can anyone tell me what has changed? I get an "General evaluation error" on line 3:
function getInflowStreamsWithoutConnectionVertex(polyline_) {
var inflowStreams = [];
var candidates = Intersects($featureSet, polyline_);
var objectId = feature_["OBJECTID"];
for (var inflowStream in Filter(candidates, 'OBJECTID <> @objectId')) {
var startVertex = getFirstVertexFromPolyline(Geometry(inflowStream));
if (Intersects(polyline_, startVertex)) {
var result = nearestPointAlongPolyline(startVertex, polyline_);
if (!result["isVertex"]) {
//result['polyline'] = inflowStream;
result['startVertex'] = startVertex;
Push(inflowStreams, result);
}
}
}
return inflowStreams
}
function getFirstVertexFromPolyline(polyline_) {
var segment = polyline_.paths[0];
return segment[0]
}
function nearestPointAlongPolyline(point_, polyline_) {
/*
finds the closest point on line_feature from point_feature
Args:
point_feature: Point Geometry
line_feature: Line Geometry
Returns: dictionary
{distance: number, // distance from point_feature to closest point
coordinates: array, // the coordinate pair of the closest point
isVertex: bool, // if the closest point is a vertex of line_feature
lineSide: text} // side of the line that point_feature is on based
*/
var parts = polyline_["paths"]
var x = point_["x"];
var y = point_["y"];
var isVertex = false;
// Loop through each part of the geometry and each segment, tracking the shortest distance
var shortest = [1e10];
for (var i in parts) {
var vertex = parts[i];
var previous = vertex[0];
isVertex = Intersects(point_, previous);
for (var j = 1; j < Count(vertex); j++) {
var current = vertex[j];
isVertex = isVertex || Intersects(point_, current);
var result = getDistance(x, y, previous["x"], previous["y"], current["x"], current["y"]);
result[4] = previous["m"];
result[5] = current["m"];
if (result[0] < shortest[0]) shortest = result
previous = current;
}
}
// Couldn't find anything
if (Count(shortest) == 1) return null
return {
"distance": shortest[0],
"coordinates": shortest[1],
"isVertex": isVertex,
"lineSide": shortest[3],
"mBeforeVertex": shortest[4],
"mAfterVertetx": shortest[5]
}
}
function getDistance(x, y, x1, y1, x2, y2) {
// adopted from https://stackoverflow.com/a/6853926
var A = x - x1;
var B = y - y1;
var C = x2 - x1;
var D = y2 - y1;
var dot = A * C + B * D;
var len_sq = C * C + D * D;
var param = -1;
if (len_sq != 0) //in case of 0 length line
param = dot / len_sq;
var xyz, yy;
var is_vertex = true;
if (param < 0) {
xyz = x1;
yy = y1;
}
else if (param > 1) {
xyz = x2;
yy = y2;
}
else {
is_vertex = false;
xyz = x1 + param * C;
yy = y1 + param * D;
}
var dx = x - xyz;
var dy = y - yy;
return [Sqrt(dx * dx + dy * dy), [xyz, yy], is_vertex, side_of_line(x, y, x1, y1, x2, y2)];
}
function side_of_line(x, y, x1, y1, x2, y2) {
// get side of line segment that a point (x, y) is on based on the direction of segment [[x1, y1], [x2, y2]]
// adopted from https://math.stackexchange.com/a/274728
var d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1)
if (d < 0) {
return 'left'
} else if (d > 0) {
return 'right'
} else {
return null
}
}
function insertVertex(polyline_, point_, mBeforeVertex) {
var dict = Dictionary(Text(polyline_));
var parts = dict.paths
for (var i in parts) {
var part = parts[i];
for (var j = 0; j < Count(part); j++) {
var vertex = part[j];
if (Round(vertex[2],4) == Round(mBeforeVertex,4)) {
var m = Round(vertex[2] + Distance(createPoint(vertex, 0, point_["spatialReference"]), point_), 4);
Insert(dict.paths[i], j + 1, [point_['x'], point_['y'], m]);
}
}
}
dict["hasM"] = true;
return Polyline(dict)
};
function createPoint(coordinates, m, spatial_ref) {
// create point geometry from coordinates [x, y]
return Point({ "x": coordinates[0], "y": coordinates[1], "m": m, "spatialReference": spatial_ref })
}
/***************************************************************************************/
var feature_ = $feature;
//var feature_ = First(Filter($featureSet, "objectId = 1"));
var inflowStreams = getInflowStreamsWithoutConnectionVertex(Geometry(feature_));
var newPolyline = Geometry(feature_);
var countInsert = 0;
for (var i in inflowStreams) {
var info = inflowStreams[i];
newPolyline = insertVertex(newPolyline, info['startVertex'], info['mBeforeVertex']);
countInsert += 1;
}
/*if (Count(inflowStreams) > 0) {
}*/
return {
"result": newPolyline,
'edit': [{
'className': 'AGIS_410_Dev.GEWIA.arcade_log',
'adds': [{
'attributes': {
'value': "Count Inserted Vertices: " + countInsert
}
}]
}]
}