use cadkit_core::{Point3, Value};
use serde::{Deserialize, Serialize};
pub const CORE: &str = "http://www.opengis.net/citygml/2.0";
pub const GML: &str = "http://www.opengis.net/gml";
pub const BUILDING: &str = "http://www.opengis.net/citygml/building/2.0";
pub const GENERICS: &str = "http://www.opengis.net/citygml/generics/2.0";
pub const GROUPS: &str = "http://www.opengis.net/citygml/cityobjectgroup/2.0";
pub const XLINK: &str = "http://www.w3.org/1999/xlink";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Name {
pub qualified: String,
pub namespace: String,
}
impl Name {
pub fn new(namespace: &str, qualified: &str) -> Self {
Self {
qualified: qualified.into(),
namespace: namespace.into(),
}
}
pub fn local(&self) -> &str {
self.qualified.rsplit(':').next().unwrap_or(&self.qualified)
}
pub fn is(&self, namespace: &str, local: &str) -> bool {
self.namespace == namespace && self.local() == local
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct XmlAttribute {
pub name: Name,
pub value: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
pub enum Node {
Element(Element),
Text(String),
Comment(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Element {
pub name: Name,
pub attributes: Vec<XmlAttribute>,
pub children: Vec<Node>,
#[serde(default)]
pub offset: u64,
}
impl Element {
pub fn new(namespace: &str, name: &str) -> Self {
Self {
name: Name::new(namespace, name),
attributes: vec![],
children: vec![],
offset: 0,
}
}
pub fn set_attribute(&mut self, namespace: &str, name: &str, value: impl Into<String>) {
let key = Name::new(namespace, name);
if let Some(a) = self
.attributes
.iter_mut()
.find(|a| a.name.is(namespace, key.local()))
{
a.value = value.into();
} else {
self.attributes.push(XmlAttribute {
name: key,
value: value.into(),
});
}
}
pub fn attribute(&self, namespace: &str, local: &str) -> Option<&str> {
self.attributes
.iter()
.find(|a| a.name.is(namespace, local))
.map(|a| a.value.as_str())
}
pub fn elements(&self) -> impl Iterator<Item = &Element> {
self.children.iter().filter_map(|n| {
if let Node::Element(e) = n {
Some(e)
} else {
None
}
})
}
pub fn text(&self) -> String {
self.children
.iter()
.filter_map(|n| {
if let Node::Text(t) = n {
Some(t.as_str())
} else {
None
}
})
.collect()
}
pub fn push(&mut self, child: Element) {
self.children.push(Node::Element(child));
}
pub fn with_text(namespace: &str, name: &str, text: impl Into<String>) -> Self {
let mut e = Self::new(namespace, name);
e.children.push(Node::Text(text.into()));
e
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[allow(missing_docs)]
pub enum ObjectKind {
Building,
BuildingPart,
Room,
Door,
Window,
BuildingInstallation,
WallSurface,
InteriorWallSurface,
RoofSurface,
GroundSurface,
FloorSurface,
CeilingSurface,
ClosureSurface,
OuterFloorSurface,
OuterCeilingSurface,
GenericCityObject,
CityObjectGroup,
}
impl ObjectKind {
pub fn xml_name(self) -> (&'static str, &'static str) {
match self {
Self::GenericCityObject => (GENERICS, "gen:GenericCityObject"),
Self::CityObjectGroup => (GROUPS, "grp:CityObjectGroup"),
Self::Building => (BUILDING, "bldg:Building"),
Self::BuildingPart => (BUILDING, "bldg:BuildingPart"),
Self::Room => (BUILDING, "bldg:Room"),
Self::Door => (BUILDING, "bldg:Door"),
Self::Window => (BUILDING, "bldg:Window"),
Self::BuildingInstallation => (BUILDING, "bldg:BuildingInstallation"),
Self::WallSurface => (BUILDING, "bldg:WallSurface"),
Self::InteriorWallSurface => (BUILDING, "bldg:InteriorWallSurface"),
Self::RoofSurface => (BUILDING, "bldg:RoofSurface"),
Self::GroundSurface => (BUILDING, "bldg:GroundSurface"),
Self::FloorSurface => (BUILDING, "bldg:FloorSurface"),
Self::CeilingSurface => (BUILDING, "bldg:CeilingSurface"),
Self::ClosureSurface => (BUILDING, "bldg:ClosureSurface"),
Self::OuterFloorSurface => (BUILDING, "bldg:OuterFloorSurface"),
Self::OuterCeilingSurface => (BUILDING, "bldg:OuterCeilingSurface"),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CityObject {
pub kind: ObjectKind,
pub id: String,
pub properties: Vec<Element>,
}
impl CityObject {
pub fn new(kind: ObjectKind, id: impl Into<String>) -> Self {
Self {
kind,
id: id.into(),
properties: vec![],
}
}
pub fn into_element(self) -> Element {
let (ns, name) = self.kind.xml_name();
let mut e = Element::new(ns, name);
e.set_attribute(GML, "gml:id", self.id);
for p in self.properties {
e.push(p);
}
e
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Polygon {
pub id: Option<String>,
pub exterior: Vec<Point3>,
pub interiors: Vec<Vec<Point3>>,
}
impl Polygon {
pub fn to_element(&self) -> Element {
let mut p = Element::new(GML, "gml:Polygon");
if let Some(id) = &self.id {
p.set_attribute(GML, "gml:id", id);
}
for (i, ring) in std::iter::once(&self.exterior)
.chain(self.interiors.iter())
.enumerate()
{
let mut boundary = Element::new(
GML,
if i == 0 {
"gml:exterior"
} else {
"gml:interior"
},
);
let mut linear = Element::new(GML, "gml:LinearRing");
for v in ring {
linear.push(Element::with_text(
GML,
"gml:pos",
format!("{} {} {}", v.x, v.y, v.z),
));
}
boundary.push(linear);
p.push(boundary);
}
p
}
}
pub fn generic_attribute(name: &str, value: &Value) -> cadkit_core::Result<Element> {
let (kind, text) = match value {
Value::Text(s) => ("gen:stringAttribute", s.clone()),
Value::Int(n) => ("gen:intAttribute", n.to_string()),
Value::Float(n) if n.is_finite() => ("gen:doubleAttribute", n.to_string()),
_ => {
return Err(cadkit_core::Error::Unsupported(
"CityGML generic attribute type".into(),
));
}
};
let mut e = Element::new(GENERICS, kind);
e.set_attribute("", "name", name);
e.push(Element::with_text(GENERICS, "gen:value", text));
Ok(e)
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CityGmlDocument {
pub root: Element,
}
impl Default for CityGmlDocument {
fn default() -> Self {
Self::new()
}
}
impl CityGmlDocument {
pub fn new() -> Self {
let mut root = Element::new(CORE, "core:CityModel");
for (prefix, uri) in [
("core", CORE),
("gml", GML),
("bldg", BUILDING),
("gen", GENERICS),
("grp", GROUPS),
("xlink", XLINK),
] {
root.set_attribute(
"http://www.w3.org/2000/xmlns/",
&format!("xmlns:{prefix}"),
uri,
);
}
Self { root }
}
pub fn add_object(&mut self, object: CityObject) {
let mut member = Element::new(CORE, "core:cityObjectMember");
member.push(object.into_element());
self.root.push(member);
}
}