Skip to content
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

GeoPackage出力ドライバーの実装 #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"nusamai-geometry",
"nusamai-gltf",
"nusamai-geojson",
"nusamai-geopackage",
"nusamai-plateau",
"nusamai-mvt",
"nusamai-projection",
Expand Down
15 changes: 15 additions & 0 deletions nusamai-geopackage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "nusamai-geopackage"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nusamai-geometry = { path = "../nusamai-geometry" }
nusamai-plateau = { path = "../nusamai-plateau" }
citygml = {path = "../nusamai-plateau/citygml" }

[dev-dependencies]
clap = { version = "4.4.11", features = ["derive"] }
quick-xml = "0.31.0"
84 changes: 84 additions & 0 deletions nusamai-geopackage/examples/gml2gpkg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use citygml::{CityGMLElement, CityGMLReader, Geometries, ParseError, SubTreeReader};
use clap::Parser;
use nusamai_plateau::models::CityObject;
use std::io::BufRead;

#[derive(Parser)]
struct Args {
#[clap(required = true)]
filename: String,
}

struct TopLevelCityObject<'a> {
cityobj: citygml::object::FeatureOrData<'a>,
geometries: Geometries,
}

fn toplevel_dispatcher<R: BufRead>(
st: &mut SubTreeReader<R>,
) -> Result<Vec<(CityObject, Geometries)>, ParseError> {
let mut items: Vec<(CityObject, Geometries)> = vec![];

match st.parse_children(|st| match st.current_path() {
b"core:cityObjectMember" => {
let mut cityobj: CityObject = Default::default();
cityobj.parse(st)?;
let geometries = st.collect_geometries();
items.push((cityobj, geometries));
Ok(())
}
b"gml:boundedBy" | b"app:appearanceMember" => {
st.skip_current_element()?;
Ok(())
}
other => Err(ParseError::SchemaViolation(format!(
"Unrecognized element {}",
String::from_utf8_lossy(other)
))),
}) {
Ok(_) => Ok(items),
Err(e) => {
println!("Err: {:?}", e);
Err(e)
}
}
}

fn main() {
let args = Args::parse();

let reader = std::io::BufReader::new(std::fs::File::open(args.filename).unwrap());
let mut xml_reader = quick_xml::NsReader::from_reader(reader);

let items = match CityGMLReader::new().start_root(&mut xml_reader) {
Ok(mut st) => match toplevel_dispatcher(&mut st) {
Ok(items) => items,
Err(e) => panic!("Err: {:?}", e),
},
Err(e) => panic!("Err: {:?}", e),
};

let tlc_objs: Vec<_> = items
.iter()
.map(|(o, g)| {
let cityobj = match o.objectify().unwrap() {
citygml::object::ObjectValue::FeatureOrData(fod) => fod,
_ => panic!("Not a FeatureOrData"),
};

TopLevelCityObject {
cityobj,
geometries: g.clone(),
}
})
.collect();

for (i, o) in tlc_objs.iter().enumerate() {
println!();
println!("{}", i);
println!("{:?}", o.cityobj);
println!("{:?}", o.geometries);
}

// TODO: tlc_objs を 受け取り、 GeoPackage にして、書き出す
}
14 changes: 14 additions & 0 deletions nusamai-geopackage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
}
}
2 changes: 1 addition & 1 deletion nusamai-plateau/citygml/src/geometric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

/// Geometries in a toplevel city object and its children.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]

Check warning on line 37 in nusamai-plateau/citygml/src/geometric.rs

View check run for this annotation

Codecov / codecov/patch

nusamai-plateau/citygml/src/geometric.rs#L37

Added line #L37 was not covered by tests
pub struct Geometries {
pub vertices: Vec<[f64; 3]>,
pub multipolygon: MultiPolygon<'static, 1, u32>,
Expand Down