// 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 munileft_field = "munileft"; var muniright_field = "muniright"; // Define the Geopolitical Areas fields var name_field = "name"; // Define the type field and the valid types for a municipality in Geopolitical Areas var type_field = "geotype"; var valid_types = ["City", "Minor Civil Division", "Town", "Township", "Village"]; // Return if the geometry did not change if ($editcontext.editType == "UPDATE" && Equals(Geometry($feature), Geometry($originalFeature))) return; // Test if the feature has the left and right field, if it is missing either return if (!HasKey($feature, munileft_field) || !HasKey($feature, muniright_field)) return; var munileft = $feature[munileft_field]; var muniright = $feature[muniright_field]; // Get the intersecting Geopolictical Areas var intersectingAreas = Intersects(Filter(FeatureSetByName($datastore, "GeopoliticalArea", [name_field], true), type_field + " IN @valid_types"), $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 munileft = intersectingArea[name_field]; var muniright = intersectingArea[name_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, 100); if (Intersects(offset_geometry, intersectingArea)) { var muniright = intersectingArea[name_field]; isRightValue = true; } // Offset the geometry to the left and test if it intersects the intersectingArea offset_geometry = Offset($feature, -100); if (Intersects(offset_geometry, intersectingArea)) { var munileft = intersectingArea[name_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, 100); if (Intersects(offset_geometry, intersectingArea) && !isRightValue) { var muniright = intersectingArea[name_field]; } // Offset this portion of the road to the left and test if it intersects the intersectingArea offset_geometry = Offset(intersection_geometry, -100); if (Intersects(offset_geometry, intersectingArea) && !isLeftValue) { var munileft = intersectingArea[name_field]; } } } } return { "result": { "attributes": Dictionary( munileft_field, munileft, muniright_field, muniright ) } }