12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- const fs = require('fs');
- const turf = require('@turf/turf');
- const wkx = require('wkx');
- const Database = require('./db');
- const moment = require('moment');
- const uuid = require('uuid');
- const pool = new Database({
- host: '192.168.100.30',
- database: 'real3d',
- user: 'postgres',
- password: 'postgis',
- port: 5432,
- });
- async function importFactor(inFilePath) {
- // 连接到数据库,设置模式
- await pool.connect();
- await pool.setSchema('base');
- try {
- // 同步读取GeoJSON文件
- const data = fs.readFileSync(inFilePath, 'utf8');
- // 清空表 base.t_fzss_fzxz_factor
- const deleteSql = `delete from t_fzss_fzxz_factor`;
- await pool.query(deleteSql);
- // 解析GeoJSON数据
- const dataInfos = JSON.parse(data);
- importItems(dataInfos, "");
- console.log("插入因子成功")
- } catch (err) {
- console.error('操作GeoJSON文件时出错:', err);
- }
- }
- async function importItems(insertItems, parentId) {
- for (let index = 0; index < insertItems.length; index++) {
- const element = insertItems[index];
- // 生成uuid并且去掉下划线
- // const id = uuid.v4().replace(/-/g, "");
- const id = element.id;
- const insertObj = {
- id: id,
- bsm: element.bsm,
- name: element.name,
- level: element.level,
- parent_id: parentId,
- order_index: index,
- status: 1,
- condition_info: JSON.stringify(element.condition_info),
- }
- if ((insertObj.bsm === null || insertObj.bsm === ``) && element.children.length == 0) {
- continue;
- }
- // 生成数据库的插入语句
- const insertSql = `insert into t_fzss_fzxz_factor (id,bsm,name,level,parent_id,order_index,status,condition_info) values('${insertObj.id}','${insertObj.bsm}','${insertObj.name}',${insertObj.level},'${insertObj.parent_id}',${insertObj.order_index},${insertObj.status},'${insertObj.condition_info}')`;
- console.log(insertSql);
- const res = await pool.query(insertSql);
- if (element.children && element.children.length > 0) {
- await importItems(element.children, id);
- }
- }
- }
- importFactor("./data/schedule/factor_info.json")
|