3

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") 
  • Would you mind formatting that up a bit (use the {} button to indicate that it's code). The near information is appended to the end of the features that are processed, so what's being overwritten? – Michael Stimson Aug 21 '14 at 5:08
  • When I will use different feature followed by the first one then the earlier near columns are overwritten. Say I first used road network as near feature. Next I want to use rail network. So for each point there will be two near features- distance to Road and Rail network within 30 m radius. – user26161 Aug 21 '14 at 5:11
  • Oh, that explains it. Use AddField_management to add fields road_near and rail_near and CalculateField_management to copy the values there. – Michael Stimson Aug 21 '14 at 5:25
  • Thanx Michael. How do I copy each shp file appending '_road' and write it to a separate folder through loop using python. Say if my original shape file is gpsTrack1.shp I want to process the near operation on it and write the result/processed file in a different folder as gpsTrack1_road.shp. Likewise it should iterate through the loop for all the files. I have modified little bit my for loop for copying the files but this is not working. – user26161 Aug 21 '14 at 5:27
  • There are a few ways, FeatureClassToFeatureClass_conversion is possibly the best, CopyFeatures_management works well if you have a layer with a selection or Copy_management is fairly basic. – Michael Stimson Aug 21 '14 at 5:30
3

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.