Select to view content in your preferred language

How to spatially sort line features???

458
0
01-08-2014 10:52 AM
YichiLiu1
Regular Contributor
Hi all,

I wrote a script to calculate angle bisectors of adjacent lines. I pasted my code here. This code worked fine if the line segments are in right order. However, if the features are not in a spatially right order, for example, the first feature (FID = 0, highlighted in the image) at the middle of the whole line, the result won???t be correct. My question is, how can sort these line segments? Or is there a better way to calculate angle bisector?

Thank you very much!

[ATTACH=CONFIG]30356[/ATTACH]


import sys
import os
import arcpy
import math

def CalculateAngle(x, y):
    Len = math.hypot (x, y)
    acos = x/Len
    angle = math.acos(acos)
    degree = math.degrees (angle)

    if y > 0:
        r = math.radians (degree)
        return r

    if y < 0:
        newdegree = 360.00 - degree
        r = math.radians (newdegree)
        return r

    if y == 0:
        if x >= 0:
            r = math.radians (0.00)
            return r
        if x < 0:
            r = math.radians (180.00)
            return r    

inputlines = arcpy.GetParameterAsText(0)

fcLocation = arcpy.GetParameterAsText(1)

fcName = arcpy.GetParameterAsText(2)

dis = arcpy.GetParameterAsText(3)

d = float(dis)

arcpy.CreateFeatureclass_management(fcLocation, fcName, "POLYLINE")

arcpy.AddMessage("New Feature Class Created")

fcOutput = os.path.join (fcLocation, fcName)

# Create search cursor, read in lines 
rows = arcpy.da.SearchCursor(inputlines, ["SHAPE@"])
#Insert Cursor: write lines to new feature lass
cursor = arcpy.da.InsertCursor(fcOutput, ["SHAPE@"])

#Read the first line
row = rows.next()
SLstartpt = row[0].firstPoint
SLstartx = SLstartpt.X
SLstarty = SLstartpt.Y

SLendpt = row[0].lastPoint
SLendx = SLendpt.X
SLendy = SLendpt.Y

STempX = SLstartx - SLendx
STempY = SLstarty - SLendy

#start the row iteration
for row in rows:
    lenth = row[0].length
    
    if lenth < 1e-03:
        continue


    ELstartpt = row[0].firstPoint
    ELstartx = ELstartpt.X
    ELstarty = ELstartpt.Y

    ELendpt = row[0].lastPoint
    ELendx = ELendpt.X
    ELendy = ELendpt.Y

    ETempX = ELendx - ELstartx
    ETempY = ELendy - ELstarty

    #Calculate the angle of first line 
    Sangle = CalculateAngle(STempX, STempY)
   
    #Calculate the angle of second line 
    Eangle = CalculateAngle(ETempX, ETempY)

    #Calculate the angle of bisector
    angle = Eangle + (Sangle - Eangle)/2

    cos = math.cos(angle)
    sin = math.sin(angle)

    TempX = d * cos
    TempY = d * sin

    StartX = TempX + ELstartx
    StartY = TempY + ELstarty

    EndX = ELstartx - TempX 
    EndY = ELstarty - TempY 

    array = arcpy.Array([arcpy.Point(StartX, StartY), arcpy.Point(EndX, EndY)])
    polyline = arcpy.Polyline(array)
    cursor.insertRow([polyline])

    STempX = (-1) * ETempX
    STempY = (-1) * ETempY

del cursor
del row
del rows
Tags (2)
0 Kudos
0 Replies