Skip to content

Instantly share code, notes, and snippets.

@nygeog
Created October 9, 2015 14:12
Show Gist options
  • Save nygeog/274f2f9b3dafdadb280c to your computer and use it in GitHub Desktop.
Save nygeog/274f2f9b3dafdadb280c to your computer and use it in GitHub Desktop.
ArcPy function near only includes FID or OBJECT ID information after its run, use this script to carry over the attributes of the near feature.
def nearJoin(inFeature,nearFeature,nearDistance,outFileLocation,outFeature):
### Normal Near Function doesn't carry over all the attributes of inFeature or nearFeature.
### To Do:
### Error handling in try: except: should handle OBJECTID vs. FID issues.
### Example:
### inF = wd+"input/fulcrum.gdb/street_segment_questions_v2_prj"
### nrF = wd+"processing/gar.gdb/gar_roads_ftl"
### nrD = "20 Meters"
### ouL = wd+"processing/fulcrum.gdb"
### ouF = "TEST"
###
### NearJoin(inF,nrF,nrD,ouL,ouF)
outFeature_temp = outFeature.replace('.shp','_temp.shp')
print 'copy inFeature to temporary dataset, why, b/c the Near_analysis function appends dist,direction info to main dataset w/o creating an output dataset, problematic, if you want to run near for many feature classes'
arcpy.FeatureClassToFeatureClass_conversion(inFeature,outFileLocation,outFeature_temp)
print 'find nearest feature to inFeature'
arcpy.Near_analysis(outFileLocation+'/'+outFeature_temp,nearFeature,nearDistance,"LOCATION","ANGLE")
print 'make feature layer for both inFeature and nearFeature'
arcpy.MakeFeatureLayer_management(outFileLocation+'/'+outFeature_temp, "inFeature_Layer")
arcpy.MakeFeatureLayer_management(nearFeature,"nearFeature_Layer")
print 'join nearFeature attributes to inFeature'
try:
arcpy.AddJoin_management("inFeature_Layer","NEAR_FID","nearFeature_Layer","OBJECTID","KEEP_ALL")
except:
arcpy.AddJoin_management("inFeature_Layer","NEAR_FID","nearFeature_Layer","FID","KEEP_ALL")
print 'feature class to feature class'
arcpy.FeatureClassToFeatureClass_conversion(outFileLocation+'/'+outFeature_temp,outFileLocation,outFeature)
print 'delete intermediate temp input data'
arcpy.Delete_management(outFileLocation+'/'+outFeature_temp)
@khibma
Copy link

khibma commented Oct 9, 2015

for your "OID/FID" problem, try:

d = arcpy.Describe("nearFeature_Layer")
objectID = d.OIDFieldName
arcpy.AddJoin_management("inFeature_Layer","NEAR_FID","nearFeature_Layer",objectID,"KEEP_ALL")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment