Repository navigation
Expand file tree
/
Copy pathgenerateData.js
More file actions
130 lines (110 loc) · 3.31 KB
/
Copy pathgenerateData.js
File metadata and controls
130 lines (110 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Script to generate simulated user data from 311 calls location dataset
*/
const fs = require('fs');
const parse = require("csv-parse");
const faker = require('faker');
const moment = require('moment');
function processFloodedRoads() {
Array.prototype.flatMap = function(lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
const floodRoads = JSON.parse(fs.readFileSync('./houston.geojson').toString());
console.log(floodRoads);
const res = floodRoads.features.flatMap(feature => {
return feature.geometry.coordinates
});
fs.writeFileSync("./flooded.json", JSON.stringify(res));
}
function loadFloodedRoads() {
return JSON.parse(fs.readFileSync('./flooded.json').toString());
}
// processFloodedRoads();
const floodedRoads = loadFloodedRoads();
const parser = parse({
delimiter: '|',
trim: true,
skip_empty_lines: true
});
const keywords = [
"small wounds",
"large wounds",
"broken arm",
"broken leg",
];
let processData = () => {
//console.log(results);
const resultsLatLng = results.slice(1).map(row => [parseFloat(row[1]), parseFloat(row[2]), row[4]]);
function getRandomPriority(minDistToHotspot) {
return Math.random()*0.3 +
/*Math.random()**/Math.min(0.7, 0.000001/minDistToHotspot)
}
function generateRandomUser(lat, lng, timestamp) {
if(!lat || !lng) {
return null;
}
const hotspots = floodedRoads;
const distToHotspot = hotspots.map(([ptlng, ptlat]) => {
const dlat = lat - ptlat;
const dlng = lng - ptlng;
return Math.pow(dlat,2) + Math.pow(dlng,2);
}).filter(Boolean);
// console.log(distToHotspot);
const minDistToHotspot = Math.min(...distToHotspot);
// console.log(minDistToHotspot);
console.log(timestamp);
return {
_id: uuidv4(),
userId: String(Math.round(Math.random() * 50000)),
name: faker.fake('{{name.firstName}}'),
timestamp: moment(timestamp, "YYYY-MM-DD HH:mm:ss.SSS").unix(),
location_information: {
geometry: {
location: {
lat: lat,
lng: lng,
}
}
},
phone_number: Math.floor(Math.random() * 9000000000 + 1000000000),
health: {
priority: getRandomPriority(minDistToHotspot),
key_words: [keywords[Math.floor(Math.random() * keywords.length)]]
},
food: {
priority: getRandomPriority(minDistToHotspot)
},
hygiene: {
priority: getRandomPriority(minDistToHotspot)
}
};
}
// https://stackoverflow.com/a/2117523
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const data =
resultsLatLng
.map((coord, i) => {
console.log(`index ${i} / ${resultsLatLng.length}`);
return generateRandomUser(coord[0], coord[1], coord[2]);
})
.filter(record => record);
console.log(data);
fs.writeFile("./public/fulldata.json", JSON.stringify(data), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
};
const results = [];
fs.createReadStream('./floodingheatmap12m.csv')
.pipe(parser)
.on('data', (data) => {
results.push(data);
})
.on('end', processData);