factor_temp.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const fs = require('fs');
  2. const turf = require('@turf/turf');
  3. const wkx = require('wkx');
  4. const Database = require('../db');
  5. const moment = require('moment');
  6. const uuid = require('uuid');
  7. const pool = new Database({
  8. host: '192.168.100.30',
  9. database: 'real3d',
  10. user: 'postgres',
  11. password: 'postgis',
  12. port: 5432,
  13. });
  14. async function importFactor(inFilePath, landTypeCode) {
  15. // 连接到数据库,设置模式
  16. await pool.connect();
  17. await pool.setSchema('base');
  18. try {
  19. // 同步读取GeoJSON文件
  20. const data = fs.readFileSync(inFilePath, 'utf8');
  21. // 清空表 base.t_fzss_fzxz_factor_temp
  22. // const deleteSql = `delete from t_fzss_fzxz_factor_temp`;
  23. // await pool.query(deleteSql);
  24. // 解析GeoJSON数据
  25. const dataInfos = JSON.parse(data);
  26. importItems(dataInfos, landTypeCode);
  27. console.log("插入模板成功" + inFilePath)
  28. } catch (err) {
  29. console.error('操作GeoJSON文件时出错:', err);
  30. }
  31. }
  32. async function importItems(insertItems, landTypeCode) {
  33. for (let index = 0; index < insertItems.length; index++) {
  34. const element = insertItems[index];
  35. // 生成uuid并且去掉下划线
  36. const id = uuid.v4().replace(/-/g, "");
  37. const insertObj = {
  38. id: id,
  39. landTypeCode: landTypeCode,
  40. factorId: element.id,
  41. factorBsm: element.bsm,
  42. factorName: element.name,
  43. order_index: index,
  44. condition_info: JSON.stringify(element.condition_info),
  45. system: true
  46. }
  47. // 生成数据库的插入语句
  48. const insertSql = `insert into t_fzss_fzxz_factor_temp (id,land_type_code,factor_id,factor_bsm,factor_name,order_index,condition_info,system)
  49. values('${insertObj.id}','${insertObj.landTypeCode}','${insertObj.factorId}','${insertObj.factorBsm}','${insertObj.factorName}',${insertObj.order_index},'${insertObj.condition_info}',${insertObj.system})`;
  50. console.log(insertSql);
  51. const res = await pool.query(insertSql);
  52. if (element.children && element.children.length > 0) {
  53. await importItems(element.children, id);
  54. }
  55. }
  56. }
  57. importFactor("./data/schedule/factor_temp/factor_sy.json", "09")