Select to view content in your preferred language

Add Parent Geometry to A Related Record

426
14
Jump to solution
03-17-2025 05:53 PM
PhilipShutler
Occasional Contributor

Here is the scenario I am trying to build out (tried to keep it short - included pics to help out): 

I have two polygon feature classes and a nonspatial table.

The first polygon feature class is called "Grid_Test" - it's a tesselation feature of grid cells primarily used for symbology and tracking where crews are in the field.

Then, I have a related nonspatial table called "Work _Items" that stores the different work that is applied in a grid cell. This has a one-to-many relationship with the grid features. One grid cell to many work items. This has an attribute rule that grabs the gird_id from Grid_Test and is set to be the first rule that is run. This rule works perfectly and is shown below for reference.

var rel_guid = $feature.REL_GUID

// search for related features
var Grid_feats = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], false)

var filtered_grid = Filter(Grid_feats, "GLOBALID= @rel_guid")

// no related features found
if(Count(filtered_grid) == 0) {
  return null
}

// related feature(s) found -> return grid id of the first one

else {
    var first_grid = First(filtered_grid);
    return first_grid["GRID_ID"];
}

 

Finally, I have a second polygon feature called "work_grid_related_polygons". This is where the issue occurs. The intent of this polygon feature class is to be one-to-one with work items and to create polygons as work items are created using the geometry of the parent grid cell. They will be stacked on top of one another as multiple work records are entered for a grid cell, but that's ok; I need this for reporting.  I intended to have an attribute rule take care of the transfer of attributes from work items and retrieve the geometry of the parent grid cell and assign it to each record that is added to the work_grid_related_polygons

Below is a graphic of my intended data model - Hopefully it helps.

datamodel_example.png

 

This is the arcade code I am running to try and accomplish creating the related polygons:

// related guid from parent in nonspatial table
var rel_guid = $feature.REL_GUID

// bring in grid feature class to get grid id
var Grid_feats = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], false)

// use filter technique to find matching record
var filtered_grid_for_grid_id = Filter(Grid_feats, "GLOBALID= @rel_guid")

var grid_id_value = null;

// check for no related features found - this should not occur cause a record is being added as related feature but it is a check
if(Count(filtered_grid_for_grid_id) == 0) {
  return {
        "result": "No matching grid found for the given GRID_ID - first step."
    };
}


// related feature(s) found -> return grid id of the first one
else {
    var first_grid_for_id = First(filtered_grid_for_grid_id);
    var grid_id_value = first_grid_for_id["GRID_ID"];
}


// Access the grid features agaib to get geometry - see parameter set to true for geometries
var grid_features = FeatureSetByName($datastore, "Grid_Test", ['*'], true);

// Filter the grid features to find the one matching Grid ID
var filtered_grids = Filter(grid_features, "GRID_ID = @grid_id_value");


// Check if a matching grid was found - again this should not occur because these are related features
if (Count(filtered_grids) == 0) {
    return {
        "result": "No matching grid found for the given GRID_ID. - Second Step"
    };
}

// Retrieve the first grid feature and check
var first_grid = First(filtered_grids);
if (first_grid == null) {
    return {
        "result": "First grid feature is null."
    };
}

// Get the geometry of the first grid feature
var grid_geom = Geometry(first_grid);
if (grid_geom == null) {
    return {
        "result": "Grid geometry is null."
    };
}


// Ensure the geometry object is valid before proceeding
if (TypeOf(grid_geom) != "Polygon" && TypeOf(grid_geom) != "Polyline" && TypeOf(grid_geom) != "Point") {
    return {
        "result": "Invalid geometry type: " + TypeOf(grid_geom)
    };
}

// Assemble the edit that creates the related polygon using grid geometry and attributes from the visit
return {
    "edit": [{
        "className": "Grid_Test_RelatePolygons",
        "adds": [{
            "attributes": {
                "grid_id": grid_id_value // value to hold as an attribute transfer as well
            },
            "geometry": grid_geom 
        }]
    }]
};

 

This code runs but no records are produced in work_grid_related_polygons. The result that comes out it is "No matching grid found for the given GRID_ID - first step". This is odd because I used the console to do checks, and they returned valid values for attributes and geometry. Also I am able to get the grid_id populated in my nonspatial table with no issues using the same filter technique from the first attribute rule. I made sure to exclude from application evaluation in the settings.

I'm trying to figure out if i have left out some logic in my scenario to try and produce polygons from related features. I think the issue may be with the sequencing of when the transactions occur in the database for related records. 

What's odd is the grid_id comes over fine, and as the first rule in the order of operations, it should be there to be picked up and then used for the second rule. I know I did a little overkill by using using the relate guid to get the grid_id at first, but I did so to make sure I was getting the grid_id from the source. It feels like a timing issue when it comes to to how the database transfers a primary to foreign key in a relationship class.  

The inspiration came from @HusseinNasser2 examples using geometry in other attribute rules. Any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

Try this, enable the update trigger and then just check when the REL_GUID changes

 

if ($originalfeature.REL_GUID == $feature.REL_GUID)
  return
/// related guid from parent in nonspatial table
var rel_guid = $feature.REL_GUID;

// bring in grid feature class to get grid id
var grid_fs = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], True);

// use filter technique to find matching record
var filtered_grid_fs = Filter(grid_fs, "GLOBALID = @REL_guid");

var first_grid_feat = First(filtered_grid_fs);
if (IsEmpty(first_grid_feat))
{
    return;
}
var grid_id_value = first_grid_feat["GRID_ID"];

// Filter the grid features to find the one matching Grid ID
var filtered_grids = Filter(grid_fs, "GRID_ID = @grid_id_value");

// Retrieve the first grid feature and check
var first_grid = First(filtered_grids);
if (IsEmpty(first_grid))
{
    return;
}


// Assemble the edit that creates the related polygon using grid geometry and attributes from the visit
return {
    "edit": [{
        "className": "Grid_Test_RelatePolygons",
        "adds": [{
            "attributes": {
                "grid_id": grid_id_value // value to hold as an attribute transfer as well
            },
            "geometry": Geometry(first_grid) 
        }]
    }]
};

View solution in original post

0 Kudos
14 Replies
MikeMillerGIS
Esri Frequent Contributor

Try this:

Should be able to reuse the FeatureSet

Dont call Count if you are going to call first.  

// related guid from parent in nonspatial table
var rel_guid = $feature.REL_GUID;

// bring in grid feature class to get grid id
var grid_fs = FeatureSetByName($datastore, "Grid_Test", ["GLOBALID", "GRID_ID"], True);

// use filter technique to find matching record
var filtered_grid_fs = Filter(grid_fs, "GLOBALID = @REL_guid");

var first_grid_feat = First(filtered_grid_fs);
if (IsEmpty(first_grid_feat))
{
    return;
}
var grid_id_value = first_grid_feat["GRID_ID"];

// Filter the grid features to find the one matching Grid ID
var filtered_grids = Filter(grid_fs, "GRID_ID = @grid_id_value");

// Retrieve the first grid feature and check
var first_grid = First(filtered_grids);
if (IsEmpty(first_grid))
{
    return;
}


// Assemble the edit that creates the related polygon using grid geometry and attributes from the visit
return {
    "edit": [{
        "className": "Grid_Test_RelatePolygons",
        "adds": [{
            "attributes": {
                "grid_id": grid_id_value // value to hold as an attribute transfer as well
            },
            "geometry": Geometry(first_grid) 
        }]
    }]
};

 

0 Kudos
PhilipShutler
Occasional Contributor

I replaced my old script with this one and got no errors but the geometry/attribute is still not coming over and transferring to Grid_Test_RelatePolygons.

Whats odd is I know there is a connection to the parent feature because grid_id is populated automatically when I add a new record in the non-spatial table (WorkItemsTest) from the first attribute rule I posted.

See the screenshot of the related record being added to the non-spatail table (WorkItemsTest) - and getting the grid id from the parent feature but not adding anything to Grid_Test_RelatePolygons (symbology is blue)

Attribute Rules Test - Screenshot.png

 

you can see in the attribute table for Grid_Test_RelatePolygons that nothing is being added too. 

Highlight area shows no records.Highlight area shows no records.

 

0 Kudos
MikeMillerGIS
Esri Frequent Contributor
If you want to email me a repo dataset, happy to dive into it if you want mmiller@esri.com
0 Kudos
PhilipShutler
Occasional Contributor

Sending it your way.

0 Kudos
PhilipShutler
Occasional Contributor
0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Worked for me.  How are you adding the rel_guid?  The rule only fires on insert, so if you add the record and it saves, then you add the rel_guid, that is an update.  You might need to add Update trigger event and then code for when that field changes using the $originalfeature.rel_guid != $feature.rel_guid

0 Kudos
PhilipShutler
Occasional Contributor

rel_guid is auto-populated from the relationship class I have set between Grid_Test and Work _Items.

Grid_Test is the origin, and Work _Items is the destination in the relationship class. GlobalID is my primary key and  rel_guid  is my foreign key. 

0 Kudos
PhilipShutler
Occasional Contributor

PhilipShutler_0-1742343316617.png

these are the properties of my relationship class.

 

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

Ahh, that explains it.  I recall an issue with this workflow.  The record is created, then the rel guid is updated.  

0 Kudos