use crate::{
Address, AddressDelta, AddressDeltas, AddressError, AddressErrorKind, AddressStatus, Addresses,
CommonAddress, Decode, IntoBin, State, StreetNamePostType, StreetNamePreDirectional,
StreetNamePreModifier, StreetNamePreType, StreetSeparator, SubaddressType, from_bin, to_bin,
};
use derive_more::{Deref, DerefMut};
use indicatif::ParallelProgressIterator;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::Path;
pub trait Geographic {
fn latitude(&self) -> f64;
fn longitude(&self) -> f64;
}
pub trait Cartesian {
fn x(&self) -> f64;
fn y(&self) -> f64;
fn distance<T: Cartesian + ?Sized>(&self, other: &T) -> f64 {
((self.y() - other.y()).powi(2) + (self.x() - other.x()).powi(2)).sqrt()
}
fn delta<T: Address + Clone + Cartesian + Sync + Send>(
&self,
others: &[T],
min: f64,
) -> AddressDeltas
where
Self: Address + Cartesian + Sized + Clone + Send + Sync,
{
let records = others
.par_iter()
.filter(|v| v.label() == self.label())
.map(|v| AddressDelta::new(v, v.distance(self)))
.filter(|d| d.delta > min)
.collect::<Vec<AddressDelta>>();
AddressDeltas::new(records)
}
fn deltas<
T: Cartesian + Address + Clone + Sync + Send,
U: Cartesian + Address + Clone + Sync + Send,
>(
values: &[T],
other: &[U],
min: f64,
) -> AddressDeltas {
let style = indicatif::ProgressStyle::with_template(
"[{elapsed_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {'Calculating deltas...'}",
)
.unwrap();
let records_raw = values
.par_iter()
.progress_with_style(style)
.map(|v| Cartesian::delta(v, other, min))
.collect::<Vec<AddressDeltas>>();
let mut records = Vec::new();
records_raw
.iter()
.map(|v| records.append(&mut v.clone()))
.for_each(drop);
AddressDeltas::new(records)
}
}
#[derive(Debug, Clone, Default, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize)]
pub struct GeoAddress {
pub address: CommonAddress,
pub latitude: f64,
pub longitude: f64,
}
impl Address for GeoAddress {
fn number(&self) -> i64 {
self.address.number
}
fn number_mut(&mut self) -> &mut i64 {
&mut self.address.number
}
fn number_suffix(&self) -> &Option<String> {
&self.address.number_suffix
}
fn number_suffix_mut(&mut self) -> &mut Option<String> {
&mut self.address.number_suffix
}
fn directional(&self) -> &Option<StreetNamePreDirectional> {
&self.address.directional
}
fn directional_mut(&mut self) -> &mut Option<StreetNamePreDirectional> {
&mut self.address.directional
}
fn street_name_pre_modifier(&self) -> &Option<StreetNamePreModifier> {
&self.address.pre_modifier
}
fn street_name_pre_modifier_mut(&mut self) -> &mut Option<StreetNamePreModifier> {
&mut self.address.pre_modifier
}
fn street_name_pre_type(&self) -> &Option<StreetNamePreType> {
&self.address.pre_type
}
fn street_name_pre_type_mut(&mut self) -> &mut Option<StreetNamePreType> {
&mut self.address.pre_type
}
fn street_name_separator(&self) -> &Option<StreetSeparator> {
&self.address.separator
}
fn street_name_separator_mut(&mut self) -> &mut Option<StreetSeparator> {
&mut self.address.separator
}
fn street_name(&self) -> &String {
&self.address.street_name
}
fn street_name_mut(&mut self) -> &mut String {
&mut self.address.street_name
}
fn street_type(&self) -> &Option<StreetNamePostType> {
&self.address.street_type
}
fn street_type_mut(&mut self) -> &mut Option<StreetNamePostType> {
&mut self.address.street_type
}
fn subaddress_id(&self) -> &Option<String> {
&self.address.subaddress_id
}
fn subaddress_id_mut(&mut self) -> &mut Option<String> {
&mut self.address.subaddress_id
}
fn subaddress_type(&self) -> &Option<SubaddressType> {
&self.address.subaddress_type
}
fn subaddress_type_mut(&mut self) -> &mut Option<SubaddressType> {
&mut self.address.subaddress_type
}
fn floor(&self) -> &Option<i64> {
&self.address.floor
}
fn floor_mut(&mut self) -> &mut Option<i64> {
&mut self.address.floor
}
fn building(&self) -> &Option<String> {
&self.address.building
}
fn building_mut(&mut self) -> &mut Option<String> {
&mut self.address.building
}
fn zip(&self) -> i64 {
self.address.zip
}
fn zip_mut(&mut self) -> &mut i64 {
&mut self.address.zip
}
fn postal_community(&self) -> &String {
&self.address.postal_community
}
fn postal_community_mut(&mut self) -> &mut String {
&mut self.address.postal_community
}
fn state(&self) -> &State {
&self.address.state
}
fn state_mut(&mut self) -> &mut State {
&mut self.address.state
}
fn status(&self) -> &AddressStatus {
&self.address.status
}
fn status_mut(&mut self) -> &mut AddressStatus {
&mut self.address.status
}
}
impl Geographic for GeoAddress {
fn latitude(&self) -> f64 {
self.latitude
}
fn longitude(&self) -> f64 {
self.longitude
}
}
impl<T: Address + Geographic + Clone> From<&T> for GeoAddress {
fn from(data: &T) -> Self {
let address = CommonAddress::from(data);
let latitude = data.latitude();
let longitude = data.longitude();
Self {
address,
latitude,
longitude,
}
}
}
#[derive(
Debug,
Clone,
Default,
serde::Serialize,
serde::Deserialize,
PartialEq,
PartialOrd,
derive_new::new,
derive_more::Deref,
derive_more::DerefMut,
)]
pub struct GeoAddresses(Vec<GeoAddress>);
impl Addresses<GeoAddress> for GeoAddresses {}
impl IntoBin<GeoAddresses> for GeoAddresses {
fn load<P: AsRef<Path>>(path: P) -> Result<Self, AddressError> {
let config = bincode::config::standard();
match from_bin(path) {
Ok(records) => {
let (result, _) = bincode::serde::decode_from_slice::<
Self,
bincode::config::Configuration,
>(&records, config)
.map_err(|source| Decode::new(source, line!(), file!().into()))?;
Ok(result)
}
Err(source) => Err(AddressErrorKind::from(source).into()),
}
}
fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), AddressError> {
to_bin(self, path)
}
}
impl<T: Address + Geographic + Clone + Sized> From<&[T]> for GeoAddresses {
fn from(addresses: &[T]) -> Self {
let records = addresses
.iter()
.map(GeoAddress::from)
.collect::<Vec<GeoAddress>>();
Self(records)
}
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd)]
pub struct AddressPoint {
pub address: CommonAddress,
pub x: f64,
pub y: f64,
}
impl Address for AddressPoint {
fn number(&self) -> i64 {
self.address.number
}
fn number_mut(&mut self) -> &mut i64 {
&mut self.address.number
}
fn number_suffix(&self) -> &Option<String> {
&self.address.number_suffix
}
fn number_suffix_mut(&mut self) -> &mut Option<String> {
&mut self.address.number_suffix
}
fn directional(&self) -> &Option<StreetNamePreDirectional> {
&self.address.directional
}
fn directional_mut(&mut self) -> &mut Option<StreetNamePreDirectional> {
&mut self.address.directional
}
fn street_name_pre_modifier(&self) -> &Option<StreetNamePreModifier> {
&self.address.pre_modifier
}
fn street_name_pre_modifier_mut(&mut self) -> &mut Option<StreetNamePreModifier> {
&mut self.address.pre_modifier
}
fn street_name_pre_type(&self) -> &Option<StreetNamePreType> {
&self.address.pre_type
}
fn street_name_pre_type_mut(&mut self) -> &mut Option<StreetNamePreType> {
&mut self.address.pre_type
}
fn street_name_separator(&self) -> &Option<StreetSeparator> {
&self.address.separator
}
fn street_name_separator_mut(&mut self) -> &mut Option<StreetSeparator> {
&mut self.address.separator
}
fn street_name(&self) -> &String {
&self.address.street_name
}
fn street_name_mut(&mut self) -> &mut String {
&mut self.address.street_name
}
fn street_type(&self) -> &Option<StreetNamePostType> {
&self.address.street_type
}
fn street_type_mut(&mut self) -> &mut Option<StreetNamePostType> {
&mut self.address.street_type
}
fn subaddress_id(&self) -> &Option<String> {
&self.address.subaddress_id
}
fn subaddress_id_mut(&mut self) -> &mut Option<String> {
&mut self.address.subaddress_id
}
fn subaddress_type(&self) -> &Option<SubaddressType> {
&self.address.subaddress_type
}
fn subaddress_type_mut(&mut self) -> &mut Option<SubaddressType> {
&mut self.address.subaddress_type
}
fn floor(&self) -> &Option<i64> {
&self.address.floor
}
fn floor_mut(&mut self) -> &mut Option<i64> {
&mut self.address.floor
}
fn building(&self) -> &Option<String> {
&self.address.building
}
fn building_mut(&mut self) -> &mut Option<String> {
&mut self.address.building
}
fn zip(&self) -> i64 {
self.address.zip
}
fn zip_mut(&mut self) -> &mut i64 {
&mut self.address.zip
}
fn postal_community(&self) -> &String {
&self.address.postal_community
}
fn postal_community_mut(&mut self) -> &mut String {
&mut self.address.postal_community
}
fn state(&self) -> &State {
&self.address.state
}
fn state_mut(&mut self) -> &mut State {
&mut self.address.state
}
fn status(&self) -> &AddressStatus {
&self.address.status
}
fn status_mut(&mut self) -> &mut AddressStatus {
&mut self.address.status
}
}
impl Cartesian for AddressPoint {
fn x(&self) -> f64 {
self.x
}
fn y(&self) -> f64 {
self.y
}
}
impl<T: Address + Cartesian + Clone> From<&T> for AddressPoint {
fn from(data: &T) -> Self {
let address = CommonAddress::from(data);
let x = data.x();
let y = data.y();
Self { address, x, y }
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, PartialOrd, Deref, DerefMut)]
pub struct AddressPoints(Vec<AddressPoint>);
impl Addresses<AddressPoint> for AddressPoints {}
impl IntoBin<AddressPoints> for AddressPoints {
fn load<P: AsRef<Path>>(path: P) -> Result<Self, AddressError> {
let config = bincode::config::standard();
match from_bin(path) {
Ok(records) => {
let (result, _) = bincode::serde::decode_from_slice::<
Self,
bincode::config::Configuration,
>(&records, config)
.map_err(|source| Decode::new(source, line!(), file!().into()))?;
Ok(result)
}
Err(source) => Err(AddressErrorKind::from(source).into()),
}
}
fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), AddressError> {
to_bin(self, path)
}
}
impl<T: Address + Cartesian + Clone + Sized> From<&[T]> for AddressPoints {
fn from(addresses: &[T]) -> Self {
let records = addresses
.iter()
.map(AddressPoint::from)
.collect::<Vec<AddressPoint>>();
Self(records)
}
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd)]
pub struct SpatialAddress {
pub address: CommonAddress,
pub latitude: f64,
pub longitude: f64,
pub x: f64,
pub y: f64,
}
impl Address for SpatialAddress {
fn number(&self) -> i64 {
self.address.number
}
fn number_mut(&mut self) -> &mut i64 {
&mut self.address.number
}
fn number_suffix(&self) -> &Option<String> {
&self.address.number_suffix
}
fn number_suffix_mut(&mut self) -> &mut Option<String> {
&mut self.address.number_suffix
}
fn directional(&self) -> &Option<StreetNamePreDirectional> {
&self.address.directional
}
fn directional_mut(&mut self) -> &mut Option<StreetNamePreDirectional> {
&mut self.address.directional
}
fn street_name_pre_modifier(&self) -> &Option<StreetNamePreModifier> {
&self.address.pre_modifier
}
fn street_name_pre_modifier_mut(&mut self) -> &mut Option<StreetNamePreModifier> {
&mut self.address.pre_modifier
}
fn street_name_pre_type(&self) -> &Option<StreetNamePreType> {
&self.address.pre_type
}
fn street_name_pre_type_mut(&mut self) -> &mut Option<StreetNamePreType> {
&mut self.address.pre_type
}
fn street_name_separator(&self) -> &Option<StreetSeparator> {
&self.address.separator
}
fn street_name_separator_mut(&mut self) -> &mut Option<StreetSeparator> {
&mut self.address.separator
}
fn street_name(&self) -> &String {
&self.address.street_name
}
fn street_name_mut(&mut self) -> &mut String {
&mut self.address.street_name
}
fn street_type(&self) -> &Option<StreetNamePostType> {
&self.address.street_type
}
fn street_type_mut(&mut self) -> &mut Option<StreetNamePostType> {
&mut self.address.street_type
}
fn subaddress_id(&self) -> &Option<String> {
&self.address.subaddress_id
}
fn subaddress_id_mut(&mut self) -> &mut Option<String> {
&mut self.address.subaddress_id
}
fn subaddress_type(&self) -> &Option<SubaddressType> {
&self.address.subaddress_type
}
fn subaddress_type_mut(&mut self) -> &mut Option<SubaddressType> {
&mut self.address.subaddress_type
}
fn floor(&self) -> &Option<i64> {
&self.address.floor
}
fn floor_mut(&mut self) -> &mut Option<i64> {
&mut self.address.floor
}
fn building(&self) -> &Option<String> {
&self.address.building
}
fn building_mut(&mut self) -> &mut Option<String> {
&mut self.address.building
}
fn zip(&self) -> i64 {
self.address.zip
}
fn zip_mut(&mut self) -> &mut i64 {
&mut self.address.zip
}
fn postal_community(&self) -> &String {
&self.address.postal_community
}
fn postal_community_mut(&mut self) -> &mut String {
&mut self.address.postal_community
}
fn state(&self) -> &State {
&self.address.state
}
fn state_mut(&mut self) -> &mut State {
&mut self.address.state
}
fn status(&self) -> &AddressStatus {
&self.address.status
}
fn status_mut(&mut self) -> &mut AddressStatus {
&mut self.address.status
}
}
impl Geographic for SpatialAddress {
fn latitude(&self) -> f64 {
self.latitude
}
fn longitude(&self) -> f64 {
self.longitude
}
}
impl Cartesian for SpatialAddress {
fn x(&self) -> f64 {
self.x
}
fn y(&self) -> f64 {
self.y
}
}
impl<T: Address + Geographic + Cartesian + Clone> From<&T> for SpatialAddress {
fn from(data: &T) -> Self {
let address = CommonAddress::from(data);
let latitude = data.latitude();
let longitude = data.longitude();
let x = data.x();
let y = data.y();
Self {
address,
latitude,
longitude,
x,
y,
}
}
}
#[derive(
Debug,
Clone,
Default,
serde::Serialize,
serde::Deserialize,
PartialEq,
PartialOrd,
derive_new::new,
derive_more::Deref,
derive_more::DerefMut,
)]
pub struct SpatialAddresses(Vec<SpatialAddress>);
impl Addresses<SpatialAddress> for SpatialAddresses {}
impl IntoBin<SpatialAddresses> for SpatialAddresses {
fn load<P: AsRef<Path>>(path: P) -> Result<Self, AddressError> {
let config = bincode::config::standard();
match from_bin(path) {
Ok(records) => {
let (result, _) = bincode::serde::decode_from_slice::<
Self,
bincode::config::Configuration,
>(&records, config)
.map_err(|source| Decode::new(source, line!(), file!().into()))?;
Ok(result)
}
Err(source) => Err(AddressErrorKind::from(source).into()),
}
}
fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), AddressError> {
to_bin(self, path)
}
}
impl<T: Address + Geographic + Cartesian + Clone + Sized> From<&[T]> for SpatialAddresses {
fn from(addresses: &[T]) -> Self {
let records = addresses
.iter()
.map(SpatialAddress::from)
.collect::<Vec<SpatialAddress>>();
Self(records)
}
}