Skip to content

Commit c3bdb7d

Browse files
committed
topology added
1 parent dc25f35 commit c3bdb7d

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pgRouting-Web-API [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/I1mran/pgRouting-Web-Direction-API/blob/master/LICENSE)
1+
# pgRouting-API [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/I1mran/pgRouting-Web-Direction-API/blob/master/LICENSE)
22

33
A complete solution of pgRouting direction Web API written in Node js (Javascript) Express
44

server.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
const express = require('express');
22
const cors = require('cors');
33
const bodyParser = require("body-parser");
4-
const {route, closest, distance} = require('./src/pgRouting');
4+
const { route, closest, distance, topology } = require('./src/pgRouting');
55
const { exp_config } = require('./src/config');
66

7-
// use nodemon no need to use webpack-hot-middleware and webpackDevMiddleware for express
8-
//
9-
// import webpackDevMiddleware from 'webpack-dev-middleware'
10-
// import config from '../webpack.config'
11-
// import webpackHotMiddleware from 'webpack-hot-middleware'
12-
// import webpack from 'webpack'
13-
147
const app = express();
15-
// const compiler = webpack(config);
168
app.use(cors());
179
app.use(bodyParser.json());
1810

19-
// use nodemon no need to use webpack-hot-middleware and webpackDevMiddleware for express
20-
// app.use(webpackDevMiddleware(compiler, {
21-
// publicPath: config.output.publicPath
22-
// }));
23-
// app.use(webpackHotMiddleware(compiler));
24-
25-
app.get('/', (req, res) => {
11+
app.get('/api', (req, res) => {
2612
res.send('Welcome to PG Routing API Written in Node JS Express!');
2713
});
2814

29-
app.get('/route', async(req, res, next) => {
15+
app.get('/api/route', async (req, res, next) => {
3016

3117
const { start, end } = req.query;
3218
route(start, end)
@@ -38,7 +24,7 @@ app.get('/route', async(req, res, next) => {
3824
});
3925
});
4026

41-
app.get('/distance', async(req, res, next) => {
27+
app.get('/api/distance', async (req, res, next) => {
4228
const { start, end } = req.query;
4329
distance(start, end)
4430
.then((result) => {
@@ -49,7 +35,7 @@ app.get('/distance', async(req, res, next) => {
4935
});
5036
});
5137

52-
app.get('/closest', async(req, res, next) => {
38+
app.get('/api/closest', async (req, res, next) => {
5339
const { lat, lng } = req.query;
5440
const buffer = values.buffer || 1;
5541
const limit = values.limit || 1;
@@ -62,4 +48,14 @@ app.get('/closest', async(req, res, next) => {
6248
});
6349
});
6450

51+
app.get('/api/topology', async (req, res, next) => {
52+
topology()
53+
.then((result) => {
54+
res.status(200).json({ result: "Okay" });
55+
})
56+
.catch((reason) => {
57+
res.status(500).json(reason);
58+
});
59+
});
60+
6561
app.listen(exp_config.port, () => console.log(`app listening on port ${exp_config.port}!`));

src/pgRouting.js

+17
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,25 @@ function closest(lat, lng, buffer, limit) {
4444
return query(nearbyQuery(lat, lng, buffer, limit));
4545
}
4646

47+
const topology = async () => {
48+
await query(`SELECT pgr_createTopology(${config_pg.table}, 0.0001, 'geom', 'id')`, () => {
49+
console.log("Topology created");
50+
})
51+
await query(`SELECT pgr_analyzeGraph(${config_pg.table}, 0.0001, 'geom','id', 'source', 'target')`, () => {
52+
console.log("Graph Anaysis");
53+
54+
})
55+
await query(`UPDATE ${config_pg.table} SET cost_len = ST_Length(st_transform(geom, 3857))`, () => {
56+
console.log("Cost length");
57+
58+
})
59+
await query(`UPDATE ${config_pg.table} SET rcost_len = cost_len)`, () => {
60+
console.log("Reverse cost length");
61+
})
62+
}
4763
module.exports = {
4864
route,
4965
distance,
5066
closest,
67+
topology
5168
};

0 commit comments

Comments
 (0)