-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (111 loc) · 3.53 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
// node> .load index.js
const soda = require('soda-js')
const fs = require('fs')
const d3 = require('d3')
const turf = require('turf')
// let supervisorGeo = loadFromLocal('data/CurrentSupervisorDistricts.geojson')
// var whereString = `lic like '%D13%' OR lic like '%D14%'`
var whereString = `lic like '%H86%'`
whereString += ' AND (location_end_date IS NULL AND dba_end_date IS NULL) '
/* get data from socrata, then analyze*/
// getData(saveData)
// getData(analyzeData)
// getData(lookForNulls)
/* load data from disk, then analyze data */
// let data = loadFromLocal('closeds.json')
// analyzeData(data)
// lookForNulls(data)
// getRollup(saveData)
let data = loadFromLocal('data/unique_naics.json')
// foo(data)
console.log(data.length);
function foo (data){
let longstring = ''
// let bigarray = []
data.forEach(el=>{
longstring += ' ' + el.naic_code
})
let bigarray = longstring.split(' ')
let unique = [...new Set(bigarray)];
writeFile('unique_naics.json', unique);
}
function getRollup(handler){
let fieldname = 'neighborhoods_analysis_boundaries'
let consumer = new soda.Consumer('data.sfgov.org')
// let whereString = `${}`
consumer.query()
.withDataset('vbiu-2p9h')
.limit(220000)
// .group(fieldname)
// .select([fieldname])
.getRows()
.on('success', handler)
.on('error', function(error) { console.error(error); });
}
function getData(handler){
let consumer = new soda.Consumer('data.sfgov.org')
consumer.query()
.withDataset('vbiu-2p9h')
.limit(2000)
.where(whereString) //D13&D14 licenses are "Motor Fuel Dispencing Facilities" or "Self-Service Motor Fuel Dispensing Facilities"
.getRows()
.on('success', handler)
.on('error', function(error) { console.error(error); });
}
function saveData(rows) {
writeFile('neighborhoods.json', rows);
}
function writeFile(filename, jsonData){
var str = JSON.stringify(jsonData)
fs.writeFile(filename, str, function(err){
if (err) { return console.error(err); }
console.log("The file was saved as", filename);
});
}
function loadFromLocal(filename){
let data = fs.readFileSync(filename)
data = JSON.parse(data)
return data
}
function analyzeData(rows){
//place businesses in supervisor districts, if possible
rows = rows.map(function(business){
if (business.supervisor_district === undefined){
business.supervisor_district = findSupervisorDistrict(business)
}
return business
})
// writeFile('data/inDistrict.json', rows)
let byDistrict = d3.nest().key(function(d){return d.supervisor_district}).object(rows)
writeFile('districts.json', byDistrict)
}
function lookForNulls(rows){
open = []
closed = []
rows.forEach((business)=>{
if (!business.dba_end_date || !business.location_end_date){
open.push( business )
console.log(business.dba_end_date);
console.log(business.location_end_date);
}else {
closed.push( business )
console.log(business.dba_end_date);
console.log(business.location_end_date);
}
})
console.log(open.length + ' open')
console.log(closed.length + ' closed');
writeFile('opens.json', open)
writeFile('closeds.json', closed)
}
// let closed = data.filter(function(el){
// return el.location_end_date != undefined
// })
function findSupervisorDistrict(business){
if (business.location === undefined) {return undefined}
let pt = turf.point(business.location.coordinates)
let district = supervisorGeo.features.find(function(geo){
return turf.inside(pt,geo)
})
return district.properties.supervisor
}