prodmaps/src/back_.js

367 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import fs from "fs";
import axios from "axios";
import SvgMap from "./svgmap/SvgMap.js";
import SvgMapBuilder from "./svgmap/SvgMapBuilder.js";
import CoordSystem from "./libs/CoordSystem.js";
import SvgNode from "./svgmap/SvgNode.js";
import SvgNodes from "./svgmap/SvgNodes.js";
import Transfrom from "./libs/Transform.js";
import BBox from "./libs/bbox.js";
import sql_pi from "./services/sql/prod_inj.js";
// import SvgNodes from "./svgmap/SvgNodes.js";
// import math_lib from './libs/math.js'
const back_url = "http://localhost:4000";
const path = "../../data/prodmaps";
// let file = fs.readFileSync('../data/2022-03-16/49_Кар тек и сум отб 7 обост.svg')
// let file = fs.readFileSync('../data/2022-03-16/49_Кар тек и сум отб 7 обост-sm.svg')
// let file = fs.readFileSync('../data/2022-03-16/49_Кар тек и сум отб 7 обост-inch.svg')
// let file = fs.readFileSync(`${path}/49_Кар тек и сум отб 7 обост-mm.svg`)
// let file = fs.readFileSync(`${path}/49_Кар тек и сум отб 7 обост.svg`);
// let file = fs.readFileSync('../data/2022-03-16/49_Кар тек и сум отб 7 обост-px.svg')
const query = (sql) => axios.post(`${back_url}/proxy/sqlite`, { query: sql }).then((x) => x.data.data);
let styles = JSON.parse(fs.readFileSync("./moc/styles.json").toString());
const all_settings = {
pt: {
tons_in_cm2: 10000,
ppu: 100, // 100
styles,
// Масштаб 1мм карты / 1мм реальный (1000мм в 1м)
map_scale: (1 / 10000) * 1000,
},
it: {
tons_in_cm2: 100000,
ppu: 100, // 100
styles,
// Масштаб 1мм карты / 1мм реальный (1000мм в 1м)
map_scale: (1 / 10000) * 1000,
},
pr: {
tons_in_cm2: 5,
ppu: 100,
styles,
// Масштаб 1мм карты / 1мм реальный (1000мм в 1м)
map_scale: (1 / 10000) * 1000,
},
ir: {
tons_in_cm2: 50,
ppu: 100,
styles,
// Масштаб 1мм карты / 1мм реальный (1000мм в 1м)
map_scale: (1 / 10000) * 1000,
},
};
// all_settings.styles = SvgMapBuilder.update_styles(all_settings.styles, all_settings.ppu)
let parser = new SvgMap();
async function start() {
await parser.parse(file.toString());
let sc = parser.get_map_scale();
console.log("scale is", sc);
let layers = parser.get_layers();
console.log(layers.map((x) => x.properties.id));
let name = "скв";
let layer = layers.filter((x) => x.properties.id == name)[0];
// console.log(layer)
// Получить координаты якоря
let anchor = parser.extract_anchor(layer);
console.log("anchor", anchor);
const query = `SELECT well, whx, why FROM wells WHERE well='${anchor.w1.name}' OR well='${anchor.w2.name}'`;
let wells = await axios.post(`${back_url}/proxy/sqlite`, { query }).then((x) => x.data.data);
console.log(wells);
let ww1 = wells.filter((x) => x.well == anchor.w1.name)[0];
let ww2 = wells.filter((x) => x.well == anchor.w2.name)[0];
if (!ww1) return ui.modal.show(`Скважины ${anchor.w1.name} не найдено в базе.`, ["ok"]);
if (!ww2) return ui.modal.show(`Скважины ${anchor.w2.name} не найдено в базе.`, ["ok"]);
// function cs(x0, y0, x1, y1) {
// return { x0, y0, x1, y1 };
// }
// function build_tr(cs1, cs2) {
// let dx1 = cs1.x1 - cs1.x0
// let dy1 = cs1.y1 - cs1.y0
// let dx2 = cs2.x1 - cs2.x0
// let dy2 = cs2.y1 - cs2.y0
// const kx12 = dx2 / dx1
// const ky12 = dy2 / dy1;
// const kx21 = dx1 / dx2
// const ky21 = dy1 / dy2;
// let tr = {
// tr12: {
// trx(x) {
// return (x - cs1.x0) * kx12 + cs2.x0;
// },
// try(y) {
// return (y - cs1.y0) * ky12 + cs2.y0;
// },
// },
// tr21: {
// trx(x) {
// return (x - cs2.x0) * kx21 + cs1.x0;
// },
// try(y) {
// return (y - cs2.y0) * ky21 + cs1.y0;
// },
// },
// };
// return tr;
// }
// Функция перевода координат из глобальных в локальные.
const cs1 = new CoordSystem(anchor.w1.x, anchor.w1.y, anchor.w2.x, anchor.w2.y);
const cs2 = new CoordSystem(ww1.whx, ww1.why, ww2.whx, ww2.why);
// let tr = Transfrom.fromCoordSyses(cs2, cs1);
// let tr = Transfrom.fromCoordSyses(cs2, cs1);
// console.log('tr is:', tr);
// console.log(wells)
const DATE = "2023-01-01";
const query_all = `SELECT
wells.well, wells.whx, wells.why,
MAX(production_injections.date) as date
,SUM(production_injections.woptm) as wopt
,SUM(production_injections.wwptm) as wwpt
,SUM(production_injections.wsgptv) as wgpt
,SUM(production_injections.wwitv) as wwit
,SUM(production_injections.wwptm)+SUM(production_injections.woptm) as wlpt
,SUM(production_injections.wwptm)/(SUM(production_injections.wwptm)+SUM(production_injections.woptm)) as wlf
FROM
wells, production_injections
WHERE
wells.well=production_injections.well
AND production_injections.date<='${DATE}'
GROUP BY
wells.well
`; // WHERE well='${anchor.w1.name}'`;
// Получим максимально удаленные скважины не лежащие вдоль оси.
let all_wells = await axios.post(`${back_url}/proxy/sqlite`, { query: query_all }).then((x) => x.data.data);
let bbox = BBox.from_array(all_wells.map((w) => ({ x: w.whx, y: w.why })));
console.log("BBOX", bbox);
console.log("BBOX", bbox.toLTWH());
console.log("BBOX", bbox.toLTRB());
tr = Transfrom.fromCoordSyses();
let mapped_wells = all_wells.map((x) => ({ ...x, lx: tr.trx(x.whx), ly: tr.try(x.why) }));
// const tons_in_cm2 = 10000
// function t2r(tons){
// // tonns/mm2 = tonns/cm2 / 100. S(mm)=Pi*r*r=tons/tons_in_cm2*100. r = sqrt(tons/tons_in_cm2*100 / PI)
// return Math.sqrt(tons / tons_in_cm2 * 100 / Math.PI)
// }
// console.log(mapped_wells.filter(x => x.well == '213').map(x => ({...x, ttt: t2r(x.wlpt)})))
// console.log(math_lib.make_ranges(vals))
// svg.append(mapped_wells.map((x) => SvgNodes.ring_sector(1.5 * sc.k, 6 * sc.k, 0, 120).move(x.lx, x.ly).add_style(styles.gpt)));
const settings = {
tons_in_cm2: 10000,
styles,
};
{
let ti_layer = parser.build_tp_layer(mapped_wells, settings);
let svg = SvgNodes.svg()
.set_attr("width", "1000px")
.set_attr("height", "1000px")
.set_attr("viewBox", "0 0 1000 1000");
// let defs = new SvgNode('defs')
// defs.append(ti_layer.defs)
// console.log(ti_layer.defs.tag)
svg.append(ti_layer.defs);
svg.append(ti_layer.svg);
fs.writeFileSync(`${path}/out.svg`, svg.render());
}
// let ti_layer = parser.build_tp_layer(mapped_wells, settings)
// let data = file.toString()
// .replace('</defs>', ti_layer.defs.render() + '</defs>')
// .replace('</svg>', ti_layer.svg.render() + '</svg>')
// fs.writeFileSync("../tmp/out_prod.svg", data) //svg.render());
// let ti_layer_inj = parser.build_ti_layer(mapped_wells, settings)
// let data2 = file.toString()
// .replace('</defs>', ti_layer_inj.defs.render() + '</defs>')
// .replace('</svg>', ti_layer_inj.svg.render() + '</svg>')
// fs.writeFileSync("../tmp/out_inj.svg", data2) //svg.render());
}
class SvgSaver {
constructor(settings, bbox) {
// Функция перевода координат из глобальных в локальные.
const cs1 = new CoordSystem(bbox.l, bbox.t, bbox.r, bbox.b);
const cs_mm = cs1.clone().flipy().moveto(0, 0).scale(settings.map_scale);
const cs_ppu = cs_mm.clone().scale(settings.ppu);
const csw = Math.abs(cs_mm.x1 - cs_mm.x0);
const csh = Math.abs(cs_mm.y1 - cs_mm.y0);
const bbox_ppu = BBox.fromLTRB(cs_ppu.x0, cs_ppu.y0, cs_ppu.x1, cs_ppu.y1);
this.svg = SvgNodes.svg()
.set_attr("width", csw + "mm")
.set_attr("height", csh + "mm")
.set_attr("viewBox", `${bbox_ppu.l} ${bbox_ppu.t} ${bbox_ppu.w()} ${bbox_ppu.h()}`);
this.tr = Transfrom.fromCoordSyses(cs1, cs_ppu);
settings.styles = SvgMapBuilder.update_styles(settings.styles, settings.ppu);
}
append_defs(defs) {
this.svg.append(defs);
}
append_svg(svg) {
this.svg.append(svg);
}
append_layer(layer) {
this.append_defs(layer.defs);
this.append_svg(layer.svg);
}
save(filename) {
fs.writeFileSync(filename, this.svg.render());
}
}
function build_bbox(data){
let bbox = BBox.from_array(data);
if (bbox.w() == 0 || bbox.h() == 0) {
bbox.r += 1000;
bbox.b += 1000;
}
return bbox
}
async function build_map_pt(devobj) {
let wells = await query(sql_pi.totals(devobj));
wells = wells.filter((x) => x.wopt > 0);
if (!wells.length) return
let bbox = build_bbox(wells.map(w => ({ x: w.whx, y: w.why })));
const saver = new SvgSaver(all_settings.pt, bbox);
let mapped_wells = wells.map((x) => ({ ...x, lx: saver.tr.trx(x.whx), ly: saver.tr.try(x.why) }));
let tp_layer = SvgMapBuilder.build_tp_layer(mapped_wells, all_settings.pt);
saver.append_layer(tp_layer);
saver.save(`${path}/out_devobj-${devobj}-PT.svg`);
}
async function build_map_it(devobj) {
let wells = await query(sql_pi.totals(devobj));
wells = wells.filter((x) => x.wwit > 0);
if (!wells.length) return
let bbox = build_bbox(wells.map(w => ({ x: w.whx, y: w.why })));
const saver = new SvgSaver(all_settings.it, bbox);
let mapped_wells = wells.map((x) => ({ ...x, lx: saver.tr.trx(x.whx), ly: saver.tr.try(x.why) }));
let tp_layer = SvgMapBuilder.build_ti_layer(mapped_wells, all_settings.it);
saver.append_layer(tp_layer);
saver.save(`${path}/out_devobj-${devobj}-IT.svg`);
}
async function build_map_pr(devobj) {
console.log("build_map_pr", devobj);
const md = await query(sql_pi.max_date(devobj)).then((x) => x[0].date);
let wells = await query(sql_pi.totals(devobj, md))
wells = wells.filter((x) => x.wopt > 0);
if (!wells.length) return
let bbox = build_bbox(wells.map(w => ({ x: w.whx, y: w.why })));
const saver = new SvgSaver(all_settings.pr, bbox);
let mapped_wells = wells.map((x) => ({
...x,
wopt: x.wopt / x.days,
wwpt: x.wwpt / x.days,
wlpt: (x.wopt + x.wwpt) / x.days,
wlf: x.wwpt / (x.wopt + x.wwpt),
lx: saver.tr.trx(x.whx),
ly: saver.tr.try(x.why),
}));
// let mapped_wells = wells.map((x) => ({ ...x, lx: saver.tr.trx(x.whx), ly: saver.tr.try(x.why) }));
let tp_layer = SvgMapBuilder.build_tp_layer(mapped_wells, all_settings.pr);
saver.append_layer(tp_layer);
saver.save(`${path}/out_devobj-${devobj}-PR.svg`);
}
async function build_map_ir(devobj) {
// const DATE = '2023-01-01'
const md = await query(sql_pi.max_date(devobj)).then((x) => x[0].date);
let wells = await query(sql_pi.totals(devobj, md))
wells = wells.filter((x) => x.wwit > 0);
if (!wells.length) return
let bbox = build_bbox(wells.map(w => ({ x: w.whx, y: w.why })));
const saver = new SvgSaver(all_settings.ir, bbox);
let mapped_wells = wells.map((x) => ({
...x,
wwit: x.wwit / x.days,
lx: saver.tr.trx(x.whx),
ly: saver.tr.try(x.why),
}));
let tp_layer = SvgMapBuilder.build_ti_layer(mapped_wells, all_settings.ir);
saver.append_layer(tp_layer);
saver.save(`${path}/out_devobj-${devobj}-IR.svg`);
}
async function start2() {
const objects = await query(sql_pi.devobjs()).then((r) => r.map((x) => x.devobj));
objects.map(build_map_pt);
objects.map(build_map_it)
objects.map(build_map_pr)
objects.map(build_map_ir)
}
start2();