import arcpy
import os
# Set geoprocessing environments
arcpy.env.overwriteOutput = True
# function to convert link info in a text file to a line feature class
def CreateLinkFeatures(in_text_file, in_data):
sr = arcpy.Describe(in_data).spatialReference
features = [] # a list to hold polyline geometry objects
f = open(in_text_file, "r")
for line in f.readlines():
# take start/end blank spaces off
# separate the start and end point coordinates
points = line.strip().split()
point1 = arcpy.Point(float(points[1]), float(points[2]))
point2 = arcpy.Point(float(points[3]), float(points[4]))
features.append(arcpy.Polyline(arcpy.Array([point1, point2]), sr))
f.close() # close the text file
# copy the geometry objects into a feature class named Links
result = arcpy.CopyFeatures_management(features, in_data + "_links")
return result.getOutput(0)
if __name__ == "__main__":
# Make a copy of the data because
# TransformFeatures tool modifies the input data
arcpy.management.CopyFeatures(r"C:\data\Tutorial.gdb\Parcels", r"C:\data\Tutorial.gdb\Parcels_copy")
links_file = r"C:\data\TF_links.txt"
tf_link_features = CreateLinkFeatures(links_file, r"C:\data\Tutorial.gdb\Parcels")
try:
arcpy.edit.TransformFeatures(r"C:\data\Tutorial.gdb\Parcels_copy", tf_link_features)
print(arcpy.GetMessages())
except arcpy.ExecuteError as aex:
print(arcpy.GetMessages(2))
except Exception as ex:
print(ex.args[0])