cadkit-gml 0.5.3

Bounded CityGML 2.0 reader, writer and neutral geometry bridge.
Documentation
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
//! Local preflight for explicitly selected TKGM architectural model workflows.
//!
//! This is a bounded subset of the public production guide, not TKGM acceptance.
//! Geometry validation, official XSD validation, identifiers checked against MAKS /
//! TAKBIS, naming rules, all code lists and the online rule engine remain separate.
//! Source: <https://cbs.tkgm.gov.tr/surdurulebilirlik/> (retrieved 2026-10-01).
//! Tender source: <https://cbs.tkgm.gov.tr/3d/html/> (retrieved 2026-10-01).

use crate::{
    CityGmlDocument, ValidationOptions,
    native::*,
    validate::{elements, validate},
};
use cadkit_core::{Error, Limits, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Workflow selection is required; a recipient name alone does not identify its rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Profile {
    /// Architectural city-model tender common rules from production guide v2.50.
    /// Tender-year-specific MAKS and sustainability extensions remain unchecked.
    CityModelTender,
    /// Digital Building condominium/easement registration guide 3.0.3.
    DigitalBuildingRegistration,
}

impl std::str::FromStr for Profile {
    type Err = Error;

    fn from_str(name: &str) -> Result<Self> {
        match name {
            "city-model-tender" => Ok(Self::CityModelTender),
            "digital-building-registration" => Ok(Self::DigitalBuildingRegistration),
            _ => Err(Error::invalid(
                0,
                "TKGM profile must be city-model-tender or digital-building-registration",
            )),
        }
    }
}

/// How certain a [`ProfileIssue`] is. Serialized as `"error"` / `"review"`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Severity {
    /// A definite mismatch with a local rule.
    Error,
    /// A conditional case that needs a human decision.
    Review,
}

impl Severity {
    /// The serialized name: `"error"` or `"review"`.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Error => "error",
            Self::Review => "review",
        }
    }
}

/// A local profile finding. No source names, IDs, attribute values or coordinates.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ProfileIssue {
    /// Stable code, for example `tkgm.attribute_type`.
    pub code: String,
    /// [`Severity::Error`] for a definite local-rule mismatch, [`Severity::Review`] for
    /// conditional cases. Neither severity establishes the receiving authority's decision.
    pub severity: Severity,
    /// Zero-based index in the bounded native element traversal.
    pub element: usize,
    /// Public schema field name, when relevant.
    pub field: Option<String>,
}

/// Results of the documented local subset. Empty issues never imply online acceptance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProfileReport {
    /// Version of the public guide used by these checks, not the OGC CityGML version.
    pub profile: String,
    /// Explicitly selected workflow; never inferred from source values.
    pub workflow: String,
    /// Always false: this API cannot execute the official TKGM rule engine.
    pub acceptance_verified: bool,
    /// Building objects inspected.
    pub buildings: u64,
    /// Groups classified as storeys and inspected.
    pub storeys: u64,
    /// Generic objects classified as independent sections and inspected.
    pub independent_sections: u64,
    /// Missing or inconsistent fields in the checked subset.
    pub issues: Vec<ProfileIssue>,
    /// True when findings exceeded min(max_objects, 10,000); no clean result is implied.
    pub truncated: bool,
    /// Known checks deliberately outside this local subset, as stable names.
    pub unchecked_rules: Vec<String>,
    #[serde(skip)]
    issue_limit: usize,
}

const BUILDING_FIELDS: &[(&str, &str)] = &[
    ("constructionID", "stringAttribute"),
    ("maksIdentityNumber", "intAttribute"),
    ("maksIntegrationState", "intAttribute"),
    ("takbisPropertyIdentityNumber", "intAttribute"),
    ("blockNumber", "stringAttribute"),
    ("parcelNumber", "stringAttribute"),
    ("tenderRegistrationNumber", "stringAttribute"),
    ("buildingType", "intAttribute"),
    ("geometrySuitability", "intAttribute"),
    ("architecturalPlanID", "stringAttribute"),
    ("takbisNeighbourhoodReference", "intAttribute"),
    ("totalIndependentSectionCount", "intAttribute"),
    ("elevatorCount", "intAttribute"),
    ("roofProjectionArea", "doubleAttribute"),
    ("architecturalProjectConfirmationDate", "dateAttribute"),
];
const STOREY_FIELDS: &[(&str, &str)] = &[
    ("storeyNumber", "stringAttribute"),
    ("storeyUsage", "intAttribute"),
    ("independentSectionCount", "intAttribute"),
];
const UNIT_FIELDS: &[(&str, &str)] = &[
    ("geometrySuitability", "intAttribute"),
    ("integrationState", "intAttribute"),
    ("independentSectionNumber", "stringAttribute"),
    ("blockName", "stringAttribute"),
    ("entrance", "stringAttribute"),
    ("takbisPropertyIdentityNumber", "intAttribute"),
    ("maksIdentityNumber", "intAttribute"),
    ("maksNumaratajIdentityNumber", "intAttribute"),
    ("maksIntegrationState", "intAttribute"),
    ("independentSectionCardinalDirection", "intAttribute"),
    ("independentSectionUsage", "intAttribute"),
    ("partCount", "intAttribute"),
    ("independentSectionPlanNetArea", "doubleAttribute"),
    ("independentSectionPlanGrossArea", "doubleAttribute"),
    ("independentSectionCalculatedArea", "doubleAttribute"),
];

const REGISTRATION_UNIT_FIELDS: &[(&str, &str)] = &[
    ("registeredQuality", "stringAttribute"),
    ("registeredInstallation", "stringAttribute"),
    ("registeredFloorDefinition", "stringAttribute"),
    // The registration table and example disagree; never coerce source values.
    ("propertyLot", ""),
];

// The tender guide's MAKS table has different presence rules before 2019,
// in 2019-2020 and after 2020. Check supplied types, but leave these conditions
// to a contract-specific validator rather than applying registration defaults.
const TENDER_CONDITIONAL_FIELDS: &[&str] = &[
    "maksIdentityNumber",
    "maksNumaratajIdentityNumber",
    "maksIntegrationState",
];

impl ProfileReport {
    fn issue(&mut self, code: &str, element: usize, field: Option<&str>) {
        self.finding(code, Severity::Error, element, field);
    }
    fn finding(&mut self, code: &str, severity: Severity, element: usize, field: Option<&str>) {
        if self.issues.len() >= self.issue_limit {
            self.truncated = true;
            return;
        }
        self.issues.push(ProfileIssue {
            code: code.into(),
            severity,
            element,
            field: field.map(str::to_owned),
        });
    }
    fn fields(&mut self, e: &Element, index: usize, fields: &[(&str, &str)], conditional: &[&str]) {
        for &(field, kind) in fields {
            let mut count = 0;
            for a in e
                .elements()
                .filter(|a| a.name.namespace == GENERICS && a.attribute("", "name") == Some(field))
            {
                count += 1;
                if !kind.is_empty() && a.name.local() != kind {
                    self.issue("tkgm.attribute_type", index, Some(field));
                }
            }
            if count == 0 && !conditional.contains(&field) {
                self.issue("tkgm.missing_attribute", index, Some(field));
            }
            if count > 1
                && !matches!(field, "storeyUsage" | "independentSectionCardinalDirection")
                && !(e.name.is(BUILDING, "Building") && field == "geometrySuitability")
            {
                self.issue("tkgm.duplicate_attribute", index, Some(field));
            }
        }
    }
    fn required(&mut self, e: &Element, index: usize, ns: &str, field: &str) {
        if !e.elements().any(|c| c.name.is(ns, field)) {
            self.issue("tkgm.missing_property", index, Some(field));
        }
    }
}

fn class(e: &Element, namespace: &str) -> Option<String> {
    e.elements()
        .find(|n| n.name.is(namespace, "class"))
        .map(Element::text)
}

/// Checks required field presence/types, core geometry-property presence, storey
/// parent/member references and explicit TUREF 3D envelope for the selected workflow.
/// Fixed unit codes apply only to Digital Building registration. The tender subset
/// does not check conditional MAKS presence or sustainability extensions by year.
/// Uses the caller's resource limits and preserves all input values unchanged.
/// Run `validate`, official XSD and TKGM validation separately for their own checks.
pub fn preflight(
    doc: &CityGmlDocument,
    profile: Profile,
    limits: &Limits,
) -> Result<ProfileReport> {
    validate(
        doc,
        &ValidationOptions {
            limits: *limits,
            geometry: false,
            ..Default::default()
        },
    )?;
    let nodes = elements(doc, limits)?;
    let ids: HashMap<_, _> = nodes
        .iter()
        .filter_map(|(e, _)| e.attribute(GML, "id").map(|id| (id, *e)))
        .collect();
    let registration = profile == Profile::DigitalBuildingRegistration;
    let conditional = if registration {
        &[][..]
    } else {
        TENDER_CONDITIONAL_FIELDS
    };
    let mut report = ProfileReport {
        profile: if registration {
            "TKGMCityGML 3.0.3 (local subset)"
        } else {
            "TKGM production guide v2.50 (architectural common subset)"
        }
        .into(),
        workflow: if registration {
            "digital_building_registration"
        } else {
            "city_model_tender_architectural"
        }
        .into(),
        acceptance_verified: false,
        buildings: 0,
        storeys: 0,
        independent_sections: 0,
        issues: vec![],
        truncated: false,
        unchecked_rules: [
            "all_code_lists",
            "naming_rules",
            "geometry_property_contents",
            "room_opening_installation_rules",
            "architectural_group_membership",
            "external_identity_values",
            "geometric_topology",
            "online_validation",
        ]
        .into_iter()
        .map(str::to_owned)
        .collect(),
        issue_limit: limits.max_objects.min(10_000) as usize,
    };
    report.unchecked_rules.extend(
        if registration {
            [
                "property_lot_type_ambiguity",
                "building_height_type_ambiguity",
            ]
        } else {
            [
                "maks_rules_by_tender_year",
                "sustainability_extensions_by_contract",
            ]
        }
        .into_iter()
        .map(str::to_owned),
    );
    let root_envelope = doc
        .root
        .elements()
        .any(|e| e.name.is(GML, "boundedBy") && e.elements().any(|c| c.name.is(GML, "Envelope")));
    for (index, &(e, _)) in nodes.iter().enumerate() {
        if e.name.is(GML, "Envelope") {
            if e.attribute("", "srsDimension") != Some("3") {
                report.issue("tkgm.envelope_dimension", index, Some("srsDimension"));
            }
            let srs = e.attribute("", "srsName").unwrap_or("");
            let code = srs
                .strip_prefix("EPSG:")
                .or_else(|| srs.strip_prefix("urn:ogc:def:crs:EPSG::"))
                .or_else(|| srs.strip_prefix("http://www.opengis.net/def/crs/EPSG/0/"));
            if !matches!(
                code,
                Some("5253" | "5254" | "5255" | "5256" | "5257" | "5258" | "5259")
            ) {
                report.issue("tkgm.crs", index, Some("srsName"));
            }
        }
        let building = e.name.is(BUILDING, "Building");
        let storey =
            e.name.is(GROUPS, "CityObjectGroup") && class(e, GROUPS).as_deref() == Some("Kat");
        let unit = e.name.is(GENERICS, "GenericCityObject")
            && class(e, GENERICS).as_deref() == Some("BagimsizBolum");
        if (e.name.is(GROUPS, "CityObjectGroup") && class(e, GROUPS).is_none())
            || (e.name.is(GENERICS, "GenericCityObject") && class(e, GENERICS).is_none())
        {
            report.issue("tkgm.missing_class", index, Some("class"));
        }
        if building || storey || unit {
            report.required(e, index, GML, "name");
            if e.attribute(GML, "id").is_none() {
                report.issue("tkgm.missing_id", index, Some("gml:id"));
            }
        }
        if building {
            report.buildings += 1;
            report.fields(e, index, BUILDING_FIELDS, conditional);
            // Only the registration guide has conflicting height types. The tender
            // architectural chapter explicitly specifies doubleAttribute.
            report.fields(
                e,
                index,
                &[(
                    "buildingHeight",
                    if registration { "" } else { "doubleAttribute" },
                )],
                &[],
            );
            if class(e, BUILDING).as_deref() != Some("MimariBina") {
                report.issue("tkgm.building_class", index, Some("class"));
            }
            for field in [
                "storeysAboveGround",
                "storeysBelowGround",
                "lod0FootPrint",
                "lod0RoofEdge",
                "lod1Solid",
                "lod2MultiSurface",
                "lod2TerrainIntersection",
                "boundedBy",
            ] {
                report.required(e, index, BUILDING, field);
            }
        }
        if storey {
            report.storeys += 1;
            report.fields(e, index, STOREY_FIELDS, &[]);
            for field in ["parent", "geometry"] {
                report.required(e, index, GROUPS, field);
            }
            if !e.elements().any(|c| c.name.is(GROUPS, "groupMember")) {
                // Common areas are Rooms with their own storeyObjectReference.
                // An explicitly empty unit count does not prove broken ownership;
                // require review instead of inventing membership from geometry.
                let zero_units = e.elements().any(|a| {
                    a.name.is(GENERICS, "intAttribute")
                        && a.attribute("", "name") == Some("independentSectionCount")
                        && a.elements().any(|v| {
                            v.name.is(GENERICS, "value") && v.text().trim().parse::<i64>() == Ok(0)
                        })
                });
                if zero_units {
                    report.finding(
                        "tkgm.empty_storey_membership",
                        Severity::Review,
                        index,
                        Some("groupMember"),
                    );
                } else {
                    report.issue("tkgm.missing_property", index, Some("groupMember"));
                }
            }
            for parent in e.elements().filter(|p| p.name.is(GROUPS, "parent")) {
                let target = parent
                    .attribute(XLINK, "href")
                    .and_then(|r| r.strip_prefix('#'))
                    .and_then(|id| ids.get(id));
                if !target.is_some_and(|t| t.name.is(BUILDING, "Building")) {
                    report.issue("tkgm.storey_parent", index, Some("parent"));
                }
            }
            for member in e.elements().filter(|p| p.name.is(GROUPS, "groupMember")) {
                let target = member
                    .attribute(XLINK, "href")
                    .and_then(|r| r.strip_prefix('#'))
                    .and_then(|id| ids.get(id));
                if !target.is_some_and(|t| {
                    (t.name.is(GENERICS, "GenericCityObject")
                        && class(t, GENERICS).as_deref() == Some("BagimsizBolum"))
                        || (t.name.is(BUILDING, "Room")
                            && class(t, BUILDING).as_deref() == Some("OrtakAlan"))
                }) {
                    report.issue("tkgm.storey_member", index, Some("groupMember"));
                }
            }
        }
        if unit {
            report.independent_sections += 1;
            report.fields(e, index, UNIT_FIELDS, conditional);
            report.required(e, index, GENERICS, "lod2Geometry");
        }
        if unit && registration {
            report.fields(e, index, REGISTRATION_UNIT_FIELDS, &[]);
            for (field, expected) in [
                ("geometrySuitability", 1006),
                ("integrationState", 1004),
                ("maksIntegrationState", 1003),
                ("takbisPropertyIdentityNumber", -1),
            ] {
                for a in e.elements().filter(|a| {
                    a.name.is(GENERICS, "intAttribute") && a.attribute("", "name") == Some(field)
                }) {
                    let value = a
                        .elements()
                        .find(|v| v.name.is(GENERICS, "value"))
                        .map(Element::text);
                    if value.as_deref().and_then(|v| v.trim().parse::<i64>().ok()) != Some(expected)
                    {
                        report.issue("tkgm.fixed_value", index, Some(field));
                    }
                }
            }
        }
        if e.name.is(GENERICS, "stringAttribute")
            && e.attribute("", "name") == Some("cadkit.geometry_status")
        {
            report.issue("tkgm.metadata_only_geometry", index, None);
        }
    }
    if report.buildings == 0 {
        report.issue("tkgm.missing_building", 0, None);
    }
    if !root_envelope {
        report.issue("tkgm.missing_envelope", 0, None);
    }
    Ok(report)
}