Created
September 16, 2020 03:21
-
-
Save ficapy/f289b28b653bdd7123f807598b723e08 to your computer and use it in GitHub Desktop.
Python: 60秒 Golang: 3.3秒 Rust: 2.6秒
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/csv" | |
| "github.com/mmcloughlin/geohash" | |
| "github.com/suifengtec/gocoord" | |
| "io" | |
| "os" | |
| ) | |
| func bd092gcj02(input string) string { | |
| x, y := geohash.Decode(input) | |
| p := gocoord.BD09ToGCJ02(gocoord.Position{Lon: y, Lat: x}) | |
| return geohash.EncodeWithPrecision(p.Lat, p.Lon, 7) | |
| } | |
| func handle_one(src string, dst string) { | |
| recordFile, _ := os.Open(src) | |
| writeFile, _ := os.Create(dst) | |
| reader := csv.NewReader(recordFile) | |
| writer := csv.NewWriter(writeFile) | |
| reader.Comma = '\t' | |
| for { | |
| line, err := reader.Read() | |
| if err == io.EOF { | |
| break | |
| } | |
| line[0] = bd092gcj02(line[0]) | |
| writer.Write(line) | |
| } | |
| } | |
| func main() { | |
| handle_one("/Users/ficapy/Downloads/worker.txt", "worker_result.txt") | |
| handle_one("/Users/ficapy/Downloads/residence.txt", "residence_result.txt") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import geohash2 as gh | |
| from coord_convert.transform import bd2gcj, bd2wgs | |
| PATH1 = '/Users/ficapy/Downloads/worker.txt' | |
| PATH2 = '/Users/ficapy/Downloads/residence.txt' | |
| def bd09_to_gcj02(input: str): | |
| x, y, *_ = gh.decode_exactly(input) | |
| x, y = bd2gcj(float(x), float(y)) | |
| ret = gh.encode(x, y, 7) | |
| return ret | |
| def handle_one(src, dst): | |
| with open(src) as csvfile, open(dst, mode='w') as csvwriter: | |
| rdr = csv.reader(csvfile, delimiter='\t') | |
| wdr = csv.writer(csvwriter, delimiter='\t') | |
| for row in rdr: | |
| row[0] = bd09_to_gcj02(row[0]) | |
| wdr.writerow(row) | |
| if __name__ == '__main__': | |
| handle_one(PATH1, "worker.txt") | |
| handle_one(PATH2, "residence.txt") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use csv; | |
| use csv::{ReaderBuilder, WriterBuilder}; | |
| use geohash::{decode, encode, Coordinate}; | |
| use std::error::Error; | |
| use undrift_gps::bd_to_gcj; | |
| const FILE_PATH: &'static str = "/Users/ficapy/Downloads/worker.txt"; | |
| const FILE_PATH2: &'static str = "/Users/ficapy/Downloads/residence.txt"; | |
| fn bd092gcj02(input: &[u8]) -> String { | |
| let (c, _, _) = decode(std::str::from_utf8(&input).unwrap()).unwrap(); | |
| let (y, x) = bd_to_gcj(c.y, c.x); | |
| encode(Coordinate { x, y }, 7).unwrap() | |
| } | |
| fn handle_one(src: &str, dst: &str) -> Result<(), Box<dyn Error>> { | |
| let mut wdr = WriterBuilder::new() | |
| .has_headers(false) | |
| // .delimiter(b'\t') | |
| .from_path(dst)?; | |
| let mut rdr = ReaderBuilder::new() | |
| .has_headers(false) | |
| .delimiter(b'\t') | |
| .from_path(src)?; | |
| let mut record = csv::ByteRecord::new(); | |
| let mut new_record = csv::ByteRecord::new(); | |
| while rdr.read_byte_record(&mut record)? { | |
| record | |
| .iter() | |
| .enumerate() | |
| .map(|(i, v)| { | |
| if i == 0 { | |
| new_record.push_field(&bd092gcj02(&record[0]).as_bytes().to_vec()) | |
| } else { | |
| new_record.push_field(v) | |
| } | |
| }) | |
| .count(); | |
| wdr.write_record(&new_record)?; | |
| new_record.clear(); | |
| } | |
| Ok(()) | |
| } | |
| fn main() -> Result<(), Box<dyn Error>> { | |
| handle_one(FILE_PATH, "worker_result.txt")?; | |
| handle_one(FILE_PATH2, "residence_result.txt")?; | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment