代码
import arcpy
import sys
import os
import math
import time
source_point_path = sys.argv[1]
temp_workspace = sys.argv[2]
if __name__ == '__main__':
try:
line_name = r'Direction_Line'
line_length = 10
angle_field_name = r'AZIM'
time_begin = time.time()
arcpy.env.workspace = temp_workspace
arcpy.env.overwriteOutput = True
point_name = os.path.split(source_point_path)[-1].split('.')[-1]
point_path = os.path.join(temp_workspace, point_name)
line_path = os.path.join(temp_workspace, line_name)
if not arcpy.Exists(source_point_path):
raise Exception, r'Not Exists {0}'.format(source_point_path)
if arcpy.Exists(point_path):
arcpy.Delete_management(point_path)
if arcpy.Exists(line_path):
arcpy.Delete_management(line_path)
arcpy.Copy_management(source_point_path, point_path)
all_point_fields = arcpy.ListFields(point_name)
angle_field_exist = False
for a_field in all_point_fields:
if a_field.name.upper() == angle_field_name:
angle_field_exist = True
break
if not angle_field_exist:
raise Exception, r'Not Exist Field {0} in {1}'.format(angle_field_name, point_name)
des_point = arcpy.Describe(point_path)
sr = des_point.spatialReference
if not arcpy.Exists(line_path):
arcpy.CreateFeatureclass_management(temp_workspace, line_name, 'POLYLINE', None, 'DISABLED', 'DISABLED', sr)
line_cursor = arcpy.da.InsertCursor(line_path, ("SHAPE@"))
point_fields = ['SHAPE@XY', angle_field_name, 'OBJECTID']
row_index = 0
with arcpy.da.SearchCursor(point_name, point_fields) as point_cursor:
for point_row in point_cursor:
row_index = row_index + 1
if point_row[0][0] and point_row[0][1] and point_row[1]:
x0 = point_row[0][0]
y0 = point_row[0][1]
a = point_row[1]
r = math.pi * a / 180
x1 = x0 + line_length * math.sin(r)
y1 = y0 + line_length * math.cos(r)
point_col = arcpy.Array([arcpy.Point(x0, y0), arcpy.Point(x1, y1)])
polyline = arcpy.Polyline(point_col)
line_cursor.insertRow((polyline,))
else:
print r'Skip Feature at row {0} with OBJECTID = {1} whose shape or angle field is null'.format(
row_index, point_row[2])
del line_cursor
print "Total Time {0}".format(time.time() - time_begin)
print "1"
except Exception, e:
print e.message