This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (112 loc) · 3.77 KB
/
index.js
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
131
132
133
134
135
136
137
138
139
140
141
142
const tileReduce = require('@mapbox/tile-reduce');
const _ = require('lodash')
const fs = require('fs');
const path = require('path');
const INPUT_FILE = 'data/states.geojson'
if (process.argv.length < 3){
console.error("Please specify a state or territory like: \n\n\tnode index.js CO \n\tnode index.js colorado\n")
process.exit(1)
}else{
process.argv.shift()
process.argv.shift()
var input = process.argv.join(" ")
}
var states = JSON.parse(fs.readFileSync(INPUT_FILE));
var values = {};
console.warn("Found " +states.features.length+ " possible areas for analysis in "+INPUT_FILE);
function cleanedName(name){
return name.toLowerCase().replace(/\s/g,"_")
}
var names = states.features.map(function(x){return cleanedName(x.properties.NAME)})
var abbrevs = states.features.map(function(x){return cleanedName(x.properties.STATE)})
var bounds;
if (names.indexOf(cleanedName(input)) > -1){
console.warn("Found area for: "+input);
bounds = states.features[names.indexOf(cleanedName(input))]
}else
if (abbrevs.indexOf(cleanedName(input)) > -1){
console.warn("Found area for: "+input);
bounds = states.features[abbrevs.indexOf(cleanedName(input))]
}else{
console.error("Sorry, "+INPUT_FILE+" does not contain a feature for "+input)
process.exit(0)
}
// process.exit(0)
// console.warn(JSON.stringify(bounds))
// TODO: Refactor this out and use again (I've written this function about 100x)
function mergeAndSum(map1, map2){
try{
Object.keys(map2).forEach(function(k){
if (map1.hasOwnProperty(k)){
map1[k] += map2[k]
}else{
map1[k] = map2[k]
}
});
return map1;
}catch(e){
console.error(JSON.stringify(map1, null, 2), JSON.stringify(map2, null, 2));
}
}
var users = {}
tileReduce({
map: path.join(__dirname, "map-statestats.js"),
zoom: 12,
sources: [{name: 'osm', mbtiles: path.join("../latest.planet-compact.mbtiles"), raw: false}],
// output: fs.createWriteStream('../data/tileSummaries.geojsonseq'),
geojson: bounds.geometry
// mapOptions: searchTerms
})
.on('reduce', function(results){
var res = results['users'];
//Iterate through the users that came back from each tile
Object.keys(res).forEach(function(user){
//Check if we have that user yet...
if (users.hasOwnProperty(user)){
//Great, we do:
Object.keys(res[user]).forEach(function(k){
//Add the value to that key
users[user][k] += res[user][k]
})
} else {
//We haven't logged this user yet, so we can just add the object
users[user] = res[user]
}
})
Object.keys(results).forEach(function(k){
if(k != 'users'){
if (values.hasOwnProperty(k)){
values[k] = mergeAndSum(values[k], results[k]);
}else{
values[k] = results[k];
}
}
})
})
.on('end', function(){
console.warn("finished, now aggregating")
var users_out = []
var count=0;
Object.keys(users).forEach(function(u){
if (count==0){
Object.keys(users[u]).forEach(function(k){
//Add the value to that key
bounds.properties[k] = users[u][k]
})
}else{
//Itererate through all of the key/value pairs for this user
Object.keys(users[u]).forEach(function(k){
//update the global value for it.
bounds.properties[k] += users[u][k]
})
users[u]['user'] = u
users_out.push(users[u])
}
count++;
})
fs.writeFileSync("results/geojson/"+cleanedName(input)+".geojson", JSON.stringify(bounds))
var sorted = _.sortBy(users_out, function(u){return -u.total_edits;})
fs.writeFileSync("results/json/"+cleanedName(input)+"_users.json", JSON.stringify(sorted))
fs.writeFileSync("results/json/"+cleanedName(input)+"_values.json", JSON.stringify(values))
console.warn("DONE")
})