Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have more than 150 village points, each with a village name attribute village. The same village name may appear on multiple points.

I want to assign individual IDs for each village name, say vill_A = 1, vill_B = 2, and so on. I tried to calculate [village], but it didn't work.

Any suggestions?

share|improve this question
2  
Are you wanting to assign a number to a village based on alphabetical order? Or do you just want a unique number for each village regardless of village name order? –  Baltok Jan 14 '13 at 15:26
    
Thanks Baltok, I don't care about alphabetical order, just unique numbers will do ..., though alphabetical order would be more helpful to keep track of things you know ... –  ToNoY Jan 14 '13 at 15:41

4 Answers 4

Probably the quickest way is,

  1. create a new field for the values. i.e. [vill_id]
  2. cacluate the oid (FID) into the vill_id.

enter image description here

share|improve this answer
    
the same village name appears many times, therefore the village has many FIDs; I just found the following code, but it doesn't work: Convert String to Short Integer: MyInt = CInt( ["field name"] ) or CLng, CDbl "field name"= MyInt –  ToNoY Jan 14 '13 at 15:57
3  
Create a summary table of all the possible village names, then run Brad's script on that table. Then you could do a join with the original data and copy that uniqueID over. –  kenbuja Jan 14 '13 at 16:44
    
@ToNoY that would have been useful information to include in your question. –  blah238 Jan 14 '13 at 16:53
1  
Simple solution is to combine the answers from @kenbuja (summary statistics on village fieldname) and BradNesom (add/calculate new field equal to OID/FID) then join field by village fieldname to the original attribute table. –  ccn Jan 14 '13 at 21:37

I agree with @Brad Nesom that calculating a field based on FID or OBJECTID is the simplest way, although if for some reason you need a python approach in the field calculator, try this calculation on a new integer field:

rec=0   
def autoIncrement():   
 global rec 
 pStart = 1  
 pInterval = 1   
 if (rec == 0):     
  rec = pStart     
 else:  
  rec += pInterval   
 return rec

Add to the box below the code block:

autoIncrement()

Press OK

share|improve this answer

The best way to handle the unique values where there can be duplicates is to use a Python dictionary to link the Village name to the id number.

Here is an example Python field calculation code block:

villages = {}
id = 1
def GetID(village):
    global villages
    global id
    if not village in villages:
        villages[village] = id
        id += 1
    return villages[village]

Then simply call GetID(!VillageNameField!) for the expression.

This will create an empty Python dictionary called villages. Then, for each record in the Village Name Field it will remember if it has already assigned an id number to that village based on whether it already exists in the dictionary. If the village does not already exist in the dictionary it will assign it the next sequential id number. Finally, it returns the id number associated with the village from the dictionary whether it was already there or had to be added.

share|improve this answer

If you are looking for any programming solution, you may think about creating a dictionary. IDictionary<string, List<int>> dictionary = new Dictionary<string, List<int>>();

Here,

  • Key = Village_name
  • Value = List of oid

Now you can assign values for all these oids or whatever you want.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.