adaptDem.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // 管井适配dem
  2. const Database = require('../db');
  3. const turf = require('@turf/turf');
  4. const fs = require('fs');
  5. const wkt = require('wkt');
  6. const pool = new Database({
  7. host: '192.168.100.30',
  8. database: 'real3d',
  9. user: 'postgres',
  10. password: 'postgis',
  11. port: 5432,
  12. });
  13. const inputPointPath = "./data/guanxian/雨水/符号字段/雨水_P.geojson"
  14. const inputLinePath = "./data/guanxian/雨水/符号字段/雨水_L.geojson"
  15. // 获取dem的矢量范围
  16. async function getDemExtent() {
  17. await pool.connect();
  18. await pool.setSchema('base');
  19. const sql =
  20. `SELECT
  21. PUBLIC.st_asgeojson(
  22. PUBLIC.st_transform(
  23. PUBLIC.ST_SetSRID(
  24. PUBLIC.ST_Extent(
  25. PUBLIC.ST_Envelope(rast)
  26. ),
  27. 3857
  28. ),
  29. 4326
  30. )
  31. ) AS extent
  32. FROM
  33. vector."RASTER_DEM_BP";
  34. `;
  35. // console.log(sql);
  36. const result = await pool.query(sql);
  37. return result.rows[0].extent;
  38. }
  39. // 获取给定点的高程值
  40. async function getDemHeight(wktStr) {
  41. const transformedGeom = `PUBLIC.ST_Transform(PUBLIC.ST_SetSRID(PUBLIC.ST_GeomFromText($1), 4326), 3857)`;
  42. const sql = `
  43. SELECT PUBLIC.ST_Value(rast, ${transformedGeom}) AS height
  44. FROM vector."RASTER_DEM_BP"
  45. WHERE PUBLIC.ST_Intersects(rast, ${transformedGeom})
  46. `;
  47. // console.log(`Executing SQL: ${sql} with parameter: ${wktStr}`);
  48. const result = await pool.query(sql, [wktStr]);
  49. return result.rows[0].height;
  50. }
  51. // 重新设置井盖的高度
  52. async function setPointHeight(geoJson, filePath) {
  53. const polygon = JSON.parse(geoJson);
  54. const data = fs.readFileSync(filePath, 'utf8');
  55. // 解析GeoJSON数据
  56. const collect = JSON.parse(data);
  57. for (let i = 0; i < collect.features.length; i++) {
  58. let pointFeature = collect.features[i]
  59. const point = turf.point([pointFeature.geometry.coordinates[0], pointFeature.geometry.coordinates[1]]);
  60. const isContain = turf.booleanContains(polygon, point);
  61. if (isContain) {
  62. const pointWkt = wkt.stringify(point);
  63. const height = await getDemHeight(pointWkt);
  64. if (height !== null) {
  65. console.log("包含:" + pointWkt);
  66. console.log(height);
  67. collect.features[i].geometry.coordinates[2] = height + 0.01;
  68. }
  69. }
  70. }
  71. filePath = filePath.replace("符号字段", "调整高程");
  72. fs.writeFileSync(filePath, JSON.stringify(collect, null, 2));
  73. console.log('GeoJSON文件已成功保存:' + filePath);
  74. }
  75. // 重新设置管线的高程
  76. async function setLineHeight(geoJson, filePath) {
  77. const polygon = JSON.parse(geoJson);
  78. const data = fs.readFileSync(filePath, 'utf8');
  79. // 解析GeoJSON数据
  80. const collect = JSON.parse(data);
  81. // 遍历所有的线
  82. for (let i = 0; i < collect.features.length; i++) {
  83. let lineFeature = collect.features[i]
  84. // 遍历所有的点
  85. for (let index = 0; index < lineFeature.geometry.coordinates.length; index++) {
  86. const points = lineFeature.geometry.coordinates[index];
  87. for (let j = 0; j < points.length; j++) {
  88. let point = points[j];
  89. point = turf.point([point[0], point[1]]);
  90. const isContain = turf.booleanContains(polygon, point);
  91. if (isContain) {
  92. const pointWkt = wkt.stringify(point);
  93. const height = await getDemHeight(pointWkt);
  94. if (height !== null) {
  95. console.log("包含:" + pointWkt);
  96. console.log(height);
  97. let z = collect.features[i].geometry.coordinates[index][j][2];
  98. if ((z > height) || Math.abs(z - height) < 0.1) {
  99. collect.features[i].geometry.coordinates[index][j][2] = height - 0.2;
  100. console.log("调整高程:" + height);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. filePath = filePath.replace("符号字段", "调整高程");
  108. fs.writeFileSync(filePath, JSON.stringify(collect, null, 2));
  109. console.log('GeoJSON文件已成功保存:' + filePath);
  110. }
  111. async function adaptDem(pointFilePath, inputLinePath) {
  112. // 获取抱坡范围
  113. const bpGeom = await getDemExtent();
  114. // console.log(bpGeom);
  115. // 设置井盖高度
  116. // await setPointHeight(bpGeom, pointFilePath);
  117. await setLineHeight(bpGeom, inputLinePath);
  118. pool.disconnect();
  119. }
  120. adaptDem(inputPointPath, inputLinePath);