Attribute rule not working in 3.1

812
4
Jump to solution
02-25-2023 08:57 AM
SarahIerley1
New Contributor II

This attribute rule works in ArcGIS Pro 3.0 but when I upgraded to 3.1 yesterday it stopped working.  Can anyone tell me why?  What changed from 3.0 to 3.1? I'm not very knowledgeable with Arcade!  I am not able to edit the layer at all with this attribute rule turned on.   Other attribute rules still work.  Here is the error message and the code:

SarahIerley1_0-1677344181327.png

 

// This rule will calculate the left and right municipality for a road.
// It determines if the road is completely within a intersectingArea or falls on the edge of a intersectingArea and updates the appropriate values on the road
// Define the Road Centerline fields
var jurisleft_field = "L_JURISDIC";
var jurisright_field = "R_JURISDIC";
// Define the Geopolitical Areas fields
var jurisname_field = "JURISD";
// Get the intersecting Geopolictical Areas
var intersectingAreas = Intersects(FeatureSetByName($datastore, "PublicSafetyGIS_Data.PUBLICSAFETYGIS.Test_Jurisdictions", [jurisname_field], true), $feature)
// This function will convert a polygon geometry to a polyline
function polygonToPolyline(p) {
var json = Dictionary(Text(p));
var polylineJSON = {
"paths": json["rings"],
"spatialReference": json["spatialReference"]
};
return Polyline(polylineJSON)
}
// Test if the road falls completely within a area and does not overlap any of the area's outline
// If it does update the left and right value to be equal to the area's value
var isWithin = false;
var partialOverlap = [];
for (var intersectingArea in intersectingAreas) {
if (Within($feature, intersectingArea)) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (!Overlaps($feature, line)) {
var jurisleft = intersectingArea[jurisname_field];
var jurisright = intersectingArea[jurisname_field];
isWithin = true;
break;
}
// Store any boundaries the line is partially within (overlaps some of the polygons intersectingArea)
else {
Push(partialOverlap, intersectingArea);
}
}
}
// If the road does not fall within a area, attempt to find any areas that it overlaps the outline
// Then test if the polygon is on the right or left side of the line and update the right or left value
if (!isWithin) {
var isRightValue = false;
var isLeftValue = false;
for (var intersectingArea in intersectingAreas) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (Within($feature, line)) {
// Offset the geometry to the right and test if it intersects the intersectingArea
var offset_geometry = Offset($feature, 5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisright = intersectingArea[jurisname_field];
isRightValue = true;
}
// Offset the geometry to the left and test if it intersects the intersectingArea
offset_geometry = Offset($feature, -5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisleft = intersectingArea[jurisname_field];
isLeftValue = true;
}
}
}
}
// If either the left or right value is not set we will loop through the partially within
if (isLeftValue || isRightValue) {
for (var i in partialOverlap) {
var intersectingArea = partialOverlap[i];
var line = polygonToPolyline(Geometry(intersectingArea));
// Get the portion of the road that overlaps the polygon intersectingArea
var intersection_geometry = Intersection($feature, line);
// Offset this portion of the road to the right and test if it intersects the intersectingArea
var offset_geometry = Offset(intersection_geometry, 5);
if (Intersects(offset_geometry, intersectingArea) && !isRightValue) {
var jurisright = intersectingArea[jurisname_field];
}
// Offset this portion of the road to the left and test if it intersects the intersectingArea
offset_geometry = Offset(intersection_geometry, -5);
if (Intersects(offset_geometry, intersectingArea) && !isLeftValue) {
var jurisleft = intersectingArea[jurisname_field];
}
}
}
return {
"result": {
"attributes":
Dictionary(
jurisleft_field, jurisleft,
jurisright_field, jurisright
)
}
}

1 Solution

Accepted Solutions
MarcoBoeringa
MVP Regular Contributor

I have updated the code. I noticed the "isLeftValue" and "isRightValue" are only initialized inside an 'if', that may not be entered by the code based on the 'if' condition. Putting the variable initialization outside the 'if' should work.

 

// This rule will calculate the left and right municipality for a road.
// It determines if the road is completely within a intersectingArea or falls on the edge of a intersectingArea and updates the appropriate values on the road
// Define the Road Centerline fields
var jurisleft_field = "L_JURISDIC";
var jurisright_field = "R_JURISDIC";
// Define the Geopolitical Areas fields
var jurisname_field = "JURISD";
// Get the intersecting Geopolictical Areas
var intersectingAreas = Intersects(FeatureSetByName($datastore, "PublicSafetyGIS_Data.PUBLICSAFETYGIS.Test_Jurisdictions", [jurisname_field], true), $feature)
// This function will convert a polygon geometry to a polyline
function polygonToPolyline(p) {
var json = Dictionary(Text(p));
var polylineJSON = {
"paths": json["rings"],
"spatialReference": json["spatialReference"]
};
return Polyline(polylineJSON)
}
// Test if the road falls completely within a area and does not overlap any of the area's outline
// If it does update the left and right value to be equal to the area's value
var isWithin = false;
var partialOverlap = [];
for (var intersectingArea in intersectingAreas) {
if (Within($feature, intersectingArea)) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (!Overlaps($feature, line)) {
var jurisleft = intersectingArea[jurisname_field];
var jurisright = intersectingArea[jurisname_field];
isWithin = true;
break;
}
// Store any boundaries the line is partially within (overlaps some of the polygons intersectingArea)
else {
Push(partialOverlap, intersectingArea);
}
}
}
// If the road does not fall within a area, attempt to find any areas that it overlaps the outline
// Then test if the polygon is on the right or left side of the line and update the right or left value
var isRightValue = false;
var isLeftValue = false;
if (!isWithin) {
for (var intersectingArea in intersectingAreas) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (Within($feature, line)) {
// Offset the geometry to the right and test if it intersects the intersectingArea
var offset_geometry = Offset($feature, 5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisright = intersectingArea[jurisname_field];
isRightValue = true;
}
// Offset the geometry to the left and test if it intersects the intersectingArea
offset_geometry = Offset($feature, -5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisleft = intersectingArea[jurisname_field];
isLeftValue = true;
}
}
}
}
// If either the left or right value is not set we will loop through the partially within
var jurisleft = ""
var jurisright = ""
if (isLeftValue || isRightValue) {
for (var i in partialOverlap) {
var intersectingArea = partialOverlap[i];
var line = polygonToPolyline(Geometry(intersectingArea));
// Get the portion of the road that overlaps the polygon intersectingArea
var intersection_geometry = Intersection($feature, line);
// Offset this portion of the road to the right and test if it intersects the intersectingArea
var offset_geometry = Offset(intersection_geometry, 5);
if (Intersects(offset_geometry, intersectingArea) && !isRightValue) {
jurisright = intersectingArea[jurisname_field];
}
// Offset this portion of the road to the left and test if it intersects the intersectingArea
offset_geometry = Offset(intersection_geometry, -5);
if (Intersects(offset_geometry, intersectingArea) && !isLeftValue) {
jurisleft = intersectingArea[jurisname_field];
}
}
}
return {
"result": {
"attributes":
Dictionary(
jurisleft_field, jurisleft,
jurisright_field, jurisright
)
}
}

 

 

View solution in original post

4 Replies
MarcoBoeringa
MVP Regular Contributor

I have updated the code. I noticed the "isLeftValue" and "isRightValue" are only initialized inside an 'if', that may not be entered by the code based on the 'if' condition. Putting the variable initialization outside the 'if' should work.

 

// This rule will calculate the left and right municipality for a road.
// It determines if the road is completely within a intersectingArea or falls on the edge of a intersectingArea and updates the appropriate values on the road
// Define the Road Centerline fields
var jurisleft_field = "L_JURISDIC";
var jurisright_field = "R_JURISDIC";
// Define the Geopolitical Areas fields
var jurisname_field = "JURISD";
// Get the intersecting Geopolictical Areas
var intersectingAreas = Intersects(FeatureSetByName($datastore, "PublicSafetyGIS_Data.PUBLICSAFETYGIS.Test_Jurisdictions", [jurisname_field], true), $feature)
// This function will convert a polygon geometry to a polyline
function polygonToPolyline(p) {
var json = Dictionary(Text(p));
var polylineJSON = {
"paths": json["rings"],
"spatialReference": json["spatialReference"]
};
return Polyline(polylineJSON)
}
// Test if the road falls completely within a area and does not overlap any of the area's outline
// If it does update the left and right value to be equal to the area's value
var isWithin = false;
var partialOverlap = [];
for (var intersectingArea in intersectingAreas) {
if (Within($feature, intersectingArea)) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (!Overlaps($feature, line)) {
var jurisleft = intersectingArea[jurisname_field];
var jurisright = intersectingArea[jurisname_field];
isWithin = true;
break;
}
// Store any boundaries the line is partially within (overlaps some of the polygons intersectingArea)
else {
Push(partialOverlap, intersectingArea);
}
}
}
// If the road does not fall within a area, attempt to find any areas that it overlaps the outline
// Then test if the polygon is on the right or left side of the line and update the right or left value
var isRightValue = false;
var isLeftValue = false;
if (!isWithin) {
for (var intersectingArea in intersectingAreas) {
var line = polygonToPolyline(Geometry(intersectingArea));
if (Within($feature, line)) {
// Offset the geometry to the right and test if it intersects the intersectingArea
var offset_geometry = Offset($feature, 5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisright = intersectingArea[jurisname_field];
isRightValue = true;
}
// Offset the geometry to the left and test if it intersects the intersectingArea
offset_geometry = Offset($feature, -5);
if (Intersects(offset_geometry, intersectingArea)) {
var jurisleft = intersectingArea[jurisname_field];
isLeftValue = true;
}
}
}
}
// If either the left or right value is not set we will loop through the partially within
var jurisleft = ""
var jurisright = ""
if (isLeftValue || isRightValue) {
for (var i in partialOverlap) {
var intersectingArea = partialOverlap[i];
var line = polygonToPolyline(Geometry(intersectingArea));
// Get the portion of the road that overlaps the polygon intersectingArea
var intersection_geometry = Intersection($feature, line);
// Offset this portion of the road to the right and test if it intersects the intersectingArea
var offset_geometry = Offset(intersection_geometry, 5);
if (Intersects(offset_geometry, intersectingArea) && !isRightValue) {
jurisright = intersectingArea[jurisname_field];
}
// Offset this portion of the road to the left and test if it intersects the intersectingArea
offset_geometry = Offset(intersection_geometry, -5);
if (Intersects(offset_geometry, intersectingArea) && !isLeftValue) {
jurisleft = intersectingArea[jurisname_field];
}
}
}
return {
"result": {
"attributes":
Dictionary(
jurisleft_field, jurisleft,
jurisright_field, jurisright
)
}
}

 

 

SarahIerley1
New Contributor II

Thank you for your response! I applied your edited code and I am now getting the error: Object not found jurisjeft, Script line 84

0 Kudos
MarcoBoeringa
MVP Regular Contributor

That is essentially the same problem. Uninitialized variable. I have again updated the code, see the code sample in the previous post, it will now set the 'jurisleft' and 'jurisright' variables to empty string "" if no jurisdiction is found. You may consider replacing that with null. Change the variable initialization to:

 

var jurisleft = null
var jurisright = null

 

Who actually developed this code? Is it part of an ESRI solution, or was this developed in-house? These errors are really basic ones, that shouldn't be in released and properly tested code.

0 Kudos
SarahIerley1
New Contributor II

Thank you so much!  That did the trick.  This seriously saved me!  This was code that one of my coworkers found on this site, I believe.  None of us are coders unfortunately.  

0 Kudos