[PYTHON] [Rust] Read the latitude / longitude csv data to find the distance between two points

background

The position information of about 500 points is summarized in csv data, and I want to find the linear distance (km) for all the combinations.

Find the distance from latitude and longitude

I referred to the following article.

--Python Distance calculation between two points given by longitude and latitude

pub fn cal_rho(lat_a: &f64, lon_a: &f64, lat_b: &f64, lon_b: &f64) -> f64 {
    const RA: f64 = 6378.140; // equatorial radius (km)
    const RB: f64 = 6356.755; // polar radius (km)
    let f: f64;
    let rad_lat_a: f64;
    let rad_lon_a: f64;
    let rad_lat_b: f64;
    let rad_lon_b: f64;
    let pa: f64;
    let pb: f64;
    let xx: f64;
    let c1: f64;
    let c2: f64;
    let dr: f64;
    let rho: f64;

    f = (RA - RB) / RA;
    rad_lat_a = lat_a.to_radians();
    rad_lon_a = lon_a.to_radians();
    rad_lat_b = lat_b.to_radians();
    rad_lon_b = lon_b.to_radians();
    pa = (RB / RA * rad_lat_a.tan()).atan();
    pb = (RB / RA * rad_lat_b.tan()).atan();
    xx = (pa.sin() * pb.sin() + pa.cos() * pb.cos() * (rad_lon_a - rad_lon_b).cos()).acos();
    c1 = ((xx.sin() - xx) * (pa.sin() + pb.sin())).powf(2.0) / ((xx / 2.0).cos()).powf(2.0);
    c2 = ((xx.sin() + xx) * (pa.sin() - pb.sin())).powf(2.0) / ((xx / 2.0).sin()).powf(2.0);
    dr = f / 8.0 * (c1 - c2);
    rho = RA * (xx + dr);
    rho
}

Load csv-> Calculate distance

I tried to create a nested loop using StringRecordsIter, which can be obtained withrdr.records (), but it didn't work. Therefore, it was converted to ʻIter-> Vec` and supported.

extern crate csv;
use csv::StringRecord;
use std::env;
use std::error::Error;
use std::ffi::OsString;
use std::fs::File;

pub fn run() -> Result<(), Box<Error>> {
    let file_path = "file.csv";
    let file = File::open(file_path)?;
    let mut rdr = csv::Reader::from_reader(file);
    let _records = rdr.records();
    let records: Vec<StringRecord> = _records.map(|x| x.unwrap()).collect();
    let data_len = &records.len();
    
    for i in 0..*data_len {
        for j in 0..*data_len {
            let r_i = &records[i];
            let lat_i = &r_i[1];
            let lat_i: f64 = lat_i.parse().unwrap();
            let lng_i = &r_i[2];
            let lng_i: f64 = lng_i.parse().unwrap();
            let r_j = &records[j];
            let lat_j = &r_j[1];
            let lat_j: f64 = lat_j.parse().unwrap();
            let lng_j = &r_j[2];
            let lng_j: f64 = lng_j.parse().unwrap();

            let dist = cal_rho(&lat_i, &lng_i, &lat_j, &lng_j);

            println!("{}", dist);
        }
    }
    Ok(())
}

It's a fumbling implementation, so I'm waiting for advice if there is a good way.

Recommended Posts

[Rust] Read the latitude / longitude csv data to find the distance between two points
Distance calculation between two latitude and longitude points with python (using spherical trigonometry)
Find the distance from latitude and longitude (considering the roundness of the earth).
Approximation of distance between two points on the surface of a spheroid (on the surface of the earth)
Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data
Coloring points according to the distance from the regression curve
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Write a python program to find the editing distance [python] [Levenshtein distance]
Various ways to calculate the similarity between data in python
[Introduction to matplotlib] Read the end time from COVID-19 data ♬