AnsweredAssumed Answered

Utilizing Functions with Arcpy

Question asked by jack.r.jenkins on Nov 12, 2018
Latest reply on Nov 27, 2018 by MClaggett-esristaff

I need to re-structure my script so the line & feature class creation happen in functions

rather than the being spelled out in the main part of my script. I then, need to separate

the function into a separate file and make the adjustments necessary in the main script to

allow it to still utilize the functions.

My script;



# Use a .txt file to generate a line shapefile
import arcpy
from collections import defaultdict
import pandas as pd 

outFolder
= "C:\\Data"
arcpy.env.workspace = outFolder

#Read textfile and store in dictionary
df = pd.read_csv(r"C:\\Data\\WalkingPaths.txt", header=None,names=["WalkingPath", "X", "Y"]) 
d
= defaultdict(list)
for index, row in df.iterrows():   
    d
[row["WellPath"]].append(arcpy.Point(row["X"],row["Y"]))

#Create feature class
fc = "WalkingPaths.shp"spatRef = arcpy.SpatialReference(26913)
arcpy.CreateFeatureclass_management(out_path=outFolder, out_name=fc, geometry_type = "POLYLINE", spatial_reference=spatRef)
arcpy.AddField_management (in_table = fc, field_name = "WalkingPath", field_type = "TEXT")

#Insert polylines and attribute
insertCursor = arcpy.da.InsertCursor (fc, ["SHAPE@","WalkingPath"])
for ranch, points in d.iteritems(): #d.items() in python 3   
    insertCursor
.insertRow([arcpy.Polyline(arcpy.Array(points)),ranch])

del insertCursor

Outcomes