-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add insert into file plus connection file
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
1 parent
24500f5
commit c50836e
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
}); |