Skip to main content

Location

Struct Location 

Source
pub struct Location {
    pub latitude_deg: f64,
    pub longitude_deg: f64,
    pub altitude_m: f64,
}
Expand description

Represents a physical observer location on Earth.

Used for computing local sidereal time, converting celestial coordinates, and modeling telescope geometry.

Fields§

§latitude_deg: f64

Latitude in degrees (+N, -S)

§longitude_deg: f64

Longitude in degrees (+E, -W, Greenwich = 0)

§altitude_m: f64

Altitude above sea level in meters

Implementations§

Source§

impl Location

Source

pub fn parse(lat_str: &str, lon_str: &str, alt_m: f64) -> Result<Self>

Parses a location from flexible coordinate strings.

Automatically detects the coordinate format and applies appropriate parsing.

§Supported Formats
§Decimal degrees
  • "40.7128" or "-74.0060"
  • "40.7128N" or "74.0060W"
  • "N40.7128" or "W 74.0060"
  • "40.7128 N" or "74.0060 West"
  • "north 40.7128" or "west 74.0060"
§DMS (Degrees Minutes Seconds)
  • "40 42 46" or "40° 42' 46\""
  • "40d42m46s" or "40deg42min46sec"
  • "40:42:46" or "40-42-46"
  • "40°42'46.08\"N" (with decimals and direction)
  • "40d 42' 46.08\" N" (mixed separators)
  • "40 degrees 42 minutes 46 seconds"
§DM (Degrees Decimal Minutes)
  • "40° 42.767'" or "40d 42.767m"
  • "40 42.767" (assumed DM if only 2 parts)
§HMS (Hours Minutes Seconds) for longitude
  • "4h 56m 27s" or "4:56:27"
  • "4h56m27.5s" or "4 hours 56 minutes 27.5 seconds"
  • "4h 56' 27\"" (using arcminute/arcsecond symbols)
§Special handling
  • Unicode: "40°42′46″" (proper Unicode prime/double-prime)
  • Compact: "404246N" or "0740060W" (DDMMSS format)
  • Aviation: "4042.767N" (DDMM.mmm format)
  • Fuzzy: Handles extra spaces, mixed case, common typos
§Arguments
  • lat_str: Latitude string in any supported format
  • lon_str: Longitude string in any supported format
  • alt_m: Altitude in meters
§Returns

Ok(Location) if parsing succeeds

§Errors

Returns Err(AstroError::InvalidDmsFormat) with helpful error messages

§Examples
use astro_math::location::Location;
 
// Decimal degrees with compass directions
let loc = Location::parse("40.7128 N", "74.0060 W", 10.0).unwrap();
assert!((loc.latitude_deg - 40.7128).abs() < 1e-6);
assert!((loc.longitude_deg + 74.0060).abs() < 1e-6);

// DMS with symbols
let loc = Location::parse("40°42'46.08\"N", "74°0'21.6\"W", 10.0).unwrap();
assert!((loc.latitude_deg - 40.7128).abs() < 1e-4);

// HMS for longitude
let loc = Location::parse("51.5074 N", "0h 7m 39.84s W", 0.0).unwrap();
assert!((loc.longitude_deg + 1.9166).abs() < 1e-3);

// Mixed formats and fuzzy matching
let loc = Location::parse("40d 42m 46s North", "74 deg 0 min 21.6 sec west", 10.0).unwrap();
assert!((loc.latitude_deg - 40.7128).abs() < 1e-4);
Source

pub fn from_dms(lat_str: &str, lon_str: &str, alt_m: f64) -> Result<Self>

Parses a Location from sexagesimal (DMS) strings for latitude and longitude.

Supports a wide range of common DMS formats:

  • "39 00 01.7"
  • "39:00:01.7"
  • "39°00'01.7\""
§Arguments
  • lat_str: Latitude string in sexagesimal format
  • lon_str: Longitude string in sexagesimal format
  • alt_m: Altitude in meters
§Returns

Ok(Location) if parsing succeeds

§Errors

Returns Err(AstroError::InvalidDmsFormat) if:

  • String doesn’t match any supported DMS format
  • Degrees, minutes, or seconds are out of valid ranges
§Examples
§DMS with spaces
use astro_math::location::Location;
let loc = Location::from_dms("+39 00 01.7", "-92 18 03.2", 250.0).unwrap();
assert!((loc.latitude_deg - 39.0004722).abs() < 1e-6);
assert!((loc.longitude_deg + 92.3008888).abs() < 1e-6);
§DMS with colons
use astro_math::location::Location;
let loc = Location::from_dms("+39:00:01.7", "-92:18:03.2", 250.0).unwrap();
assert!((loc.latitude_deg - 39.0004722).abs() < 1e-6);
§ASCII punctuation
use astro_math::location::Location;
let loc = Location::from_dms("+39°00'01.7\"", "-92°18'03.2\"", 250.0).unwrap();
assert!((loc.longitude_deg + 92.3008888).abs() < 1e-6);
§Invalid input
use astro_math::location::Location;
use astro_math::error::AstroError;
 
match Location::from_dms("foo", "bar", 100.0) {
    Err(AstroError::InvalidDmsFormat { input, .. }) => {
        assert_eq!(input, "foo");
    }
    _ => panic!("Expected InvalidDmsFormat error"),
}
Source

pub fn latitude_dms_string(&self) -> String

Source

pub fn longitude_dms_string(&self) -> String

Source

pub fn local_sidereal_time(&self, datetime: DateTime<Utc>) -> f64

Computes the Local Sidereal Time (LST) at this location for a given UTC timestamp.

§Arguments
  • datetime: UTC datetime
§Returns

Local Sidereal Time in fractional hours

§Example
use chrono::{Utc, TimeZone};
use astro_math::location::Location;

let dt = Utc.with_ymd_and_hms(1987, 4, 10, 19, 21, 0).unwrap();
let loc = Location {
    latitude_deg: 32.0,
    longitude_deg: -64.0,
    altitude_m: 200.0,
};
let lst = loc.local_sidereal_time(dt);
assert!((lst - 4.3157).abs() < 1e-3);
Source

pub fn local_mean_sidereal_time(&self, datetime: DateTime<Utc>) -> f64

Local Mean Sidreal Time (LMST) is calculated using the “mean equinox,” a theoretical reference point in space that moves at a constant rate.

§Arguments
  • datetime: UTC datetime
§Returns

Local Sidereal Time in fractional hours

§Example
use chrono::{Utc, TimeZone};
use astro_math::location::Location;

let dt = Utc.with_ymd_and_hms(1987, 4, 10, 19, 21, 0).unwrap();
let loc = Location {
    latitude_deg: 32.0,
    longitude_deg: -64.0,
    altitude_m: 200.0,
};
let lst = loc.local_mean_sidereal_time(dt);
assert!((lst - 4.315).abs() < 1e-3);
Source

pub fn latitude_dms(&self) -> String

Returns latitude formatted as ±DD° MM′ SS.sss″ (DMS)

Source

pub fn longitude_dms(&self) -> String

Returns longitude formatted as ±DDD° MM′ SS.sss″ (DMS)

Trait Implementations§

Source§

impl Clone for Location

Source§

fn clone(&self) -> Location

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Location

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Location

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.