-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
482 lines (457 loc) · 16.9 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use crate::cityjson::Appearance;
use crate::cityjson::CityJSON;
use crate::cityjson::CityJSONFeature;
use crate::cityjson::CityObject;
use crate::cityjson::GeometryTemplates;
use crate::cityjson::Transform;
use serde_json::{json, Value};
extern crate clap;
use rand::Rng;
use std::fmt;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::{self, Read, Write};
use std::path::PathBuf;
use std::collections::HashMap;
mod cityjson;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(version, about = "Create/process/modify CityJSONSeq files", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// CityJSONSeq ==> CityJSON
Cat {
/// CityJSONSeq input file
#[arg(short, long)]
file: Option<PathBuf>,
},
/// CityJSON ==> CityJSONSeq
Collect {
/// CityJSON input file
#[arg(short, long)]
file: Option<PathBuf>,
},
/// Filter a CityJSONSeq
Filter {
/// Bounding box filter
#[arg(long, value_names = &["minx", "miny", "maxx", "maxy"], value_delimiter = ' ', num_args = 4, group = "exclusive")]
bbox: Option<Vec<f64>>,
/// Keep only the CityObjects of this type
#[arg(long, group = "exclusive")]
cotype: Option<String>,
/// Excludes the selection, thus delete the selected city object(s)
#[arg(long)]
exclude: bool,
/// Circle filter: centre + radius
#[arg(
long,
value_names = &["x", "y", "radius"],
value_delimiter = ' ',
num_args = 3,
group = "exclusive"
)]
radius: Option<Vec<f64>>,
/// 1/X chances of a given feature being kept
#[arg(long, value_name = "X", value_parser = clap::value_parser!(u32).range(1..), group = "exclusive")]
random: Option<u32>,
},
}
#[derive(Debug)]
enum MyError {
IoError(std::io::Error),
JsonError(serde_json::Error),
CityJsonError(String),
}
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MyError::JsonError(json_error) => write!(f, "Error (JSON): {}", json_error),
MyError::IoError(io_error) => write!(f, "Error (io): {}", io_error),
MyError::CityJsonError(cjson_error) => write!(f, "Error (CityJSON): {}", cjson_error),
}
}
}
impl std::error::Error for MyError {}
impl From<serde_json::Error> for MyError {
fn from(err: serde_json::Error) -> Self {
MyError::JsonError(err)
}
}
impl From<std::io::Error> for MyError {
fn from(err: std::io::Error) -> Self {
MyError::IoError(err)
}
}
fn main() {
let cli = Cli::parse();
match &cli.command {
//-- cat
Commands::Cat { file } => match file {
Some(x) => {
if let Err(e) = cat_from_file(x) {
eprintln!("{e}");
std::process::exit(1);
}
}
None => {
if let Err(e) = cat_from_stdin() {
eprintln!("{e}");
std::process::exit(1);
}
}
},
//-- collect
Commands::Collect { file } => match file {
Some(x) => {
if let Err(e) = collect_from_file(x) {
eprintln!("{e}");
std::process::exit(1);
}
}
None => {
if let Err(e) = collect_from_stdin() {
eprintln!("{e}");
std::process::exit(1);
}
}
},
//-- filter
Commands::Filter {
bbox,
cotype,
exclude,
radius,
random,
} => {
if bbox.is_some() {
if let Err(e) = filter_bbox(*exclude, &bbox.clone().unwrap()) {
eprintln!("{e}");
std::process::exit(1);
}
}
if cotype.is_some() {
if let Err(e) = filter_cotype(*exclude, cotype.clone().unwrap()) {
eprintln!("{e}");
std::process::exit(1);
}
}
if radius.is_some() {
let p: Vec<f64> = radius.clone().unwrap();
if let Err(e) = filter_radius(*exclude, p[0], p[1], p[2]) {
eprintln!("{e}");
std::process::exit(1);
}
}
if random.is_some() {
if let Err(e) = filter_random(*exclude, random.unwrap()) {
eprintln!("{e}");
std::process::exit(1);
}
}
}
}
}
fn filter_random(exclude: bool, rand_factor: u32) -> Result<(), MyError> {
let stdin = std::io::stdin();
let mut rng = rand::thread_rng();
for (i, line) in stdin.lock().lines().enumerate() {
let mut w: bool = false;
let l = line.unwrap();
if i == 0 {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
} else {
let r: u32 = rng.gen_range(1..=rand_factor);
if r == 1 {
w = true;
}
if (w == true && !exclude) || (w == false && exclude) {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
}
}
}
Ok(())
}
fn filter_cotype(exclude: bool, cotype: String) -> Result<(), MyError> {
let stdin = std::io::stdin();
for (i, line) in stdin.lock().lines().enumerate() {
let mut w: bool = false;
let l = line.unwrap();
if i == 0 {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
} else {
let cjf: CityJSONFeature = serde_json::from_str(&l)?;
if cjf.city_objects[&cjf.id].thetype == cotype {
w = true;
}
if (w == true && !exclude) || (w == false && exclude) {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
}
}
}
Ok(())
}
fn filter_bbox(exclude: bool, bbox: &Vec<f64>) -> Result<(), MyError> {
let stdin = std::io::stdin();
let mut transform: Transform = Transform::new();
for (i, line) in stdin.lock().lines().enumerate() {
let mut w: bool = false;
let l = line.unwrap();
if i == 0 {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
let cj: CityJSON = serde_json::from_str(&l)?;
transform = cj.transform;
} else {
let cjf: CityJSONFeature = serde_json::from_str(&l)?;
let ci = cjf.centroid();
let cx = (ci[0] * transform.scale[0]) + transform.translate[0];
let cy = (ci[1] * transform.scale[1]) + transform.translate[1];
if (cx > bbox[0]) && (cx < bbox[2]) && (cy > bbox[1]) && (cy < bbox[3]) {
w = true;
}
if (w == true && !exclude) || (w == false && exclude) {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
}
}
}
Ok(())
}
fn filter_radius(exclude: bool, x: f64, y: f64, r: f64) -> Result<(), MyError> {
let stdin = std::io::stdin();
let mut transform: Transform = Transform::new();
for (i, line) in stdin.lock().lines().enumerate() {
let mut w: bool = false;
let l = line.unwrap();
if i == 0 {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
let cj: CityJSON = serde_json::from_str(&l)?;
transform = cj.transform;
} else {
let cjf: CityJSONFeature = serde_json::from_str(&l)?;
let ci = cjf.centroid();
let cx = (ci[0] * transform.scale[0]) + transform.translate[0];
let cy = (ci[1] * transform.scale[1]) + transform.translate[1];
let d2 = (cx - x).powf(2.0) + (cy - y).powf(2.0);
if d2 <= (r * r) {
w = true;
}
if (w == true && !exclude) || (w == false && exclude) {
io::stdout().write_all(&format!("{}\n", l).as_bytes())?;
}
}
}
Ok(())
}
fn collect_from_stdin() -> Result<(), MyError> {
let stdin = std::io::stdin();
let mut cjj: CityJSON = CityJSON::new();
for (i, line) in stdin.lock().lines().enumerate() {
let l = line.unwrap();
if i == 0 {
cjj = serde_json::from_str(&l)?;
} else {
let cjf: CityJSONFeature = serde_json::from_str(&l)?;
cjj.add_one_cjf(cjf);
}
}
cjj.retransform();
cjj.remove_duplicate_vertices();
io::stdout().write_all(&format!("{}\n", serde_json::to_string(&cjj).unwrap()).as_bytes())?;
Ok(())
}
fn collect_from_file(file: &PathBuf) -> Result<(), MyError> {
let f = File::open(file.canonicalize()?)?;
let br = BufReader::new(f);
let mut cjj: CityJSON = CityJSON::new();
for (i, line) in br.lines().enumerate() {
match &line {
Ok(l) => {
if i == 0 {
cjj = serde_json::from_str(&l)?;
} else {
let cjf: CityJSONFeature = serde_json::from_str(&l)?;
cjj.add_one_cjf(cjf);
}
}
Err(error) => eprintln!("Error reading line: {}", error),
}
}
cjj.remove_duplicate_vertices();
io::stdout().write_all(&format!("{}\n", serde_json::to_string(&cjj).unwrap()).as_bytes())?;
Ok(())
}
fn cat_from_stdin() -> Result<(), MyError> {
let mut input = String::new();
match std::io::stdin().read_to_string(&mut input) {
Ok(_) => {
let cjj: CityJSON = serde_json::from_str(&input)?;
let _ = cat(&cjj)?;
}
Err(error) => {
eprintln!("Error: {}", error);
}
}
Ok(())
}
fn cat_from_file(file: &PathBuf) -> Result<(), MyError> {
let f = File::open(file.canonicalize()?)?;
let br = BufReader::new(f);
let cjj: CityJSON = serde_json::from_reader(br)?;
cat(&cjj)?;
Ok(())
}
fn cat(cjj: &CityJSON) -> Result<(), MyError> {
if cjj.thetype != "CityJSON" {
return Err(MyError::CityJsonError(
"Input file not CityJSON.".to_string(),
));
}
if cjj.version != "1.1" && cjj.version != "2.0" {
return Err(MyError::CityJsonError(
"Input file not CityJSON v1.1 nor v2.0.".to_string(),
));
}
//-- first line: the CityJSON "metadata"
let mut cj1: CityJSON = cjj.get_empty_copy();
//-- if geometry-templates have material/textures then these need to be added to 1st line
match &cjj.geometry_templates {
Some(x) => {
let mut gts2: GeometryTemplates = x.clone();
let mut m_oldnew: HashMap<usize, usize> = HashMap::new();
let mut t_oldnew: HashMap<usize, usize> = HashMap::new();
let mut t_v_oldnew: HashMap<usize, usize> = HashMap::new();
for g in &mut gts2.templates {
g.update_material(&mut m_oldnew);
g.update_texture(&mut t_oldnew, &mut t_v_oldnew, 0);
}
//-- "slice" materials
if cjj.appearance.is_some() {
let a = cjj.appearance.as_ref().unwrap();
let mut acjf: Appearance = Appearance::new();
acjf.default_theme_material = a.default_theme_material.clone();
acjf.default_theme_texture = a.default_theme_texture.clone();
if a.materials.is_some() {
let am = a.materials.as_ref().unwrap();
let mut mats2: Vec<Value> = Vec::new();
mats2.resize(m_oldnew.len(), json!(null));
for (old, new) in &m_oldnew {
mats2[*new] = am[*old].clone();
}
acjf.materials = Some(mats2);
}
if a.textures.is_some() {
let at = a.textures.as_ref().unwrap();
let mut texs2: Vec<Value> = Vec::new();
texs2.resize(t_oldnew.len(), json!(null));
for (old, new) in &t_oldnew {
texs2[*new] = at[*old].clone();
}
acjf.textures = Some(texs2);
}
if a.vertices_texture.is_some() {
let atv = a.vertices_texture.as_ref().unwrap();
let mut t_new_vertices: Vec<Vec<f64>> = Vec::new();
t_new_vertices.resize(t_v_oldnew.len(), vec![]);
for (old, new) in &t_v_oldnew {
t_new_vertices[*new] = atv[*old].clone();
}
acjf.vertices_texture = Some(t_new_vertices);
}
cj1.appearance = Some(acjf);
}
}
None => (),
}
io::stdout().write_all(&format!("{}\n", serde_json::to_string(&cj1).unwrap()).as_bytes())?;
//-- the other lines
let cos = &cjj.city_objects;
for (key, co) in cos {
if co.is_toplevel() {
let mut cjf = CityJSONFeature::new();
let mut co2: CityObject = co.clone();
let mut g_vi_oldnew: HashMap<usize, usize> = HashMap::new();
let mut m_oldnew: HashMap<usize, usize> = HashMap::new();
let mut t_oldnew: HashMap<usize, usize> = HashMap::new();
let mut t_v_oldnew: HashMap<usize, usize> = HashMap::new();
match &mut co2.geometry {
Some(x) => {
for g in x.iter_mut() {
g.update_geometry_boundaries(&mut g_vi_oldnew);
g.update_material(&mut m_oldnew);
g.update_texture(&mut t_oldnew, &mut t_v_oldnew, 0);
}
}
None => (),
}
cjf.add_co(key.clone(), co2);
cjf.id = key.to_string();
//-- TODO: to fix: children-of-children?
//-- process all the children (only one-level lower)
for childkey in co.get_children_keys() {
let coc = cos.get(&childkey).unwrap();
let mut coc2: CityObject = coc.clone();
match &mut coc2.geometry {
Some(x) => {
for g in x.iter_mut() {
g.update_geometry_boundaries(&mut g_vi_oldnew);
g.update_material(&mut m_oldnew);
g.update_texture(&mut t_oldnew, &mut t_v_oldnew, 0);
}
}
None => (),
}
cjf.add_co(childkey.clone(), coc2);
}
//-- "slice" geometry vertices
let allvertices = &cjj.vertices;
let mut g_new_vertices: Vec<Vec<i64>> = Vec::new();
g_new_vertices.resize(g_vi_oldnew.len(), vec![]);
for (old, new) in &g_vi_oldnew {
g_new_vertices[*new] = allvertices[*old].clone();
}
cjf.vertices = g_new_vertices;
//-- "slice" materials
if cjj.appearance.is_some() {
let a = cjj.appearance.as_ref().unwrap();
let mut acjf: Appearance = Appearance::new();
acjf.default_theme_material = a.default_theme_material.clone();
acjf.default_theme_texture = a.default_theme_texture.clone();
if a.materials.is_some() {
let am = a.materials.as_ref().unwrap();
let mut mats2: Vec<Value> = Vec::new();
mats2.resize(m_oldnew.len(), json!(null));
for (old, new) in &m_oldnew {
mats2[*new] = am[*old].clone();
}
acjf.materials = Some(mats2);
}
if a.textures.is_some() {
let at = a.textures.as_ref().unwrap();
let mut texs2: Vec<Value> = Vec::new();
texs2.resize(t_oldnew.len(), json!(null));
for (old, new) in &t_oldnew {
texs2[*new] = at[*old].clone();
}
acjf.textures = Some(texs2);
}
if a.vertices_texture.is_some() {
let atv = a.vertices_texture.as_ref().unwrap();
let mut t_new_vertices: Vec<Vec<f64>> = Vec::new();
t_new_vertices.resize(t_v_oldnew.len(), vec![]);
for (old, new) in &t_v_oldnew {
t_new_vertices[*new] = atv[*old].clone();
}
acjf.vertices_texture = Some(t_new_vertices);
}
cjf.appearance = Some(acjf);
}
io::stdout()
.write_all(&format!("{}\n", serde_json::to_string(&cjf).unwrap()).as_bytes())?;
}
}
Ok(())
}