[GIS] Batch processing near feature distance in ArcGIS Desktop

arcpyproximity

I have 2000+ point shape files. These are all GPS tracks. I want to calculate the distance from each point to the nearest feature (road, train line, restaurant etc) within a 30 meter radius. I am using Near feature from proximity analysis in ArcGIS. I am facing two problems.

  1. Using batch processing one can add as many input rows in Near feature tool box as s/he wants. But this requires selecting each file
    one by one which is time consuming especially when there are thousands
    of files. So I tried using a python scripting as follows. But after
    processing the result is written in the same file. I want to write the
    table separately. How can I do that?
  2. In case I want to add different columns for each features say road, train network for each point record in a file using near feature
    how can I do that?

Currently, I am using the following python code but all the output are generated on the current file. I want to write it separately (I mean I want to generate a new table/file after near_analysis)

import arcpy # Import arcpy module

# path where all my point shp files are kept
arcpy.env.workspace = r'C:\ArcGIS sample data\' 

#feature to which distance will be calculated
nearFeature_shp = 'C:\\GIS route network\\road.shp' 

# looping through all the files
for file in arcpy.ListFeatureClasses (): 

    # Process: Near
    arcpy.Near_analysis(file, nearFeature_shp , "30 Meters", "LOCATION", "ANGLE") 

Best Answer

Python is all about combining many operations into one. In the script below, I iterate through feature classes in a workspace. For each, I iterate through a list of other feature classes to perform a near analysis on. I perform the near analysis, and with a little help of a dictionary as well as field calculate, I transfer the results into new fields. Finally, after performing the multiple near analyses I copy the feature class with feature class to feature class.

Try something like this:

import arcpy # Import arcpy module
import os

#features to which distance will be calculated
RoadnearFeature_shp = r'C:\GIS route network\road.shp'

TrainnearFeature_shp = r'C:\GIS route network\train.shp'

restnearFeature_shp = r'C:\GIS route network\restaurant.shp'

outLocation = r"C:\GISStuff"

#Dictionary for field name assignment
di = {}
di [RoadnearFeature_shp] = "ROAD"
di [TrainnearFeature_shp] = "TRAIN"
di [restnearFeature_shp] = "REST"

addedfields = []

# path where all my point shp files are kept
arcpy.env.workspace = r'C:\ArcGIS sample data'

# looping through all the files
for file in arcpy.ListFeatureClasses ():
    for NearFC in [RoadnearFeature_shp, TrainnearFeature_shp, restnearFeature_shp]:
        #Perform analysis
        arcpy.Near_analysis(file, NearFC, "30 Meters", "LOCATION", "ANGLE")

        #Add fields to store near analysis results
        arcpy.AddField_management (file, di[NearFC] + "_ID", "LONG")
        arcpy.AddField_management (file, di[NearFC] + "_DIST", "DOUBLE")

        #Calculate fields
        arcpy.CalculateField_management (file, di[NearFC] + "_ID", "!NEAR_FID!", "PYTHON_9.3")
        arcpy.CalculateField_management (file, di[NearFC] + "_DIST", "!NEAR_DIST!", "PYTHON_9.3")

        #Delete fields from analysis
        arcpy.DeleteField_management (NearFC, "NEAR_FID")
        arcpy.DeleteField_management (NearFC, "NEAR_DIST")

        #Track fields that have been added
        addedfields.append (di[NearFC] + "_ID")
        addedfields.append (di[NearFC] + "_DIST")

    #Analysis done. Copy feature class
    filepath = os.path.join (r'C:\ArcGIS sample data', file)
    NewName = file[:-4] + "_Near.shp"
    arcpy.FeatureClassToFeatureClass_conversion (filepath, outLocation, NewName)

    #Optional
    #Delete analysis fields from original feature class
    for field in addedfields:
        arcpy.DeleteField_management (filepath, field)
Related Question