5

I have written a script with the aim of producing a rectangular buffer from two sets of coordinates that denote the ends of a line. The aim is to create an array of points from the end points of the line and create a polygon from them.

My tool is reporting as working but is not creating an ouput (I have checked the output folder):

import arcpy
import math
import sys

def getBearing(x0,y0,x1,y1):

    deltaX = x1 - x0
    deltaY = y1 - y0
    return ((math.atan2(deltaY, deltaX)/math.pi*180)+360) % 360

def createRectpoints(x0,y0,x1,y1,displacement,bearing):

    #Bottom left corner.    
    xr_1 = x0 - (displacement*math.cos(math.radians(90-bearing))) 
    yr_1 = y0 + (displacement*math.cos(math.radians(bearing))) 
    #Bottom right corner.
    xr_2 = x0 + (displacement*math.cos(math.radians(90-bearing))) 
    yr_2 = y0 - (displacement*math.cos(math.radians(bearing)))    
    #Top left corner.    
    xr_3 = x1 - (displacement*math.cos(math.radians(90-bearing))) 
    yr_3 = y1 + (displacement*math.cos(math.radians(bearing))) 
    #Top right corner.
    xr_4 = x1 + (displacement*math.cos(math.radians(90-bearing))) 
    yr_4 = y1 - (displacement*math.cos(math.radians(bearing)))    

    bl_corner = (xr_1, yr_1)
    br_corner = (xr_2, yr_2)
    tl_corner = (xr_3, yr_3)
    tr_corner = (xr_4, yr_4)

    rectangle_points = [bl_corner, br_corner, tl_corner, tr_corner]

    return rectangle_points   

if __name__ == '__main__':

    # Take in Arguments
    # Arguments are optional
    outFC_location = arcpy.GetParameterAsText(0)
    end1_Easting = float(arcpy.GetParameterAsText(1))
    end1_Northing = float(arcpy.GetParameterAsText(2))    
    end2_Easting = float(arcpy.GetParameterAsText(3))
    end2_Northing = float(arcpy.GetParameterAsText(4))
    fcName = (arcpy.GetParameterAsText(5))
    fcPath = outFC_location.rpartition("\\")[0] 

    outFC = arcpy.CreateFeatureclass_management(fcPath, fcName)

    x0 = 500138
    y0 = 348941
    x1 = 502291
    y1 = 349303
    thickness = 180
    displacement = thickness/2
    bearing = getBearing(x0,y0,x1,y1)
    ext_r_points = createRectpoints(x0,y0,x1,y1, displacement, bearing)

    polygon_array = arcpy.Array([arcpy.Point(ext_r_points[0][0],     ext_r_points[0][1]),arcpy.Point(ext_r_points[1][0], ext_r_points[1][1]), arcpy.Point(ext_r_points[2][0], ext_r_points[2][1]),arcpy.Point(ext_r_points[3][0], ext_r_points[3][1])])

    mypolygon = arcpy.Polygon(polygon_array)

    cursor = arcpy.da.InsertCursor(outFC, ['SHAPE@'])
    cursor.insertRow([mypolygon])

What is the error in my code or my approach?

  • It would help to see what code you're using to produce the polygon_array. – crmackey Dec 18 '15 at 13:37
  • As said, you need to supply more details on the error message (traceback details on where it occurs and code itself with preceding part). As an alternative to see if you are correctly producing your array is to use CopyFeatures_management. Start debugging your code and once you have your array try this: arcpy.CopyFeatures_management (polygon_array, "in_memory\\my_buffer) . – fatih_dur Dec 18 '15 at 14:21
  • I have included the rest of my code, hopefully this will shed some light. Many thanks. – branches Dec 21 '15 at 15:29
  • I've edited again noting a change in the problem. – branches Dec 21 '15 at 16:05
2

The error is in the creation of the empty feature class. The fcName path is being partitioned when it shouldn't be, this should be put into the function directly, corrected code:

outFC_location = arcpy.GetParameterAsText(0)
end1_Easting = float(arcpy.GetParameterAsText(1))
end1_Northing = float(arcpy.GetParameterAsText(2))    
end2_Easting = float(arcpy.GetParameterAsText(3))
end2_Northing = float(arcpy.GetParameterAsText(4))
fcName = arcpy.GetParameterAsText(5) //No need for extra ()

outFC = arcpy.CreateFeatureclass_management(outFC_location, fcName, "POLYGON")
| improve this answer | |

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.