|
| 1 | +var config = require('./config.js'), |
| 2 | + pg = require('pg-query'), |
| 3 | + path = require('path'); |
| 4 | +var table_name = config.get('table_name'); |
| 5 | + |
| 6 | +pg.connectionParameters = config.get('db_config'); |
| 7 | +console.log(pg.connectionParameters); |
| 8 | + |
| 9 | +var error_response = "data already exists - bypassing db initialization step\n"; |
| 10 | + |
| 11 | +function createDBSchema(err, rows, result) { |
| 12 | + if(err && err.code == "ECONNREFUSED"){ |
| 13 | + return console.error("DB connection unavailable, see README notes for setup assistance\n", err); |
| 14 | + } |
| 15 | + var query = "CREATE TABLE "+table_name+" ( gid serial NOT NULL, name character varying(240), the_geom geometry, CONSTRAINT "+table_name+ "_pkey PRIMARY KEY (gid), CONSTRAINT enforce_dims_geom CHECK (st_ndims(the_geom) = 2), CONSTRAINT enforce_geotype_geom CHECK (geometrytype(the_geom) = 'POINT'::text OR the_geom IS NULL),CONSTRAINT enforce_srid_geom CHECK (st_srid(the_geom) = 4326) ) WITH ( OIDS=FALSE );"; |
| 16 | + pg(query, addSpatialIndex); |
| 17 | +}; |
| 18 | + |
| 19 | +function addSpatialIndex(err, rows, result) { |
| 20 | + if(err) { |
| 21 | + return console.error(error_response, err); |
| 22 | + } |
| 23 | + pg("CREATE INDEX "+table_name+"_geom_gist ON "+table_name+" USING gist (the_geom);", importMapPoints); |
| 24 | +} |
| 25 | + |
| 26 | +function importMapPoints(err, rows, result) { |
| 27 | + if(err) { |
| 28 | + return console.error(error_response, err); |
| 29 | + } |
| 30 | + var points = require(path.resolve('./parkcoord.json')); |
| 31 | + var insert = "Insert into "+table_name+" (name, the_geom) VALUES "; |
| 32 | + var qpoints = points.map(insertMapPinSQL).join(","); |
| 33 | + var query = insert + qpoints + ';'; |
| 34 | + console.log(query); |
| 35 | + pg(query, function(err, rows, result) { |
| 36 | + if(err) { |
| 37 | + return console.error(error_response, err); |
| 38 | + } |
| 39 | + var response = 'Data import completed!'; |
| 40 | + return response; |
| 41 | + }); |
| 42 | +}; |
| 43 | + |
| 44 | +function insertMapPinSQL(pin) { |
| 45 | + var query = ''; |
| 46 | + var escape = /'/g |
| 47 | + |
| 48 | + if(typeof(pin) == 'object'){ |
| 49 | + query = "('" + pin.Name.replace(/'/g,"''") + "', ST_GeomFromText('POINT(" + pin.pos[0] +" "+ pin.pos[1] + " )', 4326))"; |
| 50 | + } |
| 51 | + return query; |
| 52 | +}; |
| 53 | + |
| 54 | +function init_db(){ |
| 55 | + pg('CREATE EXTENSION postgis;', createDBSchema); |
| 56 | +} |
| 57 | + |
| 58 | +function flush_db(){ |
| 59 | + pg('DROP TABLE '+ table_name+';', function(err, rows, result){ |
| 60 | + var response = 'Database dropped!'; |
| 61 | + console.log(response); |
| 62 | + return response; |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function select_box(req, res, next){ |
| 67 | + //clean these variables: |
| 68 | + var query = req.query; |
| 69 | + var limit = (typeof(query.limit) !== "undefined") ? query.limit : 40; |
| 70 | + if(!(Number(query.lat1) |
| 71 | + && Number(query.lon1) |
| 72 | + && Number(query.lat2) |
| 73 | + && Number(query.lon2) |
| 74 | + && Number(limit))) |
| 75 | + { |
| 76 | + res.send(500, {http_status:400,error_msg: "this endpoint requires two pair of lat, long coordinates: lat1 lon1 lat2 lon2\na query 'limit' parameter can be optionally specified as well."}); |
| 77 | + return console.error('could not connect to postgres', err); |
| 78 | + } |
| 79 | + pg('SELECT gid,name,ST_X(the_geom) as lon,ST_Y(the_geom) as lat FROM ' + table_name+ ' t WHERE ST_Intersects( ST_MakeEnvelope('+query.lon1+", "+query.lat1+", "+query.lon2+", "+query.lat2+", 4326), t.the_geom) LIMIT "+limit+';', function(err, rows, result){ |
| 80 | + if(err) { |
| 81 | + res.send(500, {http_status:500,error_msg: err}) |
| 82 | + return console.error('error running query', err); |
| 83 | + } |
| 84 | + res.send(rows); |
| 85 | + return rows; |
| 86 | + }) |
| 87 | +}; |
| 88 | +function select_all(req, res, next){ |
| 89 | + console.log(pg); |
| 90 | + pg('SELECT gid,name,ST_X(the_geom) as lon,ST_Y(the_geom) as lat FROM ' + table_name +';', function(err, rows, result) { |
| 91 | + console.log(config); |
| 92 | + if(err) { |
| 93 | + res.send(500, {http_status:500,error_msg: err}) |
| 94 | + return console.error('error running query', err); |
| 95 | + } |
| 96 | + res.send(result); |
| 97 | + return rows; |
| 98 | + }); |
| 99 | +}; |
| 100 | + |
| 101 | +module.exports = exports = { |
| 102 | + selectAll: select_all, |
| 103 | + selectBox: select_box, |
| 104 | + flushDB: flush_db, |
| 105 | + initDB: init_db |
| 106 | +}; |
0 commit comments