<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to spatially sort line features??? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-spatially-sort-line-features/m-p/761985#M58768</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]30356[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import sys
import os
import arcpy
import math

def CalculateAngle(x, y):
&amp;nbsp;&amp;nbsp;&amp;nbsp; Len = math.hypot (x, y)
&amp;nbsp;&amp;nbsp;&amp;nbsp; acos = x/Len
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = math.acos(acos)
&amp;nbsp;&amp;nbsp;&amp;nbsp; degree = math.degrees (angle)

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (degree)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newdegree = 360.00 - degree
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (newdegree)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;gt;= 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (0.00)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (180.00)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r&amp;nbsp;&amp;nbsp;&amp;nbsp; 

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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lenth = row[0].length
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lenth &amp;lt; 1e-03:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue


&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstartpt = row[0].firstPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstartx = ELstartpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstarty = ELstartpt.Y

&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendpt = row[0].lastPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendx = ELendpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendy = ELendpt.Y

&amp;nbsp;&amp;nbsp;&amp;nbsp; ETempX = ELendx - ELstartx
&amp;nbsp;&amp;nbsp;&amp;nbsp; ETempY = ELendy - ELstarty

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of first line 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Sangle = CalculateAngle(STempX, STempY)
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of second line 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Eangle = CalculateAngle(ETempX, ETempY)

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of bisector
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = Eangle + (Sangle - Eangle)/2

&amp;nbsp;&amp;nbsp;&amp;nbsp; cos = math.cos(angle)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sin = math.sin(angle)

&amp;nbsp;&amp;nbsp;&amp;nbsp; TempX = d * cos
&amp;nbsp;&amp;nbsp;&amp;nbsp; TempY = d * sin

&amp;nbsp;&amp;nbsp;&amp;nbsp; StartX = TempX + ELstartx
&amp;nbsp;&amp;nbsp;&amp;nbsp; StartY = TempY + ELstarty

&amp;nbsp;&amp;nbsp;&amp;nbsp; EndX = ELstartx - TempX 
&amp;nbsp;&amp;nbsp;&amp;nbsp; EndY = ELstarty - TempY 

&amp;nbsp;&amp;nbsp;&amp;nbsp; array = arcpy.Array([arcpy.Point(StartX, StartY), arcpy.Point(EndX, EndY)])
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyline = arcpy.Polyline(array)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.insertRow([polyline])

&amp;nbsp;&amp;nbsp;&amp;nbsp; STempX = (-1) * ETempX
&amp;nbsp;&amp;nbsp;&amp;nbsp; STempY = (-1) * ETempY

del cursor
del row
del rows
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 08 Jan 2014 18:52:42 GMT</pubDate>
    <dc:creator>YichiLiu1</dc:creator>
    <dc:date>2014-01-08T18:52:42Z</dc:date>
    <item>
      <title>How to spatially sort line features???</title>
      <link>https://community.esri.com/t5/python-questions/how-to-spatially-sort-line-features/m-p/761985#M58768</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]30356[/ATTACH]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import sys
import os
import arcpy
import math

def CalculateAngle(x, y):
&amp;nbsp;&amp;nbsp;&amp;nbsp; Len = math.hypot (x, y)
&amp;nbsp;&amp;nbsp;&amp;nbsp; acos = x/Len
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = math.acos(acos)
&amp;nbsp;&amp;nbsp;&amp;nbsp; degree = math.degrees (angle)

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (degree)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newdegree = 360.00 - degree
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (newdegree)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r

&amp;nbsp;&amp;nbsp;&amp;nbsp; if y == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;gt;= 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (0.00)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; r = math.radians (180.00)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return r&amp;nbsp;&amp;nbsp;&amp;nbsp; 

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:
&amp;nbsp;&amp;nbsp;&amp;nbsp; lenth = row[0].length
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lenth &amp;lt; 1e-03:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; continue


&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstartpt = row[0].firstPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstartx = ELstartpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELstarty = ELstartpt.Y

&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendpt = row[0].lastPoint
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendx = ELendpt.X
&amp;nbsp;&amp;nbsp;&amp;nbsp; ELendy = ELendpt.Y

&amp;nbsp;&amp;nbsp;&amp;nbsp; ETempX = ELendx - ELstartx
&amp;nbsp;&amp;nbsp;&amp;nbsp; ETempY = ELendy - ELstarty

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of first line 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Sangle = CalculateAngle(STempX, STempY)
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of second line 
&amp;nbsp;&amp;nbsp;&amp;nbsp; Eangle = CalculateAngle(ETempX, ETempY)

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the angle of bisector
&amp;nbsp;&amp;nbsp;&amp;nbsp; angle = Eangle + (Sangle - Eangle)/2

&amp;nbsp;&amp;nbsp;&amp;nbsp; cos = math.cos(angle)
&amp;nbsp;&amp;nbsp;&amp;nbsp; sin = math.sin(angle)

&amp;nbsp;&amp;nbsp;&amp;nbsp; TempX = d * cos
&amp;nbsp;&amp;nbsp;&amp;nbsp; TempY = d * sin

&amp;nbsp;&amp;nbsp;&amp;nbsp; StartX = TempX + ELstartx
&amp;nbsp;&amp;nbsp;&amp;nbsp; StartY = TempY + ELstarty

&amp;nbsp;&amp;nbsp;&amp;nbsp; EndX = ELstartx - TempX 
&amp;nbsp;&amp;nbsp;&amp;nbsp; EndY = ELstarty - TempY 

&amp;nbsp;&amp;nbsp;&amp;nbsp; array = arcpy.Array([arcpy.Point(StartX, StartY), arcpy.Point(EndX, EndY)])
&amp;nbsp;&amp;nbsp;&amp;nbsp; polyline = arcpy.Polyline(array)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.insertRow([polyline])

&amp;nbsp;&amp;nbsp;&amp;nbsp; STempX = (-1) * ETempX
&amp;nbsp;&amp;nbsp;&amp;nbsp; STempY = (-1) * ETempY

del cursor
del row
del rows
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Jan 2014 18:52:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-spatially-sort-line-features/m-p/761985#M58768</guid>
      <dc:creator>YichiLiu1</dc:creator>
      <dc:date>2014-01-08T18:52:42Z</dc:date>
    </item>
  </channel>
</rss>

