Select to view content in your preferred language

Problem adding ID to Generate Near Table (or next nearest)

998
2
04-08-2011 03:09 AM
RichardGreaney
Deactivated User
Hello, I don't know if anyone can help me, but I'm running out of ideas as to how I can solve a problem:

In modelbuilder (version 10)
Using the 'Generate Near Table' tool
         I can successfully find the nearest three 'sites' to a set of features,
however, the resulting table does not have a field that specifies this is the nearest (1) this is the second nearest (2) this is the third nearest (3).

I require this because I want to seperate out the nearest and second/third nearest into seperate tables.

So my question:

Does anyone know if there is a way to either:

   - add an id to the generate near table (1 for nearest 2 for next nearest feature etc.)
   - or is there a tool where I can calculate the second (or third) nearest feature, but not the first

I have searched far and wide for a solution, but have had no luck so far,
so any suggestions would be greatly appreciated......

Of course this would all be easy if the 'generate near tool' could add an ID for second nearest etc.
Which I really expected it to do.

Thanks, Richard
0 Kudos
2 Replies
RichardGreaney
Deactivated User
I have figured a quick fix using a calculate field (python) with the following code:

Reclass (!OBJECTID!)

def Reclass(OBJECTID):
  if OBJECTID % 3 == 1:
    return 1
  elif (OBJECTID + 1) % 3 == 1:
    return 3
  else:
    return 2

this is using a MOD on the ObjectID
I'd still like a slightly more elegant solution if anyone can suggest one?

Thanks

Richard
0 Kudos
DarrenWiens2
MVP Alum
This started off as being elegant...

# Import arcpy module
import arcpy

# Parameters
input = "C:\\some feature class"           #input feature class
near = "C:\\some other feature class"    #near feature class
nearcount = "3"                                 #number of nearest features
output = "C:\\output table"                  #output table

# Fun stuff
# Process: Generate Near Table
arcpy.GenerateNearTable_analysis(input, near, output, "", "NO_LOCATION", "NO_ANGLE", "ALL", nearcount)

# Process: Add Field
arcpy.AddField_management(output, "rank", "SHORT", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")

rows = arcpy.UpdateCursor(output,"","","","IN_FID A; NEAR_DIST A")

count = 0

for row in rows:
    if count == 0:
        curID = row.IN_FID
    count = count + 1
    if row.IN_FID != curID:
        curID = row.IN_FID
        count = 1
    row.RANK = count
    rows.updateRow(row)
del row
del rows
0 Kudos