Select to view content in your preferred language

Attribute Rule: Automatically update related table based on intersections?

224
0
04-04-2024 02:49 PM
VinceE
by
Occasional Contributor II

I have a polygon feature class related to a table via Relationship Class (one-to-many, GlobalID in polygon related to a GUID in the table, field called REL_ZONE_ID). I am updating this related table manually. The related table lists the segments that intersect the polygons (polygon labels shown here are a shorthand version of the full Globals, full version listed in the REL_ZONE_ID field). So basically it's a table that keeps track of the lines intersecting a given polygon.

I am moving between Pro 3.1 and 3.2, so I can't yet reliably use "FeatureSetByRelationshipClass".

The table shown is the related table, storing a record of each line that intersects a polygon.The table shown is the related table, storing a record of each line that intersects a polygon.

 

I would like to do this automatically with a Calculation Attribute Rule, at INSERT, UPDATE, and DELETE. My approach:

  • Get a FeatureSet of the records that are already related to the original polygon.
  • Mark them for deletion by adding to a "delete" array.
  • Review all line segments (time consuming?); if any intersect the newly updated polygon, append segment ID record to a new "adds" array.
  • Return an "edit" record to delete the originally related records, and add the newly intersected records.

The below does this, but it's a hack--I needed to add in the if (editType != "DELETE") because otherwise when a polygon was deleted, the rule would remove the currently related records as expected, but then add the same records back in (except with a NULL related ID, since the polygon would have been deleted, and not have an ID anymore).

I thought I wouldn't need to do the if DELETE because I thought the "Intersect()" was being called on the $feature, and not the $originalFeature. So I assumed that if a feature was deleted, there couldn't be any intersections with the $feature, and therefore no records would be added to the "adds" array.

To reiterate, the code below does exactly what I want, I just can't do it without the special "DELETE" caveat.

 

/*MANAGE RELATED SEGMENTS*/
//---------------------------------------------------------------------//
// INPUTS
var proVersion = 3.1
var editType = $editContext.editType 

// Get original/current polygon feature.
var zone = $originalFeature
var newZone = $feature

// Get all polylines in SEGMENTS feature class. Geometry required for 'Intersects()'.
var segmentsFS = FeatureSetByName($datastore, "SEGMENT", ["SEG_ID"], true)

if (proVersion >= 3.2) {
    // Get table records that are related to the current POLYGON feature.
    var relRecordsFS = FeatureSetByRelationshipClass(zone, "ZONE_SEG_RELATION", ["*"], false)
} else {
    // Get all records from the related table.
    // Filter when related ID matches GlobalID of orignal POLYGON feature.
    var relRecsFS = FeatureSetByName($datastore, "SEG_TBL", ["*"], false)
    var zoneGlobalID = $originalFeature.GlobalID
    var relRecordsFS = Filter(relRecsFS, 'REL_ZONE_ID = @zoneGlobalID')
}

// MAIN ------------------------------------------------------------------------------- #
// Iterate over the currently related table records, add all 'deletes' array.
var deletes = []
for (var relatedRecord in relRecordsFS) {
    Console(`ALREADY RELATED / TO DELETE: ${relatedRecord.SEG_ID} | ${relatedRecord.GlobalID}`)
    Push(deletes, {"globalID": relatedRecord.GlobalID})
}
// For every SEGMENT line, check if it intersects current ZONE. If so, add to 'adds' array.
var adds = []
if (editType != "DELETE") {  // If a feature is deleted, there won't be anything getting added. "newZone" alone isn't working?
    for (var seg in segmentsFS) {
        if (Intersects(newZone, seg)) {
            Console(`FOUND NEW INTERSECTION / TO ADD: ${seg.SEG_ID}`)
            Push(adds, {"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID} })
        }
    }
}

Console(`\nDELETE RECORDS:\n${deletes}\n\nADD RECORDS:\n${adds}`)
return {
    'edit': [{
        'className': 'SEG_TBL',  // This is the TABLE where the edits are occurring.
            'deletes': deletes,  // [{'globalID': XXX}, {'globalID': YYY}]  <-- 2 delete examples
            'adds': adds  // [{"attributes": {"REL_ZONE_ID": newZone.GlobalID, "SEG_ID": seg.SEG_ID}}, {"attr...] <-- 2 adds
    }]
}

 

 

Tags (2)
0 Kudos
0 Replies