Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
brandboat committed Aug 14, 2014
1 parent afe37ad commit 1bcb7a3
Show file tree
Hide file tree
Showing 1,309 changed files with 549,957 additions and 166 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
The OpenShift `nodejs` cartridge documentation can be found at:
osmapp
======

This is an application of osm on openshift.

Website : http://osmapp-brandboat.rhcloud.com/

The OpenShift `nodejs` cartridge documentation can be found at:
http://openshift.github.io/documentation/oo_cartridge_guide.html#nodejs
50 changes: 50 additions & 0 deletions app/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var path = require('path'),
rootPath = path.normalize(__dirname + '/../..');

var config = {
// Development Config
//
development: {
server: {
port: 3000,
hostname: 'localhost',
},
database: {
url: 'postgresql://127.0.0.1:5432',
dbname: 'test'
},
root: rootPath
},
//
// Production Config
//
production: {
server: {
port: process.env.OPENJS_PORT || 8080,
hostname: process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'
},
database: {
url: 'postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT' || 'postgresql://127.0.0.1:5432',
dbname: process.env.OPENSHIFT_APP_NAME || 'routingonosm'
},
root: rootPath
},
//
// Test Config
//
test: {
server: {
port: 4001,
hostname: 'localhost',
},
database: {
// url: 'postgresql://127.0.0.1:5432',
// table_name: 'parks'
username: 'brandboat',
password: '1234',
dbname: 'test'
}
}
};

module.exports = config[process.env.NODE_ENV || 'development'];
34 changes: 34 additions & 0 deletions app/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var pg = require('pg');
var config = require('../config/config');
var conString = "postgres://" + config.database.username + ":"
+ config.database.password + "@localhost/" + config.database.dbname;
var client = new pg.Client(conString);

function connect_db() {
client.connect(function(err) {
if(err) {
return console.error('✗ Postgresql Connection Error. Please make sure Postgresql is running. -> ', err);
}
client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('✗ Postgresql Running Query Error', err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
});
});
}

function query_db(t_query, callback) {
client.query(t_query, callback);
}

function disconnect() {
client.end();
}

module.exports = exports = {
connectDB: connect_db,
queryDB: query_db
}

63 changes: 63 additions & 0 deletions app/config/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var logger = require('morgan');
var path = require('path');
var env = process.env.NODE_ENV || 'development';
var routes = require('../routes');
var errorHandler = require('errorhandler');

module.exports = function (app, express) {
app.set('env', env);
app.set('port', app.config.server.port || 3001);
app.set('hostname', app.config.server.hostname || '127.0.0.1');
app.set('views', path.join(__dirname, '../../app/views'));
app.set('view engine', 'jade');

if (env === 'development') {
app.use(logger('dev'));
} else {
app.use(logger());
};

// ROUTES
app.use(routes);

// load static files in /public
app.use(express.static(path.join(app.config.root, 'public')));

app.use(function handleNotFound(req, res, next){
res.status(404);

if (req.accepts('html')) {
res.render('404', { url: req.url, error: '404 Not Found' });
return;
}
});

if (env === 'development') {
app.use(errorHandler());
} else {


app.use(function logErrors(err, req, res, next){
if (err.status === 404) {
return next(err);
}
console.log(err.stack);
next(err);
});

app.use(function respondError(err, req, res, next){
var status, message;
status = err.status || 500;
res.status(status);

message = ((err.productionMessage && err.message) || err.customProductionMessage);
if (!message) {
if (status === 403) {
message = 'Not Allowed';
} else {
message = 'Oops, there was a problem!';
}
}
});
}
}
118 changes: 118 additions & 0 deletions app/controllers/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
exports.queryTime = function(req, res) {
var pg = require('pg');
var config = require('../config/config');
//var conString = "postgres://" + config.database.username + ":"
// + config.database.password + "@localhost/" + config.database.dbname;
var conString = config.database.url + "/" + config.database.dbname;
var client = new pg.Client(conString);

function connect_db() {
client.connect(function(err) {
if(err) {
return console.error('✗ Postgresql Connection Error. Please make sure Postgresql is running. -> ', err);
}
console.log("✔ Connect to Postgresql");
});
}
client.connect();
client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('✗ Postgresql Running Query Error', err);
}
res.send(200, result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
}

exports.pgr_dijkstra = function(req, res) {
var pg = require('pg');
var config = require('../config/config');
var conString = config.database.url + "/" + config.database.dbname;
//var conString = "postgres://" + config.database.username + ":"
// + config.database.password + "@localhost/" + config.database.dbname;
var client = new pg.Client(conString);
var reqPoint = [
{
"lat": req.query.beginLat,
"lng": req.query.beginLng
},
{
"lat": req.query.endLat,
"lng": req.query.endLng
}
];
var point = [];
var i = 0;
client.connect();
getPoint();

// get the nearest point of begin and end point in topology network(database).
function getPoint() {
if( i === reqPoint.length) {
return dijkstra(point[0], point[1]);
}
var qStr = "SELECT id FROM ways_vertices_pgr ORDER BY st_distance(the_geom, st_setsrid(st_makepoint(" + reqPoint[i].lng + "," + reqPoint[i].lat + "), 4326)) LIMIT 1;";

client.query(qStr, function(err,result) {
if(err) {
console.log(qStr);
return console.error('✗ Postgresql Running Query Error', err);
}
point[i++] = result.rows[0].id;
getPoint();
});
}

function dijkstra(begin, end) {
var qStr = "WITH result AS (SELECT * FROM ways JOIN (SELECT seq, id1 AS node, id2 AS edge_id, cost, ROW_NUMBER() OVER (PARTITION BY 1) AS rank FROM pgr_dijkstra('SELECT gid AS id, source::integer, target::integer, length::double precision AS cost FROM ways'," + begin + ", " + end + ", false, false)) AS route ON ways.gid = route.edge_id ORDER BY rank) SELECT ST_AsEWKT(result.the_geom), name from result;";
client.query(qStr, function(err, result) {
if(err) {
console.log(qStr);
return console.error('✗ Postgresql Running Query Error', err);
}
parsingData(result.rows);
});
}

function toGeoJson(road, type, points) {
if(road === "") {
road = "unknown";
}
var geoJson = {
"road": road,
"type": type,
"coordinates": points
};
return geoJson;
} // parsing the result from query.
function parsingData(data) {
// the data is like : "SRID=4326;LINESTRING(120.2121912 22.9975817,120.2123558 22.9982876)"
var x = 0;
var result = [];
for(var i = 0; i != data.length; i++) {
var points = [];
var y = 0;
var tmp = data[i].st_asewkt.split('(');
var tmp2 = tmp[1].split(')');
var tmp3 = tmp2[0].split(',');
for(var j = 0; j != tmp3.length; j++) {
var tmp4 = tmp3[j].split(' ');
var point = [tmp4[0], tmp4[1]]
tmp4[1];
points[y] = point;
y++;
}
var geoObj = toGeoJson(data[i].name, "LineString", points);
result[x++] = geoObj;
}
allDone(result);
}

function allDone(data) {
res.send(200, data);
res.end();
client.end();
}
}

15 changes: 15 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var express = require('express');
var Route = express.Router();
var config = require('../config/config');

var routeController = require(config.root + '/app/controllers/route');

Route.get('/', function (req, res) {
res.render('route');
});

// api routes
Route
.get('/routing', routeController.pgr_dijkstra)

module.exports = Route;
50 changes: 50 additions & 0 deletions app/views/route.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
doctype html
html
head
meta(http-equiv="Content-Type" content="text/html; charset=utf-8")
meta(name="keywords" content="HTML, CSS, leaflet, JavaScript, openshift, nodejs, openstreetmap")
meta(name="viewport" content="width=device-width, initial-scale=1.0")
meta(name="author" content="brandboat")
link(href="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.css" rel="stylesheet")
link(href="/css/route.css" rel="stylesheet")
link(href="/semantic/packaged/css/semantic.min.css" rel="stylesheet")
link(href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700|Open+Sans:300italic,400,300,700' rel='stylesheet' type='text/css')
script(src="/semantic/packaged/javascript/semantic.js" type="text/script")
script(src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js?2" type="text/javascript")
script(src="/javascripts/plugins/Leaflet.MakiMarkers.js" type="text/javascript")
script(src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript")
script(src="//cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.2/jquery.ui.touch-punch.min.js")
title osmapp

body(onload="start()")
div(id="menu")
div(class="ui primary inverted segment")
a(class="item")
<i class="location icon"></i> Routing
div(class="ui black header")
<i class="map marker icon"></i> Control Panel
div(class="ui form segmant")
div(class="inline fields")
div(class="ui button" id="begin") begin
div(class="field")
div(class="ui input")
input(type="text" placeholder="latitude" id="beginLat")
div(class="ui input")
input(type="text" placeholder="longitude" id="beginLng")
div(class="inline fields")
div(class="ui button" id="end") end
div(class="field")
div(class="ui input")
input(type="text" placeholder="latitude" id="endLat")
div(class="ui input")
input(type="text" placeholder="longitude" id="endLng")
div(class="ui buttons")
div(class="ui red button" id="reset") Reset
div(class="or")
div(class="ui green submit button" id="route") Route
div(class="ui black header")
<i class="road icon"></i> Road
div(id="road")
div(id="map")
script(type="text/javascript", src="/javascripts/route.js")

Loading

0 comments on commit 1bcb7a3

Please sign in to comment.