location

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package location provides value objects related to location information.

Package location provides value objects related to location information.

This package contains value objects that represent geographical locations and coordinates. These value objects are immutable and follow the Value Object pattern from Domain-Driven Design.

Key value objects in this package:

  • Coordinate: Represents a geographic coordinate (latitude and longitude)

The Coordinate value object provides methods for:

  • Creating and validating coordinates
  • Parsing coordinates from strings
  • String representation and formatting in different formats (decimal, DMS, DM)
  • Equality comparison
  • Distance calculation between coordinates
  • Directional comparison (north, south, east, west)
  • JSON marshaling and conversion to maps

Example usage:

// Create a new coordinate
coord, err := location.NewCoordinate(37.7749, -122.4194)
if err != nil {
    // Handle validation error
}

// Parse a coordinate from a string
coord, err := location.ParseCoordinate("37.7749, -122.4194")
if err != nil {
    // Handle parsing error
}

// Calculate distance between coordinates
distance := coord.DistanceTo(otherCoord)

// Format coordinate in different ways
decimalFormat := coord.Format("decimal")  // "37.7749, -122.4194"
dmsFormat := coord.Format("dms")          // "37° 46' 29.64" N, 122° 25' 9.84" W"

All value objects in this package are designed to be immutable, so they cannot be changed after creation. To modify a value object, create a new instance with the desired values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Coordinate

type Coordinate struct {
	base.BaseStructValueObject
	// contains filtered or unexported fields
}

Coordinate represents a geographic coordinate (latitude and longitude)

func NewCoordinate

func NewCoordinate(latitude float64, longitude float64) (Coordinate, error)

NewCoordinate creates a new Coordinate with validation

func ParseCoordinate

func ParseCoordinate(s string) (Coordinate, error)

ParseCoordinate creates a new Coordinate from a string in format "lat,lng"

func (Coordinate) DistanceTo

func (v Coordinate) DistanceTo(other Coordinate) float64

DistanceTo calculates the distance to another coordinate in kilometers using the Haversine formula

func (Coordinate) Equals

func (v Coordinate) Equals(other Coordinate) bool

Equals checks if two Coordinates are equal

func (Coordinate) Format

func (v Coordinate) Format(format string) string

Format returns the coordinate in the specified format Format options: - "dms": Degrees, minutes, seconds (e.g., "41°24'12.2"N, 2°10'26.5"E") - "dm": Degrees and decimal minutes (e.g., "41°24.2033'N, 2°10.4417'E") - "dd": Decimal degrees (e.g., "41.40339, 2.17403")

func (Coordinate) IsEastOf

func (v Coordinate) IsEastOf(other Coordinate) bool

IsEastOf checks if this coordinate is east of another coordinate

func (Coordinate) IsEmpty

func (v Coordinate) IsEmpty() bool

IsEmpty checks if the Coordinate is empty (zero value)

func (Coordinate) IsNorthOf

func (v Coordinate) IsNorthOf(other Coordinate) bool

IsNorthOf checks if this coordinate is north of another coordinate

func (Coordinate) IsSouthOf

func (v Coordinate) IsSouthOf(other Coordinate) bool

IsSouthOf checks if this coordinate is south of another coordinate

func (Coordinate) IsWestOf

func (v Coordinate) IsWestOf(other Coordinate) bool

IsWestOf checks if this coordinate is west of another coordinate

func (Coordinate) Latitude

func (v Coordinate) Latitude() float64

Latitude returns the latitude value

func (Coordinate) Longitude

func (v Coordinate) Longitude() float64

Longitude returns the longitude value

func (Coordinate) MarshalJSON

func (v Coordinate) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (Coordinate) String

func (v Coordinate) String() string

String returns the string representation of the Coordinate

func (Coordinate) ToMap

func (v Coordinate) ToMap() map[string]interface{}

ToMap converts the Coordinate to a map[string]interface{}

func (Coordinate) Validate

func (v Coordinate) Validate() error

Validate checks if the Coordinate is valid

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL