Skip to content

Commit

Permalink
Try #48:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Aug 11, 2021
2 parents 4c9e960 + 6dff9c6 commit 65b3b04
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 53 deletions.
14 changes: 10 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
name = "geocoding"
description = "Geocoding library for Rust"
version = "0.3.1"
authors = ["Stephan Hügel <urschrei@gmail.com>", "Blake Grotewold <hello@grotewold.me>"]
authors = [
"Stephan Hügel <urschrei@gmail.com>",
"Blake Grotewold <hello@grotewold.me>",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/georust/geocoding"
keywords = ["gecoding", "geo", "gis", "geospatial"]
Expand All @@ -11,12 +14,15 @@ edition = "2018"

[dependencies]
thiserror = "1.0"
geo-types = "0.6"
geo-types = "0.7"
num-traits = "0.2"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
reqwest = { version = "0.10", default-features = false, features = ["blocking", "json"] }
hyper = "0.13.6"
reqwest = { version = "0.11", default-features = false, features = [
"blocking",
"json",
] }
hyper = "0.14"
chrono = { version = "0.4", features = ["serde"] }

[features]
Expand Down
28 changes: 15 additions & 13 deletions src/geoadmin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! let res = geoadmin.forward(&address);
//! assert_eq!(res.unwrap(), vec![Point::new(7.451352119445801, 46.92793655395508)]);
//! ```
use std::fmt::Debug;

use crate::Deserialize;
use crate::GeocodingError;
use crate::InputBounds;
Expand All @@ -34,7 +36,7 @@ pub struct GeoAdmin {
/// An instance of a parameter builder for GeoAdmin geocoding
pub struct GeoAdminParams<'a, T>
where
T: Float,
T: Float + Debug,
{
searchtext: &'a str,
origins: &'a str,
Expand All @@ -44,7 +46,7 @@ where

impl<'a, T> GeoAdminParams<'a, T>
where
T: Float,
T: Float + Debug,
{
/// Create a new GeoAdmin parameter builder
/// # Example:
Expand Down Expand Up @@ -159,7 +161,7 @@ impl GeoAdmin {
params: &GeoAdminParams<T>,
) -> Result<GeoAdminForwardResponse<T>, GeocodingError>
where
T: Float,
T: Float + Debug,
for<'de> T: Deserialize<'de>,
{
// For lifetime issues
Expand Down Expand Up @@ -219,7 +221,7 @@ impl Default for GeoAdmin {

impl<T> Forward<T> for GeoAdmin
where
T: Float,
T: Float + Debug,
for<'de> T: Deserialize<'de>,
{
/// A forward-geocoding lookup of an address. Please see [the documentation](https://api3.geo.admin.ch/services/sdiservices.html#search) for details.
Expand Down Expand Up @@ -258,7 +260,7 @@ where

impl<T> Reverse<T> for GeoAdmin
where
T: Float,
T: Float + Debug,
for<'de> T: Deserialize<'de>,
{
/// A reverse lookup of a point. More detail on the format of the
Expand Down Expand Up @@ -309,7 +311,7 @@ where
// See [the documentation](https://www.swisstopo.admin.ch/content/swisstopo-internet/en/online/calculation-services/_jcr_content/contentPar/tabs/items/documents_publicatio/tabPar/downloadlist/downloadItems/19_1467104393233.download/ch1903wgs84_e.pdf) for more details
fn wgs84_to_lv03<T>(p: &Point<T>) -> Point<T>
where
T: Float,
T: Float + Debug,
{
let lambda = (p.x().to_f64().unwrap() * 3600.0 - 26782.5) / 10000.0;
let phi = (p.y().to_f64().unwrap() * 3600.0 - 169028.66) / 10000.0;
Expand Down Expand Up @@ -356,7 +358,7 @@ where
#[derive(Debug, Deserialize)]
pub struct GeoAdminForwardResponse<T>
where
T: Float,
T: Float + Debug,
{
pub features: Vec<GeoAdminForwardLocation<T>>,
}
Expand All @@ -365,7 +367,7 @@ where
#[derive(Debug, Deserialize)]
pub struct GeoAdminForwardLocation<T>
where
T: Float,
T: Float + Debug,
{
id: Option<usize>,
pub properties: ForwardLocationProperties<T>,
Expand Down Expand Up @@ -454,7 +456,7 @@ mod test {
fn new_with_sr_forward_test() {
let geoadmin = GeoAdmin::new().with_sr("2056");
let address = "Seftigenstrasse 264, 3084 Wabern";
let res = geoadmin.forward(&address);
let res = geoadmin.forward(address);
assert_eq!(res.unwrap(), vec![Point::new(2_600_968.75, 1_197_427.0)]);
}

Expand All @@ -463,7 +465,7 @@ mod test {
let geoadmin =
GeoAdmin::new().with_endpoint("https://api3.geo.admin.ch/rest/services/api/");
let address = "Seftigenstrasse 264, 3084 Wabern";
let res = geoadmin.forward(&address);
let res = geoadmin.forward(address);
assert_eq!(
res.unwrap(),
vec![Point::new(7.451352119445801, 46.92793655395508)]
Expand All @@ -474,7 +476,7 @@ mod test {
fn with_sr_forward_full_test() {
let geoadmin = GeoAdmin::new().with_sr("2056");
let bbox = InputBounds::new((2_600_967.75, 1_197_426.0), (2_600_969.75, 1_197_428.0));
let params = GeoAdminParams::new(&"Seftigenstrasse Bern")
let params = GeoAdminParams::new("Seftigenstrasse Bern")
.with_origins("address")
.with_bbox(&bbox)
.build();
Expand All @@ -490,7 +492,7 @@ mod test {
fn forward_full_test() {
let geoadmin = GeoAdmin::new();
let bbox = InputBounds::new((7.4513398, 46.92792859), (7.4513662, 46.9279467));
let params = GeoAdminParams::new(&"Seftigenstrasse Bern")
let params = GeoAdminParams::new("Seftigenstrasse Bern")
.with_origins("address")
.with_bbox(&bbox)
.build();
Expand All @@ -506,7 +508,7 @@ mod test {
fn forward_test() {
let geoadmin = GeoAdmin::new();
let address = "Seftigenstrasse 264, 3084 Wabern";
let res = geoadmin.forward(&address);
let res = geoadmin.forward(address);
assert_eq!(
res.unwrap(),
vec![Point::new(7.451352119445801, 46.92793655395508)]
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@

static UA_STRING: &str = "Rust-Geocoding";

use chrono;
pub use geo_types::{Coordinate, Point};
use num_traits::Float;
use reqwest::blocking::Client;
use reqwest::header::ToStrError;
use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::num::ParseIntError;
use thiserror::Error;

Expand Down Expand Up @@ -85,7 +85,7 @@ pub enum GeocodingError {
/// ```
pub trait Reverse<T>
where
T: Float,
T: Float + Debug,
{
// NOTE TO IMPLEMENTERS: Point coordinates are lon, lat (x, y)
// You may have to provide these coordinates in reverse order,
Expand Down Expand Up @@ -113,7 +113,7 @@ where
/// ```
pub trait Forward<T>
where
T: Float,
T: Float + Debug,
{
// NOTE TO IMPLEMENTERS: while returned provider point data may not be in
// lon, lat (x, y) order, Geocoding requires this order in its output Point
Expand All @@ -128,15 +128,15 @@ where
#[derive(Copy, Clone, Debug)]
pub struct InputBounds<T>
where
T: Float,
T: Float + Debug,
{
pub minimum_lonlat: Point<T>,
pub maximum_lonlat: Point<T>,
}

impl<T> InputBounds<T>
where
T: Float,
T: Float + Debug,
{
/// Create a new `InputBounds` struct by passing 2 `Point`s defining:
/// - minimum (bottom-left) longitude and latitude coordinates
Expand All @@ -155,7 +155,7 @@ where
/// Convert borrowed input bounds into the correct String representation
impl<T> From<InputBounds<T>> for String
where
T: Float,
T: Float + Debug,
{
fn from(ip: InputBounds<T>) -> String {
// Return in lon, lat order
Expand Down
39 changes: 20 additions & 19 deletions src/opencage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
//! // "Carrer de Calatrava, 68, 08017 Barcelone, Espagne"
//! println!("{:?}", res.unwrap());
//! ```
use crate::chrono::naive::serde::ts_seconds::deserialize as from_ts;
use crate::chrono::NaiveDateTime;
use crate::DeserializeOwned;
use crate::GeocodingError;
use crate::InputBounds;
Expand All @@ -34,9 +32,12 @@ use crate::UA_STRING;
use crate::{Client, HeaderMap, HeaderValue, USER_AGENT};
use crate::{Deserialize, Serialize};
use crate::{Forward, Reverse};
use chrono::naive::serde::ts_seconds::deserialize as from_ts;
use chrono::NaiveDateTime;
use num_traits::Float;
use serde::Deserializer;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::{Arc, Mutex};

macro_rules! add_optional_param {
Expand Down Expand Up @@ -146,7 +147,7 @@ impl<'a> Opencage<'a> {
///```
pub fn reverse_full<T>(&self, point: &Point<T>) -> Result<OpencageResponse<T>, GeocodingError>
where
T: Float + DeserializeOwned,
T: Float + DeserializeOwned + Debug,
{
let q = format!(
"{}, {}",
Expand All @@ -156,9 +157,9 @@ impl<'a> Opencage<'a> {
);
let mut query = vec![
("q", q.as_str()),
(&"key", &self.api_key),
(&"no_annotations", "0"),
(&"no_record", "1"),
("key", &self.api_key),
("no_annotations", "0"),
("no_record", "1"),
];
query.extend(self.parameters.as_query());

Expand Down Expand Up @@ -248,7 +249,7 @@ impl<'a> Opencage<'a> {
bounds: U,
) -> Result<OpencageResponse<T>, GeocodingError>
where
T: Float + DeserializeOwned,
T: Float + DeserializeOwned + Debug,
U: Into<Option<InputBounds<T>>>,
{
let ann = String::from("0");
Expand Down Expand Up @@ -291,7 +292,7 @@ impl<'a> Opencage<'a> {

impl<'a, T> Reverse<T> for Opencage<'a>
where
T: Float + DeserializeOwned,
T: Float + DeserializeOwned + Debug,
{
/// A reverse lookup of a point. More detail on the format of the
/// returned `String` can be found [here](https://blog.opencagedata.com/post/99059889253/good-looking-addresses-solving-the-berlin-berlin)
Expand Down Expand Up @@ -336,7 +337,7 @@ where

impl<'a, T> Forward<T> for Opencage<'a>
where
T: Float + DeserializeOwned,
T: Float + DeserializeOwned + Debug,
{
/// A forward-geocoding lookup of an address. Please see [the documentation](https://opencagedata.com/api#ambiguous-results) for details
/// of best practices in order to obtain good-quality results.
Expand Down Expand Up @@ -511,7 +512,7 @@ where
#[derive(Debug, Serialize, Deserialize)]
pub struct OpencageResponse<T>
where
T: Float,
T: Float + Debug,
{
pub documentation: String,
pub licenses: Vec<HashMap<String, String>>,
Expand All @@ -528,7 +529,7 @@ where
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Results<T>
where
T: Float,
T: Float + Debug,
{
pub annotations: Option<Annotations<T>>,
pub bounds: Option<Bounds<T>>,
Expand All @@ -542,7 +543,7 @@ where
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Annotations<T>
where
T: Float,
T: Float + Debug,
{
pub dms: Option<HashMap<String, String>>,
pub mgrs: Option<String>,
Expand Down Expand Up @@ -615,7 +616,7 @@ pub struct Timestamp {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bounds<T>
where
T: Float,
T: Float + Debug,
{
pub northeast: HashMap<String, T>,
pub southwest: HashMap<String, T>,
Expand Down Expand Up @@ -652,7 +653,7 @@ mod test {
fn forward_test() {
let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
let address = "Schwabing, München";
let res = oc.forward(&address);
let res = oc.forward(address);
assert_eq!(
res.unwrap(),
vec![Point(Coordinate {
Expand All @@ -678,7 +679,7 @@ mod test {
minimum_lonlat: Point::new(-0.13806939125061035, 51.51989264641164),
maximum_lonlat: Point::new(-0.13427138328552246, 51.52319711775629),
};
let res = oc.forward_full(&address, bbox).unwrap();
let res = oc.forward_full(address, bbox).unwrap();
let first_result = &res.results[0];
assert_eq!(
first_result.formatted,
Expand All @@ -693,7 +694,7 @@ mod test {
Point::new(-0.13806939125061035, 51.51989264641164),
Point::new(-0.13427138328552246, 51.52319711775629),
);
let res = oc.forward_full(&address, bbox).unwrap();
let res = oc.forward_full(address, bbox).unwrap();
let first_result = &res.results[0];
assert_eq!(
first_result.formatted,
Expand All @@ -708,7 +709,7 @@ mod test {
Point::from((-0.13806939125061035, 51.51989264641164)),
Point::from((-0.13427138328552246, 51.52319711775629)),
);
let res = oc.forward_full(&address, bbox).unwrap();
let res = oc.forward_full(address, bbox).unwrap();
let first_result = &res.results[0];
assert_eq!(
first_result.formatted,
Expand All @@ -723,7 +724,7 @@ mod test {
(-0.13806939125061035, 51.51989264641164),
(-0.13427138328552246, 51.52319711775629),
);
let res = oc.forward_full(&address, bbox).unwrap();
let res = oc.forward_full(address, bbox).unwrap();
let first_result = &res.results[0];
assert_eq!(
first_result.formatted,
Expand All @@ -734,7 +735,7 @@ mod test {
fn forward_full_test_nobox() {
let oc = Opencage::new("dcdbf0d783374909b3debee728c7cc10".to_string());
let address = "Moabit, Berlin, Germany";
let res = oc.forward_full(&address, NOBOX).unwrap();
let res = oc.forward_full(address, NOBOX).unwrap();
let first_result = &res.results[0];
assert_eq!(first_result.formatted, "Moabit, Berlin, Germany");
}
Expand Down
Loading

0 comments on commit 65b3b04

Please sign in to comment.