Delete feature using attribute rules?

538
3
Jump to solution
09-30-2021 04:48 AM
RPGIS
by
Regular Contributor

Hi,

I am trying to delete a feature based on the length of time that passes. I am hoping to fully automate a project that we are working on so that if a certain number of days pass, then that particular feature gets deleted. I hope someone can assist. I am relatively new to attribute rules but the few that I have implemented, it has helped significantly with automating tasks.

var From = Date($feature.OutageEnd)
var To = Date(Now())
var DaysAfterOutage = DateDiff(To, From, 'days')

//Constants for defining which features to delete//
var UniqueID = $feature.GlobalID

//Define which features to delete after a given amount of time//
if (DaysAfterOutage >= 7){
return {
'edit':[
'deletes':[
{UniqueID}
]
]
}
}

Any help on this would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Regular Contributor

Other than scheduling a python script to run periodically, I know of no way to do that completely automatically.

I think the Attribute Rule approach comes pretty close. All you have to do to execute it is insert a new feature or update an existing one.


Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
JohannesLindner
MVP Regular Contributor

There are a number of things that would be good in this scenario but aren't possible:

  • You can't auto delete a feature after a certain time
  • You can't run a rule on a whole table, only on features
  • You can't run a rule on demand, only when you insert/update/delete a feature (for calculation attribute rules)

So fully automated deletion won't be possible. But there are a few things that could make it easier:

  • You cold symbolize features that are ready to delete differently using symbol property connections.
    For example, you could symbolize "normal" features blue and deletable features red:

 

return IIF(DateDiff(Now(), $feature.OutageEnd, "days") >= 7, "red", "blue")​

 

  • You can use a python script to delete these features for you e.g. each night.
  • You can use an Attribute Rule that fires whenever you insert or update a new feature:

 

// Calculation Attribute rule on your feature class
// Some editable field
// Triggers: Insert, Update
// Exclude: True

// get all entries of your feature class
var fs = FeatureSetByName($datastore, "FeatureClass")

// if OutageEnd can be null, filter these values out
fs = Filter(fs, "OutageEnd IS NOT NULL")

// You could filter for the date, but the sql depends on your dbms, so I'm not going to do that. Instead, I'm going to test each feature using Arcade.
var deletes = []
for(var f in fs) {
  if(DateDiff(Now(), f.OutageEnd, "days") >= 7) {
    Push(deletes, {"globalID": f.GlobalID})
  }
}

return {
  "result": $feature.YourReturnField,
  "edit": [
    {
      "className": "FeatureClass",
      "deletes": deletes
    }
  ]
}​

 


Have a great day!
Johannes
0 Kudos
RPGIS
by
Regular Contributor

Thanks @JohannesLindner,

I was really hoping that deletions on the fly were possible. I know it is very easy to do using python, since I have built several tools to delete an existing feature if it already exists. I am surprised, however, that automated deletion isn't possible even though attribute changes can be made on the fly. So I assumed that there was a way to set up a rule to automatically delete a record if the criteria were met.

0 Kudos
JohannesLindner
MVP Regular Contributor

Other than scheduling a python script to run periodically, I know of no way to do that completely automatically.

I think the Attribute Rule approach comes pretty close. All you have to do to execute it is insert a new feature or update an existing one.


Have a great day!
Johannes
0 Kudos