Attribute Rule for Sequential Numbers by Feature ID

190
4
Jump to solution
2 weeks ago
Labels (2)
JwHayes
New Contributor III

Hello,

I have 2/3 of the code someplete. The middle portion has stumped me. I have an immediate insert rule that inserts the section tile name when a new point feature is created.

var getSec = FeatureSetByName($datastore, "Section", ["TILE_NAME"])
var getSecInt = Intersects(getSec, $feature)
var sec = First(getSecInt)

if (sec == null) return {"errorMessage": "Section Not Found"}

return sec.TILE_NAME

 

Tha last part checks for null values and inserts the next sequential value. 

// get the latest feature with a ID
var last_feature = First(OrderBy(Filter($featureset, "ID IS NOT NULL"), "OBJECTID DESC"))
// get the last CW_ID and add 1
var seq = IIf(last_feature == null, 0, last_feature.ID) + 1

return Number(seq)

 

This works well for numbering all the features but what I need is sequential numbering by tile name. In the example below I'd like all the number on the left to be 4, 5, 6 and the numbers on the be 1 and 2. Thanks for your help!

JwHayes_0-1714753959465.png

 

 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

Quick modification to accomplish that.

 

 

var intpoly = FeatureSetByName($datastore, "Polygons_1")            // load polygon dataset
var pointvals = FeatureSetByName($datastore,"Dedications",['ID'],false)  // load point dataset

var polyname = ''
var polyvals = ''

for( var f in intpoly){                                             // loop through polys and see which one intersects
   if(Intersects($feature, f)){
       var polyname = f.TITLE_NAME}                                 // set polyname variable to the TITLE_NAME of intersecting polygon
}
if (!IsEmpty(polyname)){
    var polyvals = Filter(pointvals, `TITLE_NAME = '${polyname}'`)       // If not empty, filter points by intersecting poly name
}
var numarray = []
for (var n in polyvals){
    Push(numarray, n.ID)                              // push all ID for points with TITLE_NAME matching they polygon name
}
var newID = Max(numarray)+1                           // get the max ID number and add 1 to it
return {                                              // return a dictionary that updates TITLE_NAME and ID in the points layer.
        "result": {
            "attributes": {
                "TITLE_NAME": polyname,
                "ID": newID
            },
        }
    }

 

 

Also, since this gets the values from the ID field from the Dedications point layer by polygon, you will need to have at least one point with the polygon name and an ID >= 0.  Otherwise, if you click on 'PolyA', and there is no entry for PolyA in the point file yet, the array of ID's will be empty, so it can't increment it by 1. 

Suspect you could also do some error checking here where it appends the TITLE_NAME and if the newID is null, set it to 1, but I normally don't have that many polygons, so I just create one point in each with TITLE_NAME for each polygon, and ID of 0.   These 'dummy' points don't really need to fall within the polygons.  I normally make them off the side of my map area, and can be deleted once there are ID's for each TITLE_NAME.  (Also, no error checking here.  This assumes that each point added intersects ONLY 1 polygon (no overlapping polygons)

Hope this makes sense and gets you going in the right direction.

R_

 

View solution in original post

4 Replies
RhettZufelt
MVP Frequent Contributor

Not sure I understand the question, but You might look at this post to see how I have dealt with similar need.

This shows one way to increment sequential numbers with a text component.

R_

0 Kudos
JwHayes
New Contributor III

Rhett, 

Thanks for the reply. Let's see if I can give you a better explanation.

Let's say I have two polygone (A,B) and a point feature class (Dedications). The Dedications feature class has two fields. "TILE_NAME" and "ID." 

I want to add 3 dedication points to polygon A. When I add the first point the rule will append 'A' from the polygon to the the Dedications "TILE_NAME" Field and create a sequential number, in this case "1" to the the ID field. When I add points two and three "A" is applied to both points "TILE_NAME" fields, but the next sequential numbers are "2" and "3." 

Now I want to add a point to B. B is appended to "TILE_NAME" but "1" is created for the "ID" field. Then if I create another point in polygon A, the next ID would be "4." 

I hope this helps.

0 Kudos
RhettZufelt
MVP Frequent Contributor

Quick modification to accomplish that.

 

 

var intpoly = FeatureSetByName($datastore, "Polygons_1")            // load polygon dataset
var pointvals = FeatureSetByName($datastore,"Dedications",['ID'],false)  // load point dataset

var polyname = ''
var polyvals = ''

for( var f in intpoly){                                             // loop through polys and see which one intersects
   if(Intersects($feature, f)){
       var polyname = f.TITLE_NAME}                                 // set polyname variable to the TITLE_NAME of intersecting polygon
}
if (!IsEmpty(polyname)){
    var polyvals = Filter(pointvals, `TITLE_NAME = '${polyname}'`)       // If not empty, filter points by intersecting poly name
}
var numarray = []
for (var n in polyvals){
    Push(numarray, n.ID)                              // push all ID for points with TITLE_NAME matching they polygon name
}
var newID = Max(numarray)+1                           // get the max ID number and add 1 to it
return {                                              // return a dictionary that updates TITLE_NAME and ID in the points layer.
        "result": {
            "attributes": {
                "TITLE_NAME": polyname,
                "ID": newID
            },
        }
    }

 

 

Also, since this gets the values from the ID field from the Dedications point layer by polygon, you will need to have at least one point with the polygon name and an ID >= 0.  Otherwise, if you click on 'PolyA', and there is no entry for PolyA in the point file yet, the array of ID's will be empty, so it can't increment it by 1. 

Suspect you could also do some error checking here where it appends the TITLE_NAME and if the newID is null, set it to 1, but I normally don't have that many polygons, so I just create one point in each with TITLE_NAME for each polygon, and ID of 0.   These 'dummy' points don't really need to fall within the polygons.  I normally make them off the side of my map area, and can be deleted once there are ID's for each TITLE_NAME.  (Also, no error checking here.  This assumes that each point added intersects ONLY 1 polygon (no overlapping polygons)

Hope this makes sense and gets you going in the right direction.

R_

 

JwHayes
New Contributor III

Rhett,

That pretty much did the trick - thank you! 😁

I did make a couple adjustments (lines 20,21 and 27). Added  IsEmpty to assign a value of 1 if NULL, and converted newID value to integer to match my field type. Oh, and changed "TITLE_NAME" to "TILE_NAME."  🙂

 

var intpoly = FeatureSetByName($datastore, "Section")  // load polygon dataset
var pointvals = FeatureSetByName($datastore,"Dedication",['ID'],false)  // load point dataset

var polyname = ''
var polyvals = ''

for( var f in intpoly){    // loop through polys and see which one intersects
   if(Intersects($feature, f)){
       var polyname = f.TILE_NAME}  // set polyname variable to the TILE_NAME of intersecting polygon
}
if (!IsEmpty(polyname)){
    var polyvals = Filter(pointvals, `TILE_NAME = '${polyname}'`) // If not empty, filter points by intersecting poly name
}
var numarray = []
for (var n in polyvals){
    Push(numarray, n.ID)  // push all ID for points with TILE_NAME matching they polygon name
}

var newID = Max(numarray)+1 // get the max ID number and add 1 to it
if (IsEmpty(newID)){
    newID = 1  // makes newID 1 if value is NULL
}
return {    // return a dictionary that updates TILE_NAME and ID in the points layer.
        "result": {
            "attributes": {
                //"TILE_NAME": polyname,
                "ID": Floor(newID, 0) // change to number with zero decimal places
            },
        }
    }

  

0 Kudos