UNPKG

geotoolsconnector

Version:

Fast and reliable way to analize geospatial data.

180 lines (143 loc) 5.46 kB
var express = require('express'); const axios = require('axios'); var pipeClass = require('./geotoolspipe.js'); class GeoToolsConnector { constructor(enableGUI=false) { this.enableGUI = enableGUI; if(this.enableGUI) { this.serveGUI(); } } print(text) { console.log(text) } serveGUI() { var app = express(); var path = require('path'); app.use( express.urlencoded({ extended: true }) ); app.use( express.json({ limit: '25MB' }) ); app.use(express.static(__dirname + '/public')); var pipe = new pipeClass(); function SendFile (REQ, RES, T, F) { RES.sendFile(T, { root: path.resolve() } ); } app.get('/', function (req, res) { SendFile(req, res, './gui.html'); }); app.get('/geotoolspipe', function (req, res) { SendFile(req, res, './geotoolspipe.js'); }); app.post('/dissolve', async (req, res) => { var result = await this.dissolve(req.body); if(result) res.send(result); else res.status(500); }); app.post('/intersection', async (req, res) => { var result = await this.intersection(req.body); if(result) res.send(result); else res.status(500); }); app.post('/centroid', async (req, res) => { var result = await this.centroid(req.body); if(result) res.send(result); else res.status(500); }); app.post('/difference', async (req, res) => { var result = await this.difference(req.body); if(result) res.send(result); else res.status(500); }); app.post('/polygonize', async (req, res) => { var result = await this.polygonize(req.body); if(result) res.send(result); else res.status(500); }); app.post('/contours', async (req, res) => { var result = await this.contours(req.body); if(result) res.send(result); else res.status(500); }); app.post('/simplify', async (req, res) => { var result = await this.simplify(req.body); if(result) res.send(result); else res.status(500); }); app.post('/multiparttosingleparts', async (req, res) => { var result = await this.multiparttosingleparts(req.body); if(result) res.send(result); else res.status(500); }); app.post('/adddatapipe', async (req, res) => { var success = await pipe.addData(req.body); if(success) res.send(success); else res.status(500); }); app.post('/addprocesspipe', async (req, res) => { var success = await pipe.addProcess(req.body); if(success) res.send(success); else res.status(500); }); app.post('/addresultspipe', async (req, res) => { var success = await pipe.addResults(req.body); if(success) res.send(success); else res.status(500); }); app.post('/pipe', async (req, res) => { console.log("in /pipe"); var reqJson = pipe.buildJSON(); var result = await this.pipe(reqJson); if(result) res.send(result); else res.status(500); pipe.clearPipe(); }); app.listen(4444); } makePostRequest(command, body) { var reqBody = JSON.stringify(body); if (command === 'pipe') { reqBody = body; } return new Promise((resolve, reject) => { axios.post(`https://geotools-dev.lgb.si/${command}`, reqBody) .then(res => { resolve({ body: res.data }); }) .catch(error => { resolve({ err: error }); }) }); } async dissolve(data) { var dissolve = await this.makePostRequest('dissolve', data); return dissolve.body; } async intersection(data) { var intersection = await this.makePostRequest('intersection', data); return intersection.body; } async centroid(data) { var centroid = await this.makePostRequest('centroid', data); return centroid.body; } async difference(data) { var difference = await this.makePostRequest('difference', data); return difference.body; } async polygonize(data) { var difference = await this.makePostRequest('polygonize', data); return difference.body; } async contours(data) { var contours = await this.makePostRequest('contours', data); return contours.body; } async simplify(data) { var simplify = await this.makePostRequest('simplify', data); return simplify.body; } async multiparttosingleparts(data) { var multiparttosingleparts = await this.makePostRequest('multiparttosingleparts', data); return multiparttosingleparts.body; } async pipe(data) { var pipe = await this.makePostRequest('pipe', data); return pipe.body; } } module.exports = GeoToolsConnector