-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Do Not Merge] GSIGEO2024 #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
//! Convert .asc to .bin.lz4 | ||
//! | ||
//! Usage: | ||
//! cargo run --example convert_asc_to_bin_lz4 -- input.asc | ||
|
||
use std::fs::{self, File}; | ||
use std::io::{BufReader, Write}; | ||
use std::path::Path; | ||
|
||
use japan_geoid::gsi::MemoryGrid; | ||
use japan_geoid::Geoid; | ||
|
||
fn main() -> std::io::Result<()> { | ||
let argv = std::env::args().collect::<Vec<_>>(); | ||
let path = Path::new(&argv[1]); | ||
|
||
let (lng, lat) = (138.2839817085188, 37.12378643088312); | ||
|
||
// Load from the original ascii format. | ||
let mut reader = BufReader::new(File::open("./gsigeo2011_ver2_2.asc")?); | ||
let mut reader = BufReader::new(File::open(path)?); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 入力ファイルが存在しない場合や読み込みに失敗した場合のエラーハンドリングを追加することをお勧めします。これにより、ユーザーにより具体的なエラー情報を提供できます。 |
||
let geoid = MemoryGrid::from_ascii_reader(&mut reader)?; | ||
|
||
// Calculate the geoid height. | ||
|
@@ -21,12 +30,11 @@ fn main() -> std::io::Result<()> { | |
// Dump as the efficient binary format. | ||
let mut buf = Vec::new(); | ||
geoid.to_binary_writer(&mut buf)?; | ||
File::create("./gsigeo2011_ver2_2.bin.lz4")? | ||
.write_all(&lz4_flex::compress_prepend_size(&buf))?; | ||
let out_path = path.with_extension("bin.lz4"); | ||
File::create(&out_path)?.write_all(&lz4_flex::compress_prepend_size(&buf))?; | ||
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 出力ファイルの書き込みに失敗した場合のエラーハンドリングを追加することをお勧めします。これにより、ユーザーにより具体的なエラー情報を提供できます。 |
||
|
||
// Load the binary model. | ||
let decompressed = | ||
&lz4_flex::decompress_size_prepended(&fs::read("./gsigeo2011_ver2_2.bin.lz4")?).unwrap(); | ||
let decompressed = &lz4_flex::decompress_size_prepended(&fs::read(&out_path)?).unwrap(); | ||
let geoid = MemoryGrid::from_binary_reader(&mut std::io::Cursor::new(decompressed))?; | ||
|
||
let height = geoid.get_height(lng, lat); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
usage: isg2asc GSIGEO2024beta.isg | ||
""" | ||
|
||
import sys | ||
from pathlib import Path | ||
|
||
data_points = [] | ||
|
||
filename = Path(sys.argv[1]) | ||
|
||
with open(filename, "r") as f: | ||
for line in f: | ||
if line.strip().startswith("end_of_head ========"): | ||
break | ||
|
||
for line in f: | ||
data_points.extend(v for v in line.split()) | ||
|
||
Comment on lines
+10
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ファイルが存在しない場合や読み込みに失敗した場合のエラーハンドリングを追加することをお勧めします。例えば、 |
||
NX = 1601 | ||
NY = 2101 | ||
|
||
assert len(data_points) == NX * NY | ||
Comment on lines
+17
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 本番環境での使用を考慮して、 |
||
|
||
with open(filename.with_suffix(".asc"), "w") as f: | ||
f.write("15.00000 120.00000 0.016667 0.025000 2101 1601 1 ver-beta") | ||
f.write("\n") | ||
f.write("\n") | ||
for i in range(NY): | ||
for j in range(NX): | ||
v = data_points[(NY - i - 1) * NX + j] | ||
if v == "-9999.0000": | ||
v = "999.0000" | ||
f.write(v) | ||
f.write(" ") | ||
f.write("\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コマンドライン引数が指定されていない場合に、ユーザーにわかりやすいエラーメッセージを表示するエラーハンドリングを追加することをお勧めします。