<?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 Re: arcpy function to calculate angle between two lines in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111253#M8626</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm now using brute force...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def angle(pointlist):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculates using cosine rule: A^2 = B^2 + C^2 - 2BC cos a
&amp;nbsp;&amp;nbsp;&amp;nbsp; # a = arccos ((B^2+C^2-A^2)/(2BC))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # but multiply by 180/pi to convert from radians
&amp;nbsp;&amp;nbsp;&amp;nbsp; A_vector = pointlist[0] - pointlist[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; B_vector = pointlist[0] - pointlist[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; C_vector = pointlist[1] - pointlist[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; A2,B2,C2 = map(square_length_of_vector,(A_vector,B_vector,C_vector))
&amp;nbsp;&amp;nbsp;&amp;nbsp; B,C = map(numpy.sqrt,(B2,C2))
&amp;nbsp;&amp;nbsp;&amp;nbsp; side_ratio = (B2+C2-A2)/(2*B*C)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if side_ratio &amp;gt; 1: # possible on sharp angle due to rounding errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0.
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif side_ratio &amp;lt; -1: # possible on blunt angle due to rounding errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 180.
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 180/numpy.pi * numpy.arccos(side_ratio)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...shame if arc doesn't have a way of doing that that generalises to spherical geometry, though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 06:40:28 GMT</pubDate>
    <dc:creator>CrispinCooper</dc:creator>
    <dc:date>2021-12-11T06:40:28Z</dc:date>
    <item>
      <title>arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111250#M8623</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Does this exist?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, C.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Feb 2011 14:20:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111250#M8623</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-02-23T14:20:39Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111251#M8624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Not that I know of but you could get the geometry object to each, find their angles and calculate it that way. That would be a kind of&amp;nbsp; brute force way of doing it though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Feb 2011 17:04:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111251#M8624</guid>
      <dc:creator>ChrisMathers</dc:creator>
      <dc:date>2011-02-23T17:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111252#M8625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, thanks for the replies.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My second best option is to get the geometry for each and calculate manually.&amp;nbsp; Some variant on atan(dy1/dx1)-atan(dy2-dx2) will work for the planar case I imagine.&amp;nbsp; I wondered if arc would automatically handle spherical projections, great circles etc though - I'm not sure how to do that! (or is it simpler than I think?)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll try near_analysis (though note it isn't present in arcview which which I may need this script to be compatible).&amp;nbsp; Also am I right in thinking it requires that the features be (i) in a table and (ii) that that table doesn't already have a NEAR_ANGLE field?&amp;nbsp; I need to calculate multiple angles per feature (e.g. between each feature and n other objects)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Feb 2011 13:08:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111252#M8625</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-02-24T13:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111253#M8626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm now using brute force...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def angle(pointlist):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # calculates using cosine rule: A^2 = B^2 + C^2 - 2BC cos a
&amp;nbsp;&amp;nbsp;&amp;nbsp; # a = arccos ((B^2+C^2-A^2)/(2BC))
&amp;nbsp;&amp;nbsp;&amp;nbsp; # but multiply by 180/pi to convert from radians
&amp;nbsp;&amp;nbsp;&amp;nbsp; A_vector = pointlist[0] - pointlist[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; B_vector = pointlist[0] - pointlist[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; C_vector = pointlist[1] - pointlist[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; A2,B2,C2 = map(square_length_of_vector,(A_vector,B_vector,C_vector))
&amp;nbsp;&amp;nbsp;&amp;nbsp; B,C = map(numpy.sqrt,(B2,C2))
&amp;nbsp;&amp;nbsp;&amp;nbsp; side_ratio = (B2+C2-A2)/(2*B*C)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if side_ratio &amp;gt; 1: # possible on sharp angle due to rounding errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0.
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif side_ratio &amp;lt; -1: # possible on blunt angle due to rounding errors
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 180.
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 180/numpy.pi * numpy.arccos(side_ratio)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...shame if arc doesn't have a way of doing that that generalises to spherical geometry, though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:40:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111253#M8626</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2021-12-11T06:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111254#M8627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You can make rapid coordinate system conversions by using an appropriate spatial reference parameter in a cursor. For example, this code spits out the vertice coordinates of a township layer in WGS84 coordinates (not their native StatePlane coordinates).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcgisscripting
gp = arcgisscripting.create(9.3)
fc = r"\\snarf\am\div_lm\ds\gis\tools\sde_connections_read\ropa_gis_layer_user.sde\CADASTRE.TOWNSHIP_SV"
spatialRef = r"C:\Program Files\ArcGIS\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
dsc = gp.describe(fc)
shapeFieldName = dsc.shapeFieldName
oidFieldName = dsc.oidfieldname
searchRows = gp.searchcursor(fc, "", spatialRef)
searchRow = searchRows.next()
while searchRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp; oidFieldValue = searchRow.getvalue(oidFieldName)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print oidFieldName + " = " + str(oidFieldValue)&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; shapeFieldObj = searchRow.getvalue(shapeFieldName)
&amp;nbsp;&amp;nbsp;&amp;nbsp; partCount = shapeFieldObj.partcount
&amp;nbsp;&amp;nbsp;&amp;nbsp; partIndex = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; while partIndex &amp;lt; partCount:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Part " + str(partIndex)+ ":"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; partObj = shapeFieldObj.GetPart(partIndex)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointObj = partObj.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointCounter = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while pointObj:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print pointObj.x, pointObj.y
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointObj = partObj.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointCounter += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # If pointObj is null, either the part is finished or there is an interior ring
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not pointObj: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pointObj = partObj.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if pointObj:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Interior Ring:"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; partIndex += 1
searchRow = searchRows.next()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 06:40:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111254#M8627</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T06:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111255#M8628</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You can make rapid coordinate system conversions by using an appropriate spatial reference parameter in a cursor. &lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ah, that's probably what I want then - thank you!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Feb 2011 09:02:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111255#M8628</guid>
      <dc:creator>CrispinCooper</dc:creator>
      <dc:date>2011-02-25T09:02:44Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy function to calculate angle between two lines</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111256#M8629</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you are going to be computing this a lot and efficiency matters, you can do slightly better by observing that&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; [INDENT]a · b = |a| |b| cos(theta)[/INDENT] &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where · is the dot product, |a| is the length of a and theta is that angle between vectors a and b. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Rearrange and you get &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]theta = arccos((a · b)/(|a| |b|))[/INDENT]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Feb 2011 02:22:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-function-to-calculate-angle-between-two/m-p/111256#M8629</guid>
      <dc:creator>DavidStrip</dc:creator>
      <dc:date>2011-02-28T02:22:10Z</dc:date>
    </item>
  </channel>
</rss>

