All files / datasources/local/cad loadLocalCadMetadata.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 5/5
100% Lines 20/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      2x 4x 4x 1x     3x 3x 4x 4x 4x     3x   4x                   3x       4x 4x 4x       4x 4x 24x       24x   4x    
/*!
 * Copyright 2020 Cognite AS
 */
 
import { RevealSector3D, BoundingBox3D, Versioned3DFile } from '@cognite/sdk';
 
export type LocalCadMetadataResponse = {
  readonly SectorId: number;
  readonly FileId: number;
  readonly ParentId: { Value: number } | null;
  readonly BoundingBox: {
    readonly Min: { X: number; Y: number; Z: number };
    readonly Max: { X: number; Y: number; Z: number };
  };
  readonly Depth: number;
  readonly Path: string;
  readonly Files: { [key: string]: number };
};
 
export async function loadLocalCadMetadata(cadMetadataUrl: string): Promise<RevealSector3D[]> {
  const response = await fetch(cadMetadataUrl);
  if (!response.ok) {
    throw new Error(`Could not fetch ${cadMetadataUrl}, got ${response.status}`);
  }
 
  const content = await response.text();
  const sectors: LocalCadMetadataResponse[] = [];
  for (const chunk of content.split('\n').filter(x => x.trim() !== '')) {
    const sector: LocalCadMetadataResponse = JSON.parse(chunk);
    sectors.push(sector);
  }
 
  const sdkSectors = sectors.map(
    (s): RevealSector3D => {
      return {
        id: s.SectorId,
        parentId: s.ParentId ? s.ParentId.Value : -1,
        path: s.Path,
        depth: s.Depth,
        threedFiles: transformFiles(s),
        boundingBox: transformBbox(s)
      };
    }
  );
  return sdkSectors;
}
 
function transformBbox(sector: LocalCadMetadataResponse): BoundingBox3D {
  const rMin = sector.BoundingBox.Min;
  const rMax = sector.BoundingBox.Max;
  return { min: [rMin.X, rMin.Y, rMin.Z], max: [rMax.X, rMax.Y, rMax.Z] };
}
 
function transformFiles(sector: LocalCadMetadataResponse): Versioned3DFile[] {
  const files: Versioned3DFile[] = [];
  for (const [key, value] of Object.entries(sector.Files)) {
    const file = {
      version: parseInt(key, 10),
      fileId: value
    };
    files.push(file);
  }
  return files;
}