Skip to content

Commit

Permalink
add insert into file plus connection file
Browse files Browse the repository at this point in the history
insert into file inserts geojson features as rows into openroads-vn database. the connection file builds the connection needed to do so
  • Loading branch information
maxgrossman committed Sep 14, 2017
1 parent 24500f5 commit c50836e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
22 changes: 22 additions & 0 deletions processing/db/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
var assert = require('assert');

// set the db urls base on environment
var DEFAULT_ENVIRONMENT = 'development';
var environment = process.env.MACROCOSM_ENV || DEFAULT_ENVIRONMENT;
var connection = process.env.DATABASE_URL || require('./local').connection[environment];

assert.ok(connection, 'Connection is undefined; check DATABASE_URL or local.js');

// connect knex to the current env's db.
var knex = require('knex')({
client: 'pg',
connection: connection,
debug: false,
pool: {
min: 2,
max: 10
}
});

module.exports = knex;
55 changes: 55 additions & 0 deletions processing/e-insert-tables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var createReadStream = require('fs').createReadStream;
var createWriteStream = require('fs').createWriteStream;
var readdirSync = require('fs').readdirSync;
var path = require('path');
var parallel = require('async').parallel;

var baseDir = 'data/processing/d-simplify-props'
var knex = require('./db/connection/.js')
var postgis = require('knex-postgis');

// streams to read and write geojsons
var geojsonStream = require('geojson-stream');
var parser = geojsonStream.parse();
var stringifier = geojsonStream.stringify();
// helps split single-line json into chunked-by-line geojson
var split = require('split');
// tmp dir with geojsons
var adminPath = `${baseDir}/tmp`;
var admins = readdirSync(adminPath)

var db = knex({dialect: 'postgres'});
var st = postgis(db);

// create list of async functions to pass to parallel
const adminTasks = admins.map((admin) => {
return function(cb) {
var basename = admin.split('-')[1]
var adminFile = path.join(adminPath, admin)
var adminFile = path.join('./', admin);
var adminFileStream = createReadStream(adminFile)
.pipe(split())
.pipe(parser)
.on('data', (feature) => {
insertIntoTable(feature, basename)
})
.on('end', () => { cb(null, null) })
}
});

function insertIntoTable (feature, admin) {
const properties = feature.properties;
const geometry = feature.geometry;
const statement = db.insert({
type: admin,
id: properties.id,
parent_id: properties.p_id,
geo: st.geomFromGeoJSON(geometry),
name_en: properties.en_name,
name_vn: ''
}).into(`${admin}-table`).toString();
}

parallel(adminTasks, (err, res) => {
if (!err) {}
});

0 comments on commit c50836e

Please sign in to comment.