CoordinateSharp

repository·master·Indexed 18 days ago

https://github.com/tronald/coordinatesharp

A .NET library for geographic coordinate conversions, parsing, formatting, and location-based celestial calculations such as sunrise and sunset. It supports conversions between Geodetic Latitude/Longitude and UTM, MGRS, Cartesian, Web Mercator, and GEOREF, as well as distance calculations, geo-fencing, and magnetic data via the CoordinateSharp.Magnetic extension package.

Tokens
1.6K
Snippets
4
Records
8
Agent score
14%

What's inside CoordinateSharp

  1. CoordinateSharp Abilities

    master

    CoordinateSharp provides a wide range of geographic and celestial capabilities:

    • Lat/Long formatting: Quickly format coordinate output.
    • Coordinate conversions: Convert between Geodetic Latitude/Longitude and UTM, MGRS, Cartesian (Spherical and ECEF), Web Mercator (EPS:3857), or GEOREF.
    • Coordinate parsing: Initialize coordinates using TryParse() with multiple format types.
    • Distance calculation: Calculate distances between two coordinates.
    • Coordinate moving/shifting: Shift coordinates using a distance and bearing, or a distance and a target coordinate.
    • Location based celestial information: Determine sunset, moonrise, next solar eclipse, or zodiac signs.
    • Property change notification: Properties automatically adjust when the Coordinate changes (e.g., changing GeoDate recalculates celestial times).
    • Geo-Fencing: Define perimeters and check if a coordinate is within or near polylines.
    • Magnetic Data: Determine geographic magnetic data (requires CoordinateSharp.Magnetic extension package).
  2. Install the WMM2025 coefficients file

    master

    To update your software to use the World Magnetic Model 2025 (WMM2025), you must replace the existing WMM.COF coefficients file with the new version. The installation method depends on your software type:

    • WMM2025 GUI: Locate the installed directory, remove the existing WMM.COF, and replace it with the new WMM.COF file.
    • WMM_Linux and WMM_Windows C software: For versions up to and including WMM2020, replace the WMM.COF file located in the bin directory with the new file.
    • Custom Software: Find the coefficient file (usually WMM.COF) in your installation and replace it. If your software requires a specific filename, rename the new file accordingly. If your coefficients are embedded directly into your software binary, you must re-embed the new coefficients.
  3. Install CoordinateSharp

    master

    CoordinateSharp can be installed via NuGet or by downloading it directly from the official website.

    Prerequisites

    • .NET 4.0+ Framework
    • .NET 5.0+
    • .NET Standard 1.3+ compatible runtimes
    dotnet add package CoordinateSharp
  4. Verify WMM2025 installation

    master

    After replacing the coefficients file, verify the installation using the following two methods:

    1. Header Check

    Open the WMM.COF file in a text editor and ensure the header matches exactly: 2025.0 WMM-2025 11/13/2024

    2. Test Value Validation

    Run your software against the following test values to ensure the model is calculating correctly. Note that HAE represents height above the WGS-84 ellipsoid.

    DateHAELatLongDeclInclHXYZFDdotIdotHdotXdotYdotZdotFdot
    2020.066141430.1213.0834916.934916.870.28114.935847.5-0.1-0.129.629.6-41.4-46.418.3
    2020.0180211.05-26.4629316.129311.2536.0-14589.032745.60.10.10.0-0.951.254.5-24.3
    2020.56-36-13720.16-52.2125511.423948.68791.9-32897.641630.30.00.0-21.6-21.6-3.865.4-64.9
    2020.56326810.4340.8434738.734737.7259.230023.445914.90.00.15.95.92.4128.288.3
  5. Optimize CoordinateSharp performance

    master

    CoordinateSharp uses an eager-loaded architecture. While convenient, this can impact performance. To improve benchmarks, you can turn off the eager loading of certain properties if they are not needed for your specific use case.

    Example: If you only require MGRS conversions, turning off celestial calculations can improve Coordinate initialization by 6-10ms.

  6. Access coordinate formats and celestial data

    master

    Once a Coordinate is initialized, you can access various formatting options, conversion data (like UTM), and celestial information (like sunset/moonrise) through its properties.

    Common Properties

    • ToString(): Returns a formatted string representation of the coordinate.
    • Latitude.Seconds: Accesses the seconds component of the latitude.
    • UTM: Returns the Universal Transverse Mercator representation.
    • CelestialInfo: Provides access to location-based celestial data such as SunSet, MoonAltitude, etc.
    // Assuming 'c' is an initialized Coordinate
    Console.WriteLine(c);                              // N 47º 36' 22.32" W 122º 19' 55.56"
    Console.WriteLine(c.Latitude.Seconds);             // 22.32
    Console.WriteLine(c.UTM);                          // 10T 550200mE 5272748mN
    
    Console.WriteLine(c.CelestialInfo.SunSet);         // 5-Jun-2018 4:02:00 AM
    Console.WriteLine(c.CelestialInfo.MoonAltitude);   // 14.4169966277874
  7. Initialize a Coordinate

    master

    You can create a Coordinate instance using signed decimal degrees or by parsing a coordinate string. Note that most constructors and parsing methods require a DateTime to provide accurate celestial calculations.

    Methods

    • new Coordinate(double latitude, double longitude, DateTime geoDate): Initializes with signed decimal degrees.
    • Coordinate.Parse(string coordinateString, DateTime geoDate): Parses a coordinate from a formatted string.
    // Initialize with signed decimal degrees
    Coordinate c = new Coordinate(47.6062, -122.3321, new DateTime(2018,6,5,10,10,0));
    
    // OR simply parse from a string
    Coordinate c = Coordinate.Parse("N 47º 36.372' W 122º 19.926'", new DateTime(2018,6,5,10,10,0));
  8. Use CoordinateSharp.Magnetic extension

    master

    To calculate location-based magnetic field elements, you must install the CoordinateSharp.Magnetic extension package via NuGet.

    dotnet add package CoordinateSharp.Magnetic