import arcpy
import math
import pythonaddins

class Line(object):
"""Implementation for Projects_addin.Project1_2 (Tool)"""
def __init__(self):
    self.enabled = True
    self.cursor = 3
    self.shape = "Line" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onLine(self, line_geometry):
    cur = arcpy.da.InsertCursor("NewL", ["SHAPE@"])

    fields = arcpy.ListFields('NewL')
    length = False
    heightM = False
    heightF = False
    for field in fields:
        if field.name == 'Length':
            length = True
        elif field.name == 'HeightM':
            heightM = True
        elif field.name == 'HeightF':
            heightF = True
    if length == False:
        arcpy.AddField_management('NewL', 'Length', 'DOUBLE')
    if heightM == False:
        arcpy.AddField_management('NewL', 'HeightM', 'DOUBLE')
    if heightF == False:
        arcpy.AddField_management('NewL', 'HeightF', 'DOUBLE')

    arcpy.SelectLayerByAttribute_management('NewL', 'CLEAR_SELECTION')

    fid = int(arcpy.GetCount_management('NewL').getOutput(0))-1
    arcpy.SelectLayerByAttribute_management('NewL', 'NEW_SELECTION', ''' FID = {} '''.format(fid))

    arcpy.CalculateField_management('NewL', 'Length', '!shape.length@meters!', 'PYTHON_9.3')
    arcpy.SelectLayerByLocation_management('Source_Package', 'INTERSECT', 'NewL')

    with arcpy.da.SearchCursor('Source_Package', ['SunElevati']) as cursor:
        for feat in cursor:
            new_heightM = math.tan(math.radians(feat[0]))
    arcpy.SelectLayerByAttribute_management('Source_Package', 'CLEAR_SELECTION')
    arcpy.CalculateField_management('NewL', 'HeightM', '!Length!*{}'.format(new_heightM), 'PYTHON_9.3')
    arcpy.CalculateField_management('NewL', 'HeightF', '!HeightM!*3.28089895', 'PYTHON_9.3')

This code generates a line with attribution depending on the polygon it is overlapping. Is it possible to take the first vertex of the line and create a point in another feature class, while simultaneously transferring the attribution?

I was thinking (line_geometry.firstPoint.X, line_geometry.firstPoint.Y) would create the coordinates, but I am still considering other possibilities.

Edit 1:

point = arcpy.Point(line_geometry.firstPoint.X, line_geometry.firstPoint.Y)
pt_cur = arcpy.da.InsertCursor(Point Feature Here, ['SHAPE@'])

I added this to the end and I have created the point I desired, now I am working on transferring the attributes.

Edit 2:

I have also successfully added the attribution aspect.


You're almost there. In this line:

pt_cur = arcpy.da.InsertCursor(Point Feature Here, ['SHAPE@'])

Change the list containing only 'SHAPE@' to a list of the fields you want to update (including 'SHAPE@').

You'll want to grab the values in a SearchCursor. Probably something like in this answer to Using arcpy.da.InsertCursor to insert entire row that is fetched from search cursor?.

  • Thanks. I did something similar to that to add the attribution. – Qwerty Oct 6 '16 at 3:46
  • Your approach is actually significantly better than my idea, thanks again. – Qwerty Oct 6 '16 at 3:51

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.