ArcObjects Library Reference

Get Azimuth from Two Points Snippet

Get the geodetically correct Rhumb Line azimuth between two points.

[C#]

///<summary>Get the geodetically correct Rhumb Line azimuth between two points.</summary>
/// 
///<param name="fromPoint">An IPoint interface that is the start (or from) location</param>
///<param name="toPoint">An IPoint interface that is the end (or to) location</param>
///<param name="spatialReference">An esriSRGeoCSType enum that is a predefined geographic coordinate system. Example: ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_NAD1983</param>
///  
///<returns>A System.Double that represents the true azimuth</returns>
///  
///<remarks></remarks>
public System.Double GetAzimuthFromTwoPoints(ESRI.ArcGIS.Geometry.IPoint fromPoint, ESRI.ArcGIS.Geometry.IPoint toPoint, ESRI.ArcGIS.Geometry.esriSRGeoCSType spatialReference)
{

    // Define the spatial reference of the rhumb line.
    ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 spatialReferenceFactory2 = new ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass();
    ESRI.ArcGIS.Geometry.ISpatialReference2 spatialReference2 = (ESRI.ArcGIS.Geometry.ISpatialReference2)spatialReferenceFactory2.CreateSpatialReference((System.Int16)spatialReference);

    // Initialize the MeasurementTool and define the properties of the line.
    // These properties include the line type, which is a rhumb line in this case, and the 
    // spatial reference of the line. 
    ESRI.ArcGIS.DefenseSolutions.IMeasurementTool measurementTool = new ESRI.ArcGIS.DefenseSolutions.MeasurementToolClass();
    measurementTool.SpecialGeolineType = ESRI.ArcGIS.DefenseSolutions.cjmtkSGType.cjmtkSGTRhumbLine;
    measurementTool.SpecialSpatialReference = spatialReference2;

    // Determine the distance and azimuth of the rhumb line based on the start and end point coordinates.
    measurementTool.ConstructByPoints(fromPoint, toPoint);

    // Return the Azimuth.
    return measurementTool.Angle;
}
[Visual Basic .NET]

'''<summary>Get the geodetically correct Rhumb Line azimuth between two points.</summary>
''' 
'''<param name="fromPoint">An IPoint interface that is the start (or from) location</param>
'''<param name="toPoint">An IPoint interface that is the end (or to) location</param>
'''<param name="spatialReference">An esriSRGeoCSType enum that is a predefined geographic coordinate system. Example: ESRI.ArcGIS.Geometry.esriSRGeoCSType.esriSRGeoCS_NAD1983</param>
'''  
'''<returns>A System.Double that represents the true azimuth</returns>
'''  
'''<remarks></remarks>
Public Function GetAzimuthFromTwoPoints(ByVal fromPoint As ESRI.ArcGIS.Geometry.IPoint, ByVal toPoint As ESRI.ArcGIS.Geometry.IPoint, ByVal spatialReference As ESRI.ArcGIS.Geometry.esriSRGeoCSType) As System.Double

  'Define the spatial reference of the rhumb line. 
  Dim spatialReferenceFactory2 As ESRI.ArcGIS.Geometry.ISpatialReferenceFactory2 = New ESRI.ArcGIS.Geometry.SpatialReferenceEnvironmentClass
  Dim spatialReference2 As ESRI.ArcGIS.Geometry.ISpatialReference2 = CType(spatialReferenceFactory2.CreateSpatialReference(spatialReference), ESRI.ArcGIS.Geometry.ISpatialReference2)

  'Initialize the MeasurementTool and define the properties of the line.
  'These properties include the line type, which is a rhumb line in this case, and the 
  'spatial reference of the line. 
  Dim measurementTool As ESRI.ArcGIS.DefenseSolutions.IMeasurementTool = New ESRI.ArcGIS.DefenseSolutions.MeasurementToolClass
  measurementTool.SpecialGeolineType = ESRI.ArcGIS.DefenseSolutions.cjmtkSGType.cjmtkSGTRhumbLine
  measurementTool.SpecialSpatialReference = spatialReference2

  'Determine the distance and azimuth of the rhumb line based on the 
  'start and end point coordinates. 
  measurementTool.ConstructByPoints(fromPoint, toPoint)

  'Return the Azimuth
  Return measurementTool.Angle

End Function


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.DefenseSolutions
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System