AnsweredAssumed Answered

Calculating fields equal to the distance of points in one layer to another (using searchcursor, near tables)

Question asked by kristenm37 on Nov 30, 2016
Latest reply on Dec 1, 2016 by kristenm37

I have a set of several points, "pts", and a large dataset, "observations", of thousands of points. I would like to calculate the distance from each point in the small dataset to all points in the large dataset (within 100km). 

 

My current strategy is to loop over the smaller dataset, select all relevant points in the larger datset (within 100km) and create a near table. I then try joining the near table for each of the smaller points to the large dataset. This would ideally mean that "observations" would have extra columns entitled, "Dist_pt1", "Dist_pt2", etc. equal to the distance between that observation and the given point (or blank if the observation and point are far away).

 

I'm running into two errors. First, the near tables appear to have zero or just one observation (corresponding to the closest point). Also, the JoinField command (which I've been commenting out for now) causes the code (and ArcGIS) to crash. 

 

I'd appreciate any help!

fc = "pts"
with arcpy.da.SearchCursor(fc,['Shape@','Name']) as cursor:
    for row in cursor:
        name=str(row[1])
        arcpy.AddField_management("observations", name, "FLOAT")
        query= "\"Name\" = \'"+name+"\'"
        arcpy.SelectLayerByAttribute_management (fc, "NEW_SELECTION", query)
        arcpy.GenerateNearTable_analysis(fc, "observations", "near_"+name, \
                                         "100 Kilometers")
        arcpy.AddField_management("near_"+name, "Name_"+name, "TEXT")
        arcpy.CalculateField_management("near_"+name, "Name_"+name, repr(row[1]), "PYTHON")
        arcpy.AddField_management("near_"+name, "Dist_"+name, "TEXT")
        arcpy.CalculateField_management("near_"+name, "Dist_"+name, "!Near_Dist!", "PYTHON")       
        arcpy.JoinField_management(fc, "Name", "near_"+name, "Name_"+name, "Dist_"+name)

Outcomes