Skip to content

Commit

Permalink
Add geo location by latitude and longitude coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkumar-mourougappane committed Aug 10, 2024
1 parent 654c61b commit 1bc9bc1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ postcode = "0.1.1"
public-ip = "0.2.2"
ipgeolocate = "0.3.6"
tokio = { version = "1.39.2", features = ["full", "fs"] }
geo_rust = "0.4.1"
32 changes: 32 additions & 0 deletions src/geo_location/geo_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::net::IpAddr;

use geo_rust::{get_nearest_postcode, get_postal_data, Country, GeoLocation};
use ipgeolocate::{Locator, Service};
use postcode::Postcode;
use serde::Serialize;
Expand All @@ -15,6 +16,8 @@ pub enum GeoLocationError {
CannotParseFromPostCode(#[from] postcode::Error),
#[error("Cannot obtain location using IP geolocate.")]
GeolocationError(#[from] ipgeolocate::GeoError),
#[error("Cannot obtain location coordinates.")]
CoordinatesParseError,
#[error("Cannot parse `{0}` to `{1}.")]
ParseError(String, String),
#[error("unknown data store error")]
Expand Down Expand Up @@ -156,3 +159,32 @@ pub async fn local_from_public_ip() -> Result<GeoLocationData, GeoLocationError>
}
}
}

pub async fn locate_from_coordinates(
latitude: f64,
longitude: f64,
) -> Result<GeoLocationData, GeoLocationError> {
let geonames_data = get_postal_data(Country::All);
let location = GeoLocation {
latitude,
longitude,
};
let location_data = match get_nearest_postcode(location, &geonames_data) {
Some(location_data) => location_data,
None => return Err(GeoLocationError::CoordinatesParseError),
};
let postal_code = location_data.postal_code.clone();

let ip_address = match public_ip::addr().await {
Some(ip) => ip_addr_to_string(ip),
None => return Err(GeoLocationError::CannotParseIP),
};

Ok(GeoLocationData::new(
latitude,
longitude,
ip_address,
postal_code,
"".to_owned(),
))
}
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ use meteo_wizard::{
web_protocols::http_fetch,
};

use std::{process::exit};
use std::process::exit;

#[tokio::main]
async fn main() {
env_logger::builder()
.filter_level(log::LevelFilter::Debug)
.init();

let geo_location_data= match geo_location::geo_data::local_from_public_ip().await {
Ok(location_data) => location_data,
Err(error) => {
log::error!("{}", error);
exit(-1)
}
};

let geo_location_data = match geo_location::geo_data::local_from_public_ip().await {
Ok(location_data) => location_data,
Err(error) => {
log::error!("{}", error);
exit(-1)
}
};

let url_config = UrlConfig::new(
geo_location_data.get_latitude(),
Expand Down

0 comments on commit 1bc9bc1

Please sign in to comment.