ArcPy根据角度字段创建方位线

文章目录

代码

# -*- coding:gbk-*-
import arcpy
import  sys
import os
import math
import time

source_point_path = sys.argv[1]  # 举证照片点图层的全路径(图层须包含方位角字段AZIM)
temp_workspace = sys.argv[2]  # 临时工作空间(生成方位线的处理将在此工作空间中进行)

if __name__ == '__main__':
    try:
        # source_point_path = r'E:/Temp/SD_Result.gdb/NJZZP'  # 举证照片点图层的全路径(图层须包含方位角字段AZIM)
        # temp_workspace = r'E:/Temp/SD_Temp.gdb'  # 临时工作空间(生成方位线的处理将在此工作空间中进行)
        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']  # 图形XY坐标、方位角字段
        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)
                    # print '{0}: X0 = {1}, Y0 = {2}, Angle = {3}, R = {4},' \
                    #       ' Dx = {5}, Dy = {6}, X1 = {7}, Y1 = {8}'.format(
                    #     row_index, x0, y0, a, r, math.sin(r), math.cos(r), x1, y1)
                    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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值