// 管井适配dem const Database = require('../db'); const turf = require('@turf/turf'); const fs = require('fs'); const wkt = require('wkt'); const pool = new Database({ host: '192.168.100.30', database: 'real3d', user: 'postgres', password: 'postgis', port: 5432, }); const inputPointPath = "./data/guanxian/雨水/符号字段/雨水_P.geojson" const inputLinePath = "./data/guanxian/雨水/符号字段/雨水_L.geojson" // 获取dem的矢量范围 async function getDemExtent() { await pool.connect(); await pool.setSchema('base'); const sql = `SELECT PUBLIC.st_asgeojson( PUBLIC.st_transform( PUBLIC.ST_SetSRID( PUBLIC.ST_Extent( PUBLIC.ST_Envelope(rast) ), 3857 ), 4326 ) ) AS extent FROM vector."RASTER_DEM_BP"; `; // console.log(sql); const result = await pool.query(sql); return result.rows[0].extent; } // 获取给定点的高程值 async function getDemHeight(wktStr) { const transformedGeom = `PUBLIC.ST_Transform(PUBLIC.ST_SetSRID(PUBLIC.ST_GeomFromText($1), 4326), 3857)`; const sql = ` SELECT PUBLIC.ST_Value(rast, ${transformedGeom}) AS height FROM vector."RASTER_DEM_BP" WHERE PUBLIC.ST_Intersects(rast, ${transformedGeom}) `; // console.log(`Executing SQL: ${sql} with parameter: ${wktStr}`); const result = await pool.query(sql, [wktStr]); return result.rows[0].height; } // 重新设置井盖的高度 async function setPointHeight(geoJson, filePath) { const polygon = JSON.parse(geoJson); const data = fs.readFileSync(filePath, 'utf8'); // 解析GeoJSON数据 const collect = JSON.parse(data); for (let i = 0; i < collect.features.length; i++) { let pointFeature = collect.features[i] const point = turf.point([pointFeature.geometry.coordinates[0], pointFeature.geometry.coordinates[1]]); const isContain = turf.booleanContains(polygon, point); if (isContain) { const pointWkt = wkt.stringify(point); const height = await getDemHeight(pointWkt); if (height !== null) { console.log("包含:" + pointWkt); console.log(height); collect.features[i].geometry.coordinates[2] = height + 0.01; } } } filePath = filePath.replace("符号字段", "调整高程"); fs.writeFileSync(filePath, JSON.stringify(collect, null, 2)); console.log('GeoJSON文件已成功保存:' + filePath); } // 重新设置管线的高程 async function setLineHeight(geoJson, filePath) { const polygon = JSON.parse(geoJson); const data = fs.readFileSync(filePath, 'utf8'); // 解析GeoJSON数据 const collect = JSON.parse(data); // 遍历所有的线 for (let i = 0; i < collect.features.length; i++) { let lineFeature = collect.features[i] // 遍历所有的点 for (let index = 0; index < lineFeature.geometry.coordinates.length; index++) { const points = lineFeature.geometry.coordinates[index]; for (let j = 0; j < points.length; j++) { let point = points[j]; point = turf.point([point[0], point[1]]); const isContain = turf.booleanContains(polygon, point); if (isContain) { const pointWkt = wkt.stringify(point); const height = await getDemHeight(pointWkt); if (height !== null) { console.log("包含:" + pointWkt); console.log(height); let z = collect.features[i].geometry.coordinates[index][j][2]; if ((z > height) || Math.abs(z - height) < 0.1) { collect.features[i].geometry.coordinates[index][j][2] = height - 0.2; console.log("调整高程:" + height); } } } } } } filePath = filePath.replace("符号字段", "调整高程"); fs.writeFileSync(filePath, JSON.stringify(collect, null, 2)); console.log('GeoJSON文件已成功保存:' + filePath); } async function adaptDem(pointFilePath, inputLinePath) { // 获取抱坡范围 const bpGeom = await getDemExtent(); // console.log(bpGeom); // 设置井盖高度 // await setPointHeight(bpGeom, pointFilePath); await setLineHeight(bpGeom, inputLinePath); pool.disconnect(); } adaptDem(inputPointPath, inputLinePath);