Select to view content in your preferred language

sequential id for each class which contain couple of hundred of points.

3421
15
09-12-2013 10:30 PM
nadeemfareed
Deactivated User
hello python hero and legends.
                                         i have more then 23 routes generated by using network analyst. later on, i have converted the route into point shapefile. new formed point shapefile have many hundred of points for each routes. i have created a  new column and i want to populate that column with sequential numbers for each point. a python script is available for such calculation but this scripts generates sequential number starting from 1st point to last one regardless the route class. i am looking for some advance calculation. i want sequential numbering for each route in new column. like
route type seq.no
1              point             1
1              point             2
1              point             3
1              point             4
2              point             1
2              point             2
2              point             3
.             
.
.
.
                so on.
i need all this is a single python script.
looking forward your great contribution.

Nadeem Fareed
Tags (2)
0 Kudos
15 Replies
nadeemfareed
Deactivated User
hello python hero and legends.
                                         i have more then 23 routes generated by using network analyst. later on, i have converted the route into point shapefile. new formed point shapefile have many hundred of points for each routes. i have created a  new column and i want to populate that column with sequential numbers for each point. a python script is available for such calculation but this scripts generates sequential number starting from 1st point to last one regardless the route class. i am looking for some advance calculation. i want sequential numbering for each route in new column. like
route type seq.no
1              point             1
1              point             2
1              point             3
1              point             4
2              point             1
2              point             2
2              point             3
.             
.
.
.
                so on.
i need all this is a single python script.
looking forward your great contribution.

Nadeem Fareed
there are things known and there are things unknown inbetween are doors
0 Kudos
RichardFairhurst
MVP Alum
hello python hero and legends.
                                         i have more then 23 routes generated by using network analyst. later on, i have converted the route into point shapefile. new formed point shapefile have many hundred of points for each routes. i have created a  new column and i want to populate that column with sequential numbers for each point. a python script is available for such calculation but this scripts generates sequential number starting from 1st point to last one regardless the route class. i am looking for some advance calculation. i want sequential numbering for each route in new column. like
route type seq.no
1              point             1
1              point             2
1              point             3
1              point             4
2              point             1
2              point             2
2              point             3
.             
.
.
.
                so on.
i need all this is a single python script.
looking forward your great contribution.

Nadeem Fareed


I have adapted the standard Python AutoIncrement Field Calculation to use a dictionary to keep track of the Route ID count, which I believe will do what you want:

Parser:
Python

Show Codeblock:
Checked

Pre-Logic Script Code:
recDict = {}
def autoIncrement(RID):
  global recDict
  pStart = 1 # adjust start value, if required 
  pInterval = 1 # adjust interval value, if required
  if RID in recDict:
    recDict[RID] = recDict[RID] + pInterval 
  else:
    recDict[RID] = pStart 
  return recDict[RID]


Expression:
autoIncrement(!RID!) # Put your Route ID field name in the parentheses.

The records will sequence in the order that they are encountered in the point fc, which will be the order of the points along the route if that is the order they are stored in the point fc by your route conversion to point method.
0 Kudos
RichardFairhurst
MVP Alum
I had the chance to test my code and it worked.  So this is the calculation you need and all you should need to do is match your Route ID case field.  I did not test a Null value in the RID field, so it is possible that if Null values can be in your Route ID field that the code would need to be modified slightly.
0 Kudos
nadeemfareed
Deactivated User
dear sir
        see the attachment for detail. its not working.
0 Kudos
RichardFairhurst
MVP Alum
dear sir
        see the attachment for detail. its not working.


Screen shot the actual field calculator screen.  It does work, so something is wrong in the way it was transferred, but I can't tell from the result screen, since I don't use that to evaluate failures.  Maybe at work I can recheck it.  But the calculation works.

The expression should not contain the word "def " in front of the AutoIncrement(!CID!).  "def " should only occur in the codeblock.  Did you type that word in the expression, or did the results tab insert that word in the expression?

Edit:  After running the calculation and looking at the results tab inputs, I have confirmed that you should not have the word "def " in the expression portion of the calculation, only in the codeblock portion.
0 Kudos
nadeemfareed
Deactivated User
hello everyone
the script given in response to my post by rfairhur24
is not working well i tried it many ways i request rfairhur24, please try it and then send me back. i try hard to get job done with this script but its not working. i also request the other user or expert to please do me favor and just post something different and new for this task. i also request rfairhur24 please provide me stand alone python script that can work like a tool, any thing that could work batter. there is another option that i have
each route have single unique ID named CID.
for every route CID is different like for route no 1. points CID is 1, for route no.2 CID is 2 same is true for 23 routes.
i am looking for a scripts that generate the sequential ID for each CID.

best regards
Nadeem Fareed
0 Kudos
RichardFairhurst
MVP Alum
hello everyone
the script given in response to my post by rfairhur24
is not working well i tried it many ways i request rfairhur24, please try it and then send me back. i try hard to get job done with this script but its not working. i also request the other user or expert to please do me favor and just post something different and new for this task. i also request rfairhur24 please provide me stand alone python script that can work like a tool, any thing that could work batter. there is another option that i have
each route have single unique ID named CID.
for every route CID is different like for route no 1. points CID is 1, for route no.2 CID is 2 same is true for 23 routes.
i am looking for a scripts that generate the sequential ID for each CID.

best regards
Nadeem Fareed


Do me a favor and screen shot your actual calculation input as I requested.  The only test you showed me a screen shot for had a definite error in the way you transferred my code to the Field Calculator.  Since the code I provided works when I input it into the field calculator (tested on over 10,000 unique Route IDs each with between 1 and 300 sequenced events, so more than an adequate test to say it should work for your small data set), I want to eliminate user failure as the reason for your failed attempts to make my code work and a screen shot would be the best way to do that.  I would screen shot my calculation for you, but at the moment I cannot access my system remotely to do that.

Are you sure there are absolutely no Null values in the CID field?  If there were the calculation would fail.  So on the off chance that is the problem I have added code below to deal with that situation.

Any standalone script I would write would be based on the same code, so before I go to the trouble of trying to come up with any other script code try setting the calculation up exactly as shown below and screen shot your Field Calculator set up and results so I can verify you correctly transferred the calculation to the Field Calculator first.

It should be:

Parser: Python

Show Codeblock:  Checked

Pre-Logic Script Code:
recDict = {}
def autoIncrement(CID):
  global recDict
  pStart = 1 # adjust start value, if required 
  pInterval = 1 # adjust interval value, if required
  if not CID:
    CID = "Null"
  if CID in recDict:
    recDict[CID] = recDict[CID] + pInterval 
  else:
    recDict[CID] = pStart 
  return recDict[CID]


Expression:  autoIncrement(!CID!)
0 Kudos
RichardFairhurst
MVP Alum
There is a geoprocessing way to do this.

1.  If your data is not already sorted by CID values, first run the Sort tool with the sort fields being the CID field and the ObjectID field.  I will call the output "Sorted"

2.  On the sorted output add a long field called something like Sequence.

3.  Calculate the Sequence field to be equal to the ObjectID.

4.  Do a Summary Statistics of the Sorted feature class using the CID field as the case field and the Min of the Sequence field as the summary.  I will call the output "Summary".

5.  Make the sorted output a Feature Layer.

6.  Join the sorted feature layer as the target and the summary as the join table on the common CID fields.

7.  Calculate the Sequence field to be:

Sorted.Sequence - Summary.Min_Sequence + 1

8.  Remove the Join.

The Sequence field will now contain a sequential ID that restarts the numbering at 1 for each CID value.  Null values in the CID field would potentially mess this up, but I think it would handle that correctly.
0 Kudos
RichardFairhurst
MVP Alum
I just added the calculation to a model and it performed perfectly for 30,000+ routes each with between 1 to 182 sequenced events.  Here are screen shots of my calculation (part 1 and 2 to scroll the full code in the code block).  So if you duplicate that set up exactly it should work.
0 Kudos