Skip to content

Commit

Permalink
WEBGIS
Browse files Browse the repository at this point in the history
  • Loading branch information
arieansyah committed Jul 23, 2020
0 parents commit 480fa76
Show file tree
Hide file tree
Showing 24 changed files with 1,999 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=password
DB_DATABASE=database
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
.env
.env.backup
49 changes: 49 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var polygon = require('./routes/polygon');
var usersRouter = require('./routes/users');
var migration = require('./routes/migration');
var seed = require('./routes/seeder');
require('dotenv').config();

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'node_modules')));

app.use('/', indexRouter);
app.use('/migration', migration);
app.use('/seed', seed);
app.use('/users', usersRouter);
app.use('/api/polygon', polygon);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('geo-final:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
20 changes: 20 additions & 0 deletions config/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
require('dotenv').config()

const {
Pool
} = require('pg')
const isProduction = process.env.NODE_ENV === 'production'

const connectionString = `postgresql://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_DATABASE}`

const pool = new Pool({
connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
ssl: isProduction,
})

// console.log(pool)

module.exports = {
pool
}
42 changes: 42 additions & 0 deletions controller/MapController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// const pool = require('../config/connection');
const {
pool
} = require('../config/connection');
require('dotenv').config();

async function index(req, res) {
res.render('map', {
title: 'WebGis'
})
}

module.exports.index = index;

async function polygon(req, res) {
let polygons = `SELECT
row_to_json(fc)
FROM (
SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
FROM (
SELECT 'Feature'
As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json((id, name, deskripsi, luas_area)) As properties
FROM polygon As lg
UNION ALL
SELECT 'Feature'
As type, ST_AsGeoJSON(ln.geom)::json As geometry, row_to_json((id, name, deskripsi, panjang_garis)) As properties
FROM line As ln
UNION ALL
SELECT 'Feature'
As type, ST_AsGeoJSON(p.geom)::json As geometry, row_to_json((id, name)) As properties
FROM point As p
) As f
) As fc`;
pool.query(polygons, (error, results) => {
if (error) {
throw error
}
res.status(200).json(results.rows)
})
}

module.exports.polygon = polygon;
46 changes: 46 additions & 0 deletions controller/PolygonController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const {
pool
} = require('../config/connection');

require('dotenv').config();

async function create(req, res) {
let result = {
success: false
}

let featureParse = JSON.parse(req.body.geom)

var query = `INSERT INTO polygon (name, geom, luas_area, deskripsi)
VALUES (
'${req.body.name}',
ST_GeomFromGeoJSON('${JSON.stringify(featureParse.features[0].geometry)}'),
0,
'${req.body.deskripsi}');`

await pool.query(query)
.then(validate => {
result.success = true
result.msg = "berhasil"
res.json(result)
}).catch(err => {
console.log(err);
result.msg = err.message
res.json(result)
})

let update_polygon = `UPDATE polygon SET luas_area = ST_Area(geom::geography);`
pool.query(update_polygon)
.then(level => {
result.success = true
result.msg = 'Update Data success'
res.json(result)
})
.catch(err => {
console.log(err);
result.msg = err.message
res.json(result)
})
}

module.exports.create = create;
75 changes: 75 additions & 0 deletions migration/tableMigration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const {
pool
} = require('../config/connection');
require('dotenv').config();

module.exports = {
create: async function (req, res) {
let result = {
success: false,
msg: '',
level: null
}

let extension = 'CREATE EXTENSION postgis;'
await pool.query(extension);

let polygon = 'CREATE TABLE polygon (id SERIAL,' +
'name VARCHAR,' +
'geom GEOMETRY,' +
'luas_area REAL,' +
'deskripsi TEXT,' +
'PRIMARY KEY(id));'

pool.query(polygon)
.then(level => {
result.success = true
result.msg = 'Create table success'
result.level = level
res.json(result)
})
.catch(err => {
console.log(err);
result.msg = err.message
res.json(result)
});

let line = 'CREATE TABLE line (id SERIAL,' +
'name VARCHAR,' +
'geom GEOMETRY,' +
'panjang_garis REAL,' +
'deskripsi TEXT,' +
'PRIMARY KEY(id));'

pool.query(line)
.then(level => {
result.success = true
result.msg = 'Create table success'
result.level = level
res.json(result)
})
.catch(err => {
console.log(err);
result.msg = err.message
res.json(result)
});

let point = 'CREATE TABLE point (id SERIAL,' +
'name VARCHAR,' +
'geom GEOMETRY,' +
'PRIMARY KEY(id));'

pool.query(point)
.then(level => {
result.success = true
result.msg = 'Create table success'
result.level = level
res.json(result)
})
.catch(err => {
console.log(err);
result.msg = err.message
res.json(result)
});
},
}
Loading

0 comments on commit 480fa76

Please sign in to comment.