h3o_cli/commands/
cell_to_polygon.rs1use anyhow::{Context, Result as AnyResult};
4use clap::{Parser, ValueEnum};
5use geojson::Feature;
6use h3o::{CellIndex, geom::SolventBuilder};
7use kml::Kml;
8
9#[derive(Parser, Debug)]
13pub struct Args {
14 #[arg(short, long)]
16 index: Option<CellIndex>,
17
18 #[arg(short, long, value_enum, default_value_t = Format::Geojson)]
20 format: Format,
21
22 #[arg(short, long, default_value_t = false)]
24 pretty: bool,
25}
26
27#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum)]
28enum Format {
29 Geojson,
30 Kml,
31}
32
33pub fn run(args: &Args) -> AnyResult<()> {
35 let indexes = crate::utils::get_cell_indexes(args.index)
36 .collect::<AnyResult<Vec<_>>>()?;
37 let solvent = SolventBuilder::new().build();
38
39 match args.format {
40 Format::Geojson => {
41 let geometry =
42 solvent.dissolve(indexes).context("compute GeoJSON")?;
43 let feature = Feature {
44 bbox: None,
45 geometry: Some((&geometry).into()),
46 id: None,
47 properties: None,
48 foreign_members: None,
49 };
50 crate::json::print(&feature, args.pretty)?;
51 }
52 Format::Kml => {
53 let style_id = "lineStyle1";
54 let style = kml::types::Style {
55 id: Some(style_id.to_owned()),
56 line: Some(kml::types::LineStyle {
57 id: Some("lineStyle2".to_owned()),
58 color: "ff0000ff".to_owned(),
59 width: 2.,
60 ..kml::types::LineStyle::default()
61 }),
62 ..kml::types::Style::default()
63 };
64
65 let elements = vec![
66 Kml::Style(style),
67 crate::kml::polygons(
68 solvent.dissolve(indexes).context("compute polygons")?,
69 style_id,
70 ),
71 ];
72
73 crate::kml::print_document(
74 "H3 Geometry".to_owned(),
75 "Generated by cellToPolygon".to_owned(),
76 elements,
77 )?;
78 }
79 }
80
81 Ok(())
82}