Skip to content

Instantly share code, notes, and snippets.

@Zia-
Created March 22, 2020 10:17
Show Gist options
  • Save Zia-/eed86eeadde1000316dc59fa8bd226c5 to your computer and use it in GitHub Desktop.
Save Zia-/eed86eeadde1000316dc59fa8bd226c5 to your computer and use it in GitHub Desktop.
var fs = require('fs'),
es = require('event-stream'),
csv = require('csv-parser')
var input = '/Users/zzz/code/gitHub/ground_motion/additional/sample_data/ziya_hoca/levels_new/250m.csv'
var output = '/Users/zzz/code/gitHub/ground_motion/additional/sample_data/ziya_hoca/levels_new/250m.geojson'
var x_inc = 0.00298157703587,
y_inc = 0.00225232613786
/*
30m
x_inc = 0.000357789244304
y_inc = 0.000269755339767
50m
x_inc = 0.000596315407174
y_inc = 0.000449592232946
70m
x_inc = 0.000834599657099
y_inc = 0.000629551132157
100m
x_inc = 0.00119263081435
y_inc = 0.000899184465891
130m
x_inc = 0.00154974625593
y_inc = 0.00116871331083
180m
x_inc = 0.00214860381697
y_inc = 0.00161665221045
250m
x_inc = 0.00298157703587
y_inc = 0.00225232613786
--- Not in use below pixel size
320m
x_inc = 0.00380979287917
y_inc = 0.0028818582882
500m
x_inc = 0.00596315407174
y_inc = 0.00450465227573
1000m
x_inc = 0.0119263081435
y_inc = 0.00892267662308
*/
x_inc = x_inc/2
y_inc = y_inc/2
var outStream = fs.createWriteStream(output);
outStream.writable = true;
outStream.write('{"type":"FeatureCollection", "crs":{"type": "name", "properties":{"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}, "features":[')
function genGeom(data) {
// var lnArrInt = lnArr.map(Number)
// data
// console.log(lnArrInt, typeof(lnArrInt))
var x = parseFloat(data.x),
y = parseFloat(data.y),
z = parseFloat(data.z)
var coord = [
[
[x-x_inc, y-y_inc],
[x+x_inc, y-y_inc],
[x+x_inc, y+y_inc],
[x-x_inc, y+y_inc],
[x-x_inc, y-y_inc]
]
]
var geom = {}
geom['type'] = 'Polygon'
geom['coordinates'] = coord
var feat = {}
feat['type'] = 'Feature'
feat['geometry'] = geom
feat['properties'] = {
'z': z
}
return feat
}
var counter = 0
// var s = fs.createReadStream('/Users/zzz/exp/rubbish/newPixelData/upsample.csv')
// .pipe(es.split())
// .pipe(es.mapSync( (ln) => {
// var lnArr = ln.split(",")
// if (counter == 0) {
// console.log("aaa")
// counter += 1
// } else if (counter == 1) {
// console.log("aaa1", lnArr)
// var feat = genGeom(lnArr)
// outStream.write(JSON.stringify(feat))
// counter += 1
// }
// console.log("aaa2", lnArr)
// var feat = genGeom(lnArr)
// outStream.write(',\n' + JSON.stringify(feat))
// })
// .on('error', (err) => {
// console.log('Error while reading file.', err);
// })
// .on('end', () => {
// outStream.write(']}')
// console.log('Read entire file.')
// })
// );
fs.createReadStream(input)
.pipe(csv(['x', 'y', 'z']))
.on('data', (data) => {
if (counter == 0) {
counter += 1
} else if (counter == 1) {
var feat = genGeom(data)
outStream.write(JSON.stringify(feat))
counter += 1
} else {
var feat = genGeom(data)
outStream.write(',\n' + JSON.stringify(feat))
}
})
.on('end', () => {
outStream.write(']}')
// console.log(results);
// [
// { NAME: 'Daffy Duck', AGE: '24' },
// { NAME: 'Bugs Bunny', AGE: '22' }
// ]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment