Select to view content in your preferred language

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

3431
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
yes it working now.
thanks buddy rfairhur24
its really very great job i really appriciate.
you did well.
one more task for you if you feel easy.
in next column i want to find the great value of sequential number generated through this script. for each route. like route 1 as great value or number of counts equal to 305. the next colum should populate with this number for each point of route 1. similarly for each rroute.
CID          SEQ           GREATEST VALUE
1              1                   4
1               2                   4
1               3                    4
1               4                     4

this is the final task for me. after this my application will be fully functional.
once again thanks for your golden time with me
0 Kudos
nadeemfareed
Deactivated User
dear rfairhur24.

i have some issues with script you have provided to me, now, its ok with arcgis 10 version but its giving error in 10.1 version. arcgis 10 have python version 2.6.5 while arcgis 10.1 have python version 2.7.2. so script in python 2.7.2 version show some error in script like indent error like somethin. please could you fix the compatibility errors please. i try my hard to do this but i am not hero of python like you.

thanks for response
0 Kudos
RichardFairhurst
MVP Alum
dear rfairhur24.

i have some issues with script you have provided to me, now, its ok with arcgis 10 version but its giving error in 10.1 version. arcgis 10 have python version 2.6.5 while arcgis 10.1 have python version 2.7.2. so script in python 2.7.2 version show some error in script like indent error like somethin. please could you fix the compatibility errors please. i try my hard to do this but i am not hero of python like you.

thanks for response


No error here and I originally wrote it using ArcGIS 10.1 and it is using Python 2.7.2 on my system, so that is not the problem.  The Python version makes no difference.  None of the code I wrote would break between those versions.

Again, you need to screenshot what you are doing for me to troubleshoot this, since there are no indent errors in the code I am using.  Even a single extra or missing space character will break the code, so I can only spot it from a screenshot.  My code could not cause an indent error as I wrote it and the ArcGIS version or Python version could not cause that kind of error.  So screenshot please.
0 Kudos
RichardFairhurst
MVP Alum
just to be clear, my code was built with Service Pack 2 for ArcMap 10.1, so make sure you have installed Service Pack 2.  I don't know that there is any effect of the Service Pack that might relate to your problem, but it is best to eliminate that possibility.

The code I provided before only ran as a Field Calculation.  It was not meant to be run as a Python script.

To add the new subroutine you want that will fill in the Greatest Value field the code needs to be converted to a standalone script.  A Field Calculation cannot efficiently summarize data and update records with the summary data, but a script can.

So here is a script that should replace the Field Calculation that will assign with both the Sequence field values and the Greatest Value field values.  You should no longer need the field calculation if you use this script.  Make sure you customize the variable values for your data file name and path and for the field list so that it exactly matches your data set up:

# Import the arcpy module
import arcpy

pStart = 1 # adjust start value, if required 

pInterval = 1 # adjust interval value, if required

# Initialize the sequence number dictionary and the Route ID variables
seqDict = {}

CID = ""

# Assign data and field list variables.
# Customize these variable inputs for your specific data
myData = r"C:\MyPath\MyData.shp"

fields = ["CID", "SEQUENCE", "GREATEST"]

# Step 1 - Use an update cursor to assign Sequence numbers
rows = arcpy.da.UpdateCursor(myData, fields)
for row in rows:
    if row[0] is None:
        CID = "Null"
    else:
        CID = row[0]
    if CID in seqDict:
        seqDict[CID] = seqDict[CID] + pInterval
    else:
        seqDict[CID] = pStart
    row[1] = seqDict[CID]
    rows.updateRow(row)
    del row
del rows
    
# Step 2 - Use an update cursor to assign the Greatest Value to all records
rows = arcpy.da.UpdateCursor(myData, fields)
for row in rows:
    if row[0] is None:
        CID = "Null"
    else:
        CID = row[0]
    row[2] = seqDict[CID]
    rows.updateRow(row)
    del row
del rows


I also want to make sure you understand that this code will only work if it runs on every record in your data and that it will overwrite any previously assigned sequence numbers if any records have changed.  To make the script continue any preexisting sequence numbering from any previous run of the tool the code would have to be changed to split Step 1 into two steps.  First a search cursor would have to populate the sequence dictionary with the maximum sequence number for all existing sequences and then an update cursor would have to run to actually update the new records that did not have sequences assigned to continue those sequences or begin new sequences for new routes.
0 Kudos
MathewCoyle
Honored Contributor
just to be clear, my code was built with Service Pack 2 for ArcMap 10.1, so make sure you have installed Service Pack 2.


Service pack 2? Did I miss a memo?
0 Kudos
RichardFairhurst
MVP Alum
Service pack 2? Did I miss a memo?


Nope, just wishful thinking on my part.  I meant Service Pack 1.  Sorry.
0 Kudos