|
| 1 | +import { buildingIdentityFromProperties, normalizeBuildingIdentity, type BuildingIdentityField } from "./buildingIdentity"; |
| 2 | +import type { SpatialGeometry } from "./model"; |
| 3 | +import type { VworldWfsFeature } from "./vworld"; |
| 4 | + |
| 5 | +export type BuildingFootprintDuplicateGroup = { |
| 6 | + key: string; |
| 7 | + featureIds: string[]; |
| 8 | + count: number; |
| 9 | +}; |
| 10 | + |
| 11 | +export type BuildingFootprintQuality = { |
| 12 | + totalFeatures: number; |
| 13 | + polygonFeatures: number; |
| 14 | + usablePolygonFeatures: number; |
| 15 | + invalidGeometryCount: number; |
| 16 | + missingIdentityCount: number; |
| 17 | + duplicateIdentityGroups: BuildingFootprintDuplicateGroup[]; |
| 18 | + duplicateIdentityFeatureCount: number; |
| 19 | + duplicateGeometryCount: number; |
| 20 | + propertyFieldNames: string[]; |
| 21 | + geometryTypes: string[]; |
| 22 | +}; |
| 23 | + |
| 24 | +const identityFields: BuildingIdentityField[] = ["buildingManagementNo", "bldrgstPk", "ufid", "pnu", "gid", "featureId"]; |
| 25 | + |
| 26 | +function coordinatePair(value: unknown): value is [number, number] { |
| 27 | + return Array.isArray(value) && value.length >= 2 && Number.isFinite(Number(value[0])) && Number.isFinite(Number(value[1])); |
| 28 | +} |
| 29 | + |
| 30 | +function validRing(value: unknown) { |
| 31 | + if (!Array.isArray(value) || value.length < 3) return false; |
| 32 | + const points = value.filter(coordinatePair); |
| 33 | + return points.length >= 3 && new Set(points.map(point => `${Number(point[0]).toFixed(7)},${Number(point[1]).toFixed(7)}`)).size >= 3; |
| 34 | +} |
| 35 | + |
| 36 | +export function isUsableBuildingFootprint(geometry?: SpatialGeometry) { |
| 37 | + if (!geometry || !Array.isArray(geometry.coordinates)) return false; |
| 38 | + if (geometry.type === "Polygon") return validRing(geometry.coordinates[0]); |
| 39 | + if (geometry.type === "MultiPolygon") return geometry.coordinates.some(polygon => Array.isArray(polygon) && validRing(polygon[0])); |
| 40 | + return false; |
| 41 | +} |
| 42 | + |
| 43 | +function identityKey(feature: VworldWfsFeature) { |
| 44 | + const identity = buildingIdentityFromProperties(feature.properties, feature.id); |
| 45 | + for (const field of identityFields) { |
| 46 | + const value = normalizeBuildingIdentity(identity[field]); |
| 47 | + if (value) return `${field}:${value}`; |
| 48 | + } |
| 49 | + return ""; |
| 50 | +} |
| 51 | + |
| 52 | +function featureId(feature: VworldWfsFeature, index: number) { |
| 53 | + return feature.id?.trim() || `feature-${index + 1}`; |
| 54 | +} |
| 55 | + |
| 56 | +function geometryKey(geometry?: SpatialGeometry) { |
| 57 | + if (!geometry || !Array.isArray(geometry.coordinates)) return ""; |
| 58 | + const round = (value: unknown): unknown => Array.isArray(value) ? value.map(round) : typeof value === "number" ? Number(value.toFixed(7)) : value; |
| 59 | + return `${geometry.type}:${JSON.stringify(round(geometry.coordinates))}`; |
| 60 | +} |
| 61 | + |
| 62 | +export function auditBuildingFootprints(features: VworldWfsFeature[]): BuildingFootprintQuality { |
| 63 | + const duplicateIdentityMap = new Map<string, string[]>(); |
| 64 | + const duplicateGeometryMap = new Map<string, string[]>(); |
| 65 | + const fields = new Set<string>(); |
| 66 | + const geometryTypes = new Set<string>(); |
| 67 | + let polygonFeatures = 0; |
| 68 | + let usablePolygonFeatures = 0; |
| 69 | + let invalidGeometryCount = 0; |
| 70 | + let missingIdentityCount = 0; |
| 71 | + features.forEach((feature, index) => { |
| 72 | + Object.keys(feature.properties).forEach(field => fields.add(field)); |
| 73 | + if (feature.geometry?.type) geometryTypes.add(feature.geometry.type); |
| 74 | + const isPolygon = feature.geometry?.type === "Polygon" || feature.geometry?.type === "MultiPolygon"; |
| 75 | + if (isPolygon) polygonFeatures += 1; |
| 76 | + if (isUsableBuildingFootprint(feature.geometry)) usablePolygonFeatures += 1; |
| 77 | + else invalidGeometryCount += 1; |
| 78 | + const idKey = identityKey(feature); |
| 79 | + if (!idKey) missingIdentityCount += 1; |
| 80 | + else duplicateIdentityMap.set(idKey, [...(duplicateIdentityMap.get(idKey) ?? []), featureId(feature, index)]); |
| 81 | + const shapeKey = geometryKey(feature.geometry); |
| 82 | + if (shapeKey) duplicateGeometryMap.set(shapeKey, [...(duplicateGeometryMap.get(shapeKey) ?? []), featureId(feature, index)]); |
| 83 | + }); |
| 84 | + const duplicateIdentityGroups = Array.from(duplicateIdentityMap.entries()).filter(([, ids]) => ids.length > 1).map(([key, ids]) => ({ key, featureIds: ids, count: ids.length })); |
| 85 | + const duplicateGeometryGroups = Array.from(duplicateGeometryMap.values()).filter(ids => ids.length > 1); |
| 86 | + return { totalFeatures: features.length, polygonFeatures, usablePolygonFeatures, invalidGeometryCount, missingIdentityCount, duplicateIdentityGroups, duplicateIdentityFeatureCount: duplicateIdentityGroups.reduce((sum, group) => sum + group.count, 0), duplicateGeometryCount: duplicateGeometryGroups.reduce((sum, ids) => sum + ids.length, 0), propertyFieldNames: Array.from(fields).sort(), geometryTypes: Array.from(geometryTypes).sort() }; |
| 87 | +} |
| 88 | + |
| 89 | +export function usableBuildingFootprintFeatures(features: VworldWfsFeature[]) { |
| 90 | + return features.filter(feature => isUsableBuildingFootprint(feature.geometry)); |
| 91 | +} |
0 commit comments