adaptDem.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/雨水/符号字段/雨水_P.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. // 获取给定点的高程值
  41. async function getDemHeight(wktStr) {
  42. const transformedGeom = `PUBLIC.ST_Transform(PUBLIC.ST_SetSRID(PUBLIC.ST_GeomFromText($1), 4326), 3857)`;
  43. const sql = `
  44. SELECT PUBLIC.ST_Value(rast, ${transformedGeom}) AS height
  45. FROM vector."RASTER_DEM_BP"
  46. WHERE PUBLIC.ST_Intersects(rast, ${transformedGeom})
  47. `;
  48. // console.log(`Executing SQL: ${sql} with parameter: ${wktStr}`);
  49. const result = await pool.query(sql, [wktStr]);
  50. return result.rows[0].height;
  51. }
  52. // 重新设置井盖的高度
  53. async function setPointHeight(geoJson, filePath) {
  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 polygon = JSON.parse(geoJson);
  60. const point = turf.point([pointFeature.geometry.coordinates[0], pointFeature.geometry.coordinates[1]]);
  61. const isContain = turf.booleanContains(polygon, point);
  62. if (isContain) {
  63. const pointWkt = wkt.stringify(point);
  64. const height = await getDemHeight(pointWkt);
  65. if (height !== null) {
  66. console.log("包含:" + pointWkt);
  67. console.log(height);
  68. }
  69. } else {
  70. console.log("不包含");
  71. }
  72. }
  73. // const sql =
  74. // `UPDATE base."t_gj" SET "z" = 0 WHERE "z" < 0;`;
  75. // console.log(sql);
  76. // const result = await pool.query(sql);
  77. // return result.rows[0].extent;
  78. }
  79. async function adaptDem(pointFilePath) {
  80. // 获取抱坡范围
  81. const bpGeom = await getDemExtent();
  82. // console.log(bpGeom);
  83. // 设置井盖高度
  84. await setPointHeight(bpGeom, pointFilePath);
  85. pool.disconnect();
  86. }
  87. adaptDem(inputPointPath);