TomGeo

Unexpected results of Near and Append

Discussion created by TomGeo on Jul 25, 2013
Latest reply on Jul 25, 2013 by rzufelt
Hi,

I have a point layer (2500 features) and a polygon layer (2400 features) where I want to get the Euclidian distance from every point to all the polygons.
My attempt was to iterate over the point layer and within the iteration I iterate trough the polygon layer and make a Near_analysis between the two features selected.
For the first result of the near I create a feature class, using CopyFeature_management. Otherwise I use Append_management to append to the already existing feature class.

What I expected is a feature class, containing 2400 points where the point id is always the same and the feature id various. However, all I get is a feature class holding only one feature.

Maybe my attempt is wrong, maybe I forgot something in the script...
Would be nice if you had a short look at the scrip to tip me of.

Cheers Thomas
import arcpy, gc
arcpy.env.workspace = 'path/to/database.gdb/'
arcpy.env.overwriteOutput = True

origin = arcpy.MakefeatureLayer_management('data/start', 'tmpOrigin')
areas = arcpy.MakefeatureLayer_management('data/Areas', 'tmpAreas')

okay = []

for row in arcpy.da.SearchCursor(origin, ['Respondent']):
    expr = 'Respondent = ' + str(int(row[0]))
    arcpy.SelectLayerByAttribute_management(origin, 'NEW_SELECTION', expr)

    output = 'Euc_Output_' + str(int(row[0]))
    area_cnt = 0
    for area_row in arcpy.da.SearchCursor(areas, ['Identifier']):
        area_expr = 'Identifier = ' + str(int(area_row[0]))
        arcpy.SelectLayerByAttribute_management(areas, 'NEW_SELECTION', area_expr)
        arcpy.Near_analysis(origin, areas, '', 'LOCATION')

        if area_cnt == 0:
            arcpy.CopyFeatures_management(origin, output)
        else:
            arcpy.Append_management(origin, output)
        area_cnt += 1
    del area_row

    okay.append(output)

arcpy.Merge_management(okay, 'finaloutput')
for item in okay:
    arcpy.Delete_managemnt(item)

arcpy.Delete_management(origin)
arcpy.Delete_management(areas)
del row
gc.collect()

Outcomes