@csi-foxbyte/mesh-dem-to-terrain
Version:
A Node.js library that converts DEM meshes (DXF, GeoJSON, etc.) into Cesium Terrain Tiles (quantized-mesh or heightmap) with flexible zoom-level control and multi-threading support.
2 lines • 2.54 kB
JavaScript
import {createWriteStream}from'fs';import {writeFile}from'fs/promises';import X from'gdal-async';import {join,basename}from'path';import k from'proj4';import G from'rbush';import {Box3,Vector3}from'three';import {triangulate3DPolygon}from'./helpers.js';import {yauzlOpen,processZip}from'./zip.js';async function U(B,f,I,L){function y(e,t){return e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2]}function O(e){switch(e.type){case "LineString":const t=e.coordinates[0],o=e.coordinates.slice(-1)[0];return y(t,o)&&e.coordinates.pop(),triangulate3DPolygon([e.coordinates]);case "Polygon":for(const i of e.coordinates){const n=i[0],r=i.slice(-1)[0];y(n,r)&&i.pop();}return triangulate3DPolygon(e.coordinates);default:return console.error(`Unsupported GeoJSOn type found ${e.type}`),[]}}async function V(e,t,o,i){const n=createWriteStream(e);n.write(new Float64Array([t.x,t.y,t.z]))||await new Promise(r=>n.once("drain",r)),n.write(new Int32Array([o.length]))||await new Promise(r=>n.once("drain",r)),n.write(o.length<=2**16?new Uint16Array(o):new Uint32Array(o))||await new Promise(r=>n.once("drain",r)),n.write(new Int32Array([i.length]))||await new Promise(r=>n.once("drain",r)),n.write(new Float32Array(i))||await new Promise(r=>n.once("drain",r)),n.end(),await new Promise((r,d)=>{n.on("finish",()=>r()),n.on("error",u=>d(u));});}const s=new Box3;let w=0;const g=new G,x=await yauzlOpen(B,{lazyEntries:true});let Z=x.entryCount;await processZip(x,[".dxf"],async(e,t)=>{try{const o=join(f,basename(e)+".bin"),i=X.open(t),n=i.srs?.toProj4()??L,r="+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs";if(!n)throw new Error("No srs found, please provide a srcProj4 string.");const d=k(n,r);let u=0,P=[],b=[],c=null;const a=new Box3;for(const _ of i.layers)_.features.forEach(E=>{const h=E.getGeometry().toObject(),S=O(h),l=h.coordinates.map(m=>d.forward(m));if(!S)return;c||(c=new Vector3(l[0][0],l[0][1],l[0][2]));const z=new Box3;z.setFromArray(l.flat()),a.union(z),P.push(...S.map(m=>m+u)),b.push(...l.flatMap(m=>[m[0]-c.x,m[1]-c.y,m[2]-c.z])),u+=l.length;});s.union(a),g.insert({minX:a.min.x,minY:a.min.y,minZ:a.min.z,maxX:a.max.x,maxY:a.max.y,maxZ:a.max.z,path:o}),await V(o,c,P,b),await writeFile(join(f,"metadata.json"),JSON.stringify({minX:s.min.x,minY:s.min.y,minZ:s.min.z,maxX:s.max.x,maxY:s.max.y,maxZ:s.max.z,tiles:g.toJSON()})),w++,I(w/Z);}catch(o){console.error(o);}});}export{U as default};//# sourceMappingURL=preprocess.js.map
//# sourceMappingURL=preprocess.js.map