Mercurial > core / rust/lib/obj/src/object/location.rs
changeset 698: |
96958d3eb5b0 |
parent: |
3d78bed56188
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 04 Oct 2024 22:04:59 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
3 //! Location object types 6 use serde::{Deserialize, Serialize}; 8 /// A City object descriptor. Serves as an anchor for many properties 9 /// in location-based data. 10 #[derive(Serialize, Deserialize, Debug)] 14 area: Option<Vec<Point>>, 15 region: Option<String>, 16 state: Option<String>, 17 country: Option<String>, 18 timezone: Option<String>, 22 pub fn new(point: Point) -> Self { 25 name: "ellisville".to_string(), 30 timezone: Some("est".to_string()), 34 impl Objective for City {} 36 /// Geo-coordinate Point object type 37 #[derive(Serialize, Deserialize, Debug, Default, PartialEq)] 44 /// Create a new Point from (f32, f32) 45 pub fn new(lat: f32, lng: f32) -> Self { 49 /// Given an additional Point, and assuming Points are on Earth, 50 /// returns the distance in kilometers between them using the 52 pub fn earth_distance_from(&self, other: Point) -> f32 { 53 let earth_radius_kilometer = 6371.0_f32; 54 let lat_rads = self.lat.to_radians(); 55 let other_lat_rads = other.lat.to_radians(); 57 let delta_latitude = (self.lat - other.lat).to_radians(); 58 let delta_longitude = (self.lng - other.lng).to_radians(); 60 let central_angle_inner = (delta_latitude / 2.0).sin().powi(2) 62 * other_lat_rads.cos() 63 * (delta_longitude / 2.0).sin().powi(2); 64 let central_angle = 2.0 * central_angle_inner.sqrt().asin(); 66 let distance = earth_radius_kilometer * central_angle; 69 "Distance between points on the surface of Earth is {:.1} kilometers", 78 fn london_to_paris() { 80 Point::new(48.85341_f32, -2.34880_f32) 81 .earth_distance_from(Point::new(51.50853_f32, -0.12574_f32)), 86 impl From<City> for Point { 87 fn from(city: City) -> Self { 92 impl Objective for Point {}