Select to view content in your preferred language

How to create unique id with batch wise in ArcGIS 10.4.1

1683
6
Jump to solution
02-26-2022 01:18 AM
Labels (3)
santhoshp
Occasional Contributor

Hi Friends,

kindly attached .gdb, example excel sheet, and image for your reference. I want to create a unique id 001 for each batch have 30 point feature, next 30 point feature I want to create 002....etc like that 

I want to create at a time any arcpy code available please help me.

I want to create a unique at a time like 001,002,003....etc each batch has 30 point features see the example below.

Thanks

Santhosh

 

work_unit_no.JPG

0 Kudos
1 Solution

Accepted Solutions
KimGarbade
Frequent Contributor

I just used a Notebook and tried this code and it worked.  A little different from the original code that runs in Spyder:

intCurrentBatch = 1
intStartNumber = 1
intBatchSize = 10
intBSAfterFirstTimeThrough = intBatchSize - 1
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if intStartNumber <= intBatchSize:
row[0] = str(intCurrentBatch).zfill(4)
cursor.updateRow(row)
intStartNumber = intStartNumber + 1
else:
intStartNumber = 1
intCurrentBatch = intCurrentBatch + 1
row[0] = str(intCurrentBatch).zfill(4)
cursor.updateRow(row)
intBatchSize = intBSAfterFirstTimeThrough

 

Image of Notebook.  Keep in mind that indentation level is important.

KimGarbade_0-1646070660355.png

Results

KimGarbade_1-1646070715678.png

 

View solution in original post

0 Kudos
6 Replies
KimGarbade
Frequent Contributor

There might be more elegant ways of doing it but this worked for me.  I wrote this in Spyder:

KimGarbade_0-1645881316379.png

result in my test data:

KimGarbade_1-1645881341972.png

 

0 Kudos
santhoshp
Occasional Contributor

Hi KimGarbade,

Can you share with me the above code? Exactly I am looking at the above result.

Thanks

Santhosh 

 

0 Kudos
KimGarbade
Frequent Contributor

Sorry, I'm not at the computer I wrote that code on (that was my home PC).  So I don't have a text version of the code here (at work), but the code I used was in the first image above.  There are no lines of code either above or below the lines shown (no lines below line 27).

I wrote the code in Spyder, which is a third party Python editor, but it should work in any python environment that supports arcpy.

0 Kudos
santhoshp
Occasional Contributor

Hi KimGarbade,

when I was using the above code it's showing the error message. can you see the below image for your reference

Thanks

Santhosh

error.JPG

 

0 Kudos
KimGarbade
Frequent Contributor

I just used a Notebook and tried this code and it worked.  A little different from the original code that runs in Spyder:

intCurrentBatch = 1
intStartNumber = 1
intBatchSize = 10
intBSAfterFirstTimeThrough = intBatchSize - 1
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
if intStartNumber <= intBatchSize:
row[0] = str(intCurrentBatch).zfill(4)
cursor.updateRow(row)
intStartNumber = intStartNumber + 1
else:
intStartNumber = 1
intCurrentBatch = intCurrentBatch + 1
row[0] = str(intCurrentBatch).zfill(4)
cursor.updateRow(row)
intBatchSize = intBSAfterFirstTimeThrough

 

Image of Notebook.  Keep in mind that indentation level is important.

KimGarbade_0-1646070660355.png

Results

KimGarbade_1-1646070715678.png

 

0 Kudos
santhoshp
Occasional Contributor

Hi KimGarbade,

Finally, I got the Result of my data Thank you so much for your help. 

Thanks

Santhosh

 

 

Result in my data below

Finally results.JPG

 
0 Kudos