Skip to content

TechNobre/PowerUtils.Geolocation

Repository files navigation

PowerUtils.Geolocation

Logo

Library to work with geographic coordinates

Tests Quality Gate Status Coverage

NuGet Nuget License: MIT

Support to

  • .NET 3.1 or more
  • .NET Framework 4.6.2 or more
  • .NET Standard 2.0 or more

Dependencies

  • Newtonsoft.Json NuGet (.NET Framework 4.6.2 | .NET Framework 4.8 | .NET Standard 2.0 | .NET Standard 2.1)

How to use

Install NuGet package

This package is available through Nuget Packages: https://www.nuget.org/packages/PowerUtils.Geolocation

Nuget

Install-Package PowerUtils.Geolocation

.NET CLI

dotnet add package PowerUtils.Geolocation

Exceptions

namespace PowerUtils.Geolocation.Exceptions

  • InvalidCoordinateException(coordinate);
  • MinLatitudeException(coordinate);
  • MaxLatitudeException(coordinate);
  • MinLongitudeException(coordinate);
  • MaxLongitudeException(coordinate);

Types

namespace PowerUtils.Geolocation.Types

CardinalDirection

public enum CardinalDirection
{
    North,
    South,
    East,
    West,
}

DistanceUnit

public enum DistanceUnit
{
    kilometer,
    Meter,
    Mile
}

GeographicalOrientation

public enum GeographicalOrientation
{
    Latitude,
    Longitude
}

Extensions

namespace PowerUtils.Geolocation

LengthConversionExtensions

.FromKilometerToMeter()

Convert kilometers to meters (int, uint, long, ulong, float, double, decimal)

// result = 45_000
var result = 45.FromKilometerToMeter();

.FromKilometerToMile()

Convert kilometers to miles (float, double, decimal)

// result = 137.472_122
var result = (221.24).FromKilometerToMile();

.FromKilometerTo()

Converting (double, decimal, float) numbers in kilometers to a new unit

// result = 20_000
var result = 20.FromKilometerTo(DistanceUnit.Meter);

.FromMeterToKilometer()

Convert meters to kilometers (int, uint, long, ulong, float, double, decimal)

// result = 45
var result = (45_000).FromMeterToKilometer();

.FromMeterToMile()

Convert meters to kilometers (float, double, decimal)

// result = 7.098204899547
var result = (11_423.457).FromMeterToMile();

.FromMeterTo()

Converting (double, decimal, float) numbers in meters to a new unit

// result = 0.002
var result = 2.FromMeterTo(DistanceUnit.kilometer);

.FromMileToMeter()

Convert miles to meters (float, double, decimal)

// result = 356_050.3816
var result = (221.24).FromMileToMeter();

.FromMileToKilometer()

Convert miles to kilometers (float, double, decimal)

// result = 356.05038160000004
var result = (221.24).FromMileToKilometer();

.FromMileTo()

Converting (double, decimal, float) numbers in miles to a new unit

// result = 32_18.68
var result = 2.FromMileTo(DistanceUnit.kilometer);

ConversionExtensions

.GetGeographicalOrientation()

Get the geographical orientation from a specific cardinal direction

// result = GeographicalOrientation.Longitude
var result = CardinalDirection.North.GetGeographicalOrientation();
.ToRadian()

Convert degree to radian (PI / 180)

// result = 0.19198621771937624
var result = 11.ToRadian();
.ToDegree()

Convert radian to degree (180 / PI)

// result = 11
var result = (0.19198621771937624).ToDegree();
.ToDegree()

Convert radian to degree (180 / PI)

// result = 11
var result = (0.19198621771937624).ToDegree();
.ToDDPoint()

Convert decimal degree point (string) to decimal degree point (double)

// result = -8.668_173
var result = "-8,668173".ToDDPoint();

Objects

namespace PowerUtils.Geolocation

GeoDDCoordinate

Decimal degree coordinate

var coordinate = new GeoDDCoordinate(81.54, -54.1272);
(var latitude, var longitude) = coordinates;
Deconstruct

Deconstruct GeoDDCoordinate to double latitude and double longitude

(var latitude, var longitude) = coordinates;
.Clone()

Create a new object 'GeoDDCoordinate' with the same data

(var newCoordinate = coordinates.Clone();
Comparisons
GeoDDCoordinate left = new(1.54, 54.1272);
GeoDDCoordinate right = new(1.54, 54.1272);

// result1 = true
var result1 = left == right;

// result2 = false
var result2 = left != right;

// result3 = true
var result3 = left.Equals(right);
Implicits
var text1 = "-12.51214,14.1272";

var coordinate = (GeoDDCoordinate)text1;

var text2 = (string)coordinate;
Parse
var coordinate1 = GeoDDCoordinate.Parse("12,152", "-8,12");
var coordinate2 = GeoDDCoordinate.Parse("81.54  , -54.1272");

var result3 = GeoDDCoordinate.TryParse("12,152", "-8,12", out var coordinate3);
var result4 = GeoDDCoordinate.TryParse("81.54  , -54.1272", out var coordinate4);
Distance
// distance1 = 189143
var distance1 = GeoDDCoordinate.Distance(37.165611, -8.545786, 38.737545, -9.370047, 0);

// distance2 = 18542.719416538552
var distance2 = GeoDDCoordinate.PreciseDistance(37.068673, -7.939493, 37.098708, -8.145107);


GeoDDCoordinate left = new(37.068673, -7.939493);
GeoDDCoordinate right = new(37.098708, -8.145107);

// distance3 = 18543
var distance3 = left.Distance(right);

GeoJSON

var coordinate = new GeoDDCoordinate(9.1, 12);

var result = new GeoJSON(coordinate);
Implicits
var coordinate = new GeoDDCoordinate(9.1, 12);

var geoJSON = (GeoJSON)coordinate;

var result = (GeoDDCoordinate)geoJSON;

Guard

var latitude = GuardGeolocation.Against.Latitude(degree);

var longitude = GuardGeolocation.Against.Longitude(degree);

GuardGeolocation.Against.Latitude()

  • Exception
    • MinLatitudeException
    • MaxLatitudeException

GuardGeolocation.Against.Longitude()

  • Exception
    • MinLongitudeException
    • MaxLongitudeException

Contribution

If you have any questions, comments, or suggestions, please open an issue or create a pull request