-
Notifications
You must be signed in to change notification settings - Fork 7
/
Socrata.js
625 lines (590 loc) · 21.2 KB
/
Socrata.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
var request = require('request')
var async = require('async')
var JSONStream = require('JSONStream')
var es = require('event-stream')
var ogr2ogr = require('ogr2ogr')
var Socrata = function (koop) {
var socrata = koop.BaseModel(koop)
// set up this object to hold info on what is currently processing
var processing = {}
socrata.pageLimit = 10000
socrata.resourcePath = '/resource/'
socrata.viewPath = '/views/'
socrata.expirationPeriod = (1000 * 60 * 60 * 24)
if (koop.config && koop.config.socrata) {
if (koop.config.socrata.pageLimit) {
socrata.pageLimit = koop.config.socrata.pageLimit
}
if (koop.config.socrata.token) {
socrata.token = koop.config.socrata.token
}
}
// adds a service to the koop.Cache.db
// needs a host, generates an id
socrata.register = function (id, host, callback) {
var type = 'socrata:services'
if (!id && host) return callback(new Error('Try POSTing {"host":"url", "id":"yourId"}'))
koop.Cache.db.serviceRegister(type, {'id': id, 'host': host}, function (err, success) {
callback(err, id)
})
}
socrata.remove = function (id, callback) {
koop.Cache.db.serviceRemove('socrata:services', parseInt(id, 0) || id, callback)
}
// get service by id, no id == return all
socrata.find = function (id, callback) {
koop.Cache.db.serviceGet('socrata:services', parseInt(id, 0) || id, function (err, res) {
if (err) {
callback('No datastores have been registered with this provider yet. Try POSTing {"host":"url", "id":"yourId"} to /socrata', null)
} else {
callback(null, res)
}
})
}
// got the service and get the item
socrata.getResource = function (host, hostId, id, options, callback) {
var type = 'Socrata'
var key = id
var table = [type, id, (options.layer || 0)].join(':')
var urlId
var meta = {}
var firstRow = []
var errors = []
var locationField
// test id for '!' character indicating presence of a column name and handle
if (id.indexOf('!') !== -1) {
urlId = id.split('!')[0]
locationField = id.split('!')[1]
} else {
urlId = id
}
// request queue for paging data in a controlled way
var pageQueue = async.queue(function (url, callback) {
koop.log.info('Processing: ' + url)
socrata.processStream(socrata.getPage(url), meta, function (err, geojson) {
if (err) {
callback('Failed while processing a page of ' + table + '. ' + err)
} else {
koop.Cache.insertPartial(type, key, geojson, 0, function (err, success) {
if (err) {
callback('Failed while inserting a page of ' + table + '. ' + err)
} else if (success) {
callback(null)
}
})
}
})
}, 4)
pageQueue.drain = function () {
koop.Cache.getInfo(table, function (err, info) {
if (err) {
koop.log.error('Could not get info of ' + table, err)
info = {}
}
delete info.status
koop.Cache.updateInfo(table, info, function () {
koop.log.info('Finished paging ' + table)
// remove this dataset from the global processing object
delete processing[host + id]
})
})
}
// request queue for gathering info on a dataset
var infoQueue = async.queue(function (job, cb) {
job.task(job.host, job.urlId, cb)
}, 3)
// when the infoQueue drains we will use the feedback from the first three socrata API calls to decide how to proceed
infoQueue.drain = function () {
// if the dataset is only one page long and getting the first row fails
// we can still grab it by requesting the whole dataset in one go
if (!errors.length) {
// proceed along the happy path
socrata.ingestResource(host, urlId, meta, firstRow, pageQueue, function (err, info) {
if (err) {
koop.log.error(err)
} else {
koop.log.info(info)
}
})
} else if (errors.length === 1 && errors[0].split('::')[0] === 'first') {
socrata.ingestResourceFallback(host, urlId, meta, function (err, info) {
if (err) {
koop.log.error(err)
} else {
koop.log.info(info)
}
})
// maybe we have errors because this is a blob file, we can just request the whole thing if it's a zip
} else if (errors.length && meta.blobFilename && meta.blobFilename.split('.')[1] === 'zip') {
socrata.processZip(host, urlId, function (err, geojson) {
if (err) {
koop.log.error(err)
} else {
socrata.insert(key, meta, geojson, function (err, success) {
if (err) {
koop.log.error('Processing failed for zip resource: ' + 'Socrata:' + key + ':0', err)
// handle err
} else {
koop.log.info('Processing completed for zip resource: ' + 'Socrata:' + key + ':0')
}
})
}
})
// if count is the only thing that failed we can still try to grab the dataset in one go
} else if (errors.length === 1 && errors[0].split('::')[0] === 'count') {
socrata.ingestResourceFallback(host, urlId, meta, function (err, info) {
if (err) {
koop.log.error(err)
} else {
koop.log.info(info)
}
})
} else {
socrata.setFail(table, errors, function () {
koop.log.info('Processing failed on ' + 'Socrata:' + key + ':0', errors.join(', '))
delete processing[host + id]
})
}
}
koop.Cache.get(type, key, options, function (err, entry) {
var cacheMiss = err ||
(entry.length && entry[0].status === 'processing') ||
(entry.length && entry[0].info && entry[0].info.status === 'processing')
if (cacheMiss) {
koop.Cache.getInfo(table, function (error, info) {
if (error && !processing[host + key]) {
// we don't have the data and it's not processing return processing and make a new request
processing[host + id] = true
callback(null, [{status: 'processing'}])
infoQueue.push({
task: socrata.getRowCount,
host: host,
urlId: urlId
}, function (err, rowCount) {
if (err) {
koop.log.error('Could not get rowCount. ' + err)
errors.push(err)
} else {
meta.rowCount = rowCount
}
}
)
infoQueue.push({
task: socrata.getMeta,
host: host,
urlId: urlId
}, function (err, info) {
if (err) {
koop.log.error('Could not get metadata. ' + err)
errors.push(err)
} else {
meta.name = info.name
meta.locationField = locationField || info.locationField
meta.updated_at = info.updated_at
meta.fields = info.fields
meta.field_types = info.field_types || []
meta.blobFilename = info.blobFilename
}
}
)
infoQueue.push({
task: socrata.getFirst,
host: host,
urlId: urlId
}, function (err, data) {
if (err) {
koop.log.error('Could not get first row. ' + err)
errors.push(err)
} else {
firstRow = data
}
}
)
} else if (error & processing[host + key]) {
// we don't have any info yet, but it is processing just callback
callback(null, [{status: 'processing'}])
} else {
// we have info either an error or some progress
callback(null, [info])
}
})
} else {
// we have the data or info so send it back
callback(null, entry)
// after data has been sent, check for expiration
socrata.checkExpiration(host, urlId, function (err, expired) {
if (err) {
koop.log.error('Unable to check updated date for: ' + host + urlId)
} else if (expired) {
koop.log.info('Cache expired, regenerating. ' + host + urlId)
}
})
}
})
}
socrata.getRowCount = function (host, id, callback) {
var countUrl = host + socrata.resourcePath + id + '.json?$select=count(*)'
var options = {url: countUrl, gzip: true}
if (socrata.token) {
options.headers = {'X-App-Token': socrata.token}
}
request(options, function (err, res, body) {
if (err) {
callback(err)
} else if (res.statusCode !== 200) {
callback('count::' + options.url + '::' + res.statusCode, null)
} else {
var rowCount
try {
rowCount = JSON.parse(body)[0].count
} catch (e) {
err = 'Could not parse count JSON'
}
callback(err, rowCount)
}
})
}
socrata.getMeta = function (host, id, callback) {
var meta = {}
var metaUrl = host + socrata.viewPath + id + '.json'
var options = {url: metaUrl, gzip: true}
request(options, function (err, res, body) {
if (err) {
callback(err)
} else if (res.statusCode !== 200) {
callback('meta::' + options.url + '::' + res.statusCode)
} else {
var response = JSON.parse(body)
meta.blobFilename = response.blobFilename
meta.updated_at = new Date(res.headers['last-modified'])
meta.name = response.name
meta.fields = []
meta.field_types = []
response.columns.forEach(function (col) {
meta.fields.push(col.fieldName)
meta.field_types.push(col.dataTypeName)
if (col.dataTypeName === 'location') {
meta.location_field = col.fieldName
}
})
callback(err, meta)
}
})
}
socrata.getFirst = function (host, id, callback) {
var firstUrl = host + socrata.resourcePath + id + '.json?$order=:id&$limit=1'
var options = {url: firstUrl, gzip: true}
if (socrata.token) {
options.headers = {'X-App-Token': socrata.token}
}
request(options, function (err, res, body) {
if (err) {
callback(err)
} else if (res.statusCode !== 200) {
callback('first::' + options.url + '::' + res.statusCode)
} else {
callback(err, JSON.parse(body))
}
})
}
socrata.buildPages = function (host, id, rowCount) {
var urls = []
var pageCount = Math.ceil((rowCount - 1) / socrata.pageLimit)
for (var p = 0; p < pageCount; p++) {
var page = host + socrata.resourcePath + id + '.json?$order=:id&$limit=' + socrata.pageLimit + '&$offset=' + ((p * socrata.pageLimit) + 1)
urls[p] = page
}
return urls
}
socrata.getPage = function (pageUrl) {
var options = {url: pageUrl, gzip: true}
if (socrata.token) {
options.headers = {'X-App-Token': socrata.token}
}
// Return the stream so it can be piped to a parser
return request(options)
}
socrata.processZip = function (host, id, callback) {
var zipUrl = host + '/api/geospatial/' + id + '?method=export&format=Original'
var options = {url: zipUrl, gzip: true}
if (socrata.token) {
options.headers = {'X-App-Token': socrata.token}
}
socrata.ogrZip(request(options), callback)
// to do error handling
}
socrata.ogrZip = function (stream, callback) {
ogr2ogr(stream, 'ESRI Shapefile').exec(function (err, data) {
callback(err, data)
})
}
socrata.processStream = function (dataStream, meta, callback) {
var geojson = { type: 'FeatureCollection', features: [] }
dataStream
.pipe(JSONStream.parse('*'))
.pipe(es.map(function (data, cb) {
socrata.toGeojson([data], meta, function (err, converted) {
if (err) {
cb(err)
}
geojson.features.push(converted.features[0])
cb()
})
}))
.on('error', function (err) {
callback(err)
})
.on('end', function () {
callback(null, geojson)
})
}
socrata.ingestResource = function (host, id, meta, firstRow, pageQueue, callback) {
socrata.toGeojson(firstRow, meta, function (err, geojson) {
if (err) {
callback('Failed to parse the first row at: ' + host + id + '. ' + err)
// update status as failed
} else {
meta.status = 'processing'
socrata.insert(id, meta, geojson, function (err, success) {
if (err) {
callback('First row insert failed for: ' + host + id + '. ' + err)
} else {
var pages = socrata.buildPages(host, id, meta.rowCount)
pageQueue.push(pages, function (err) {
if (err) {
callback(err)
} else {
callback(null, 'Beginning to page through ' + host + '/resource' + id + ' ' + pages.length + ' Pages.')
}
})
}
})
}
})
}
socrata.ingestResourceFallback = function (host, id, meta, callback) {
var url = host + socrata.resourcePath + id + '.json'
socrata.processStream(socrata.getPage(url), meta, function (err, geojson) {
if (err) {
callback(err)
} else {
meta.status = 'complete'
socrata.insert(id, meta, geojson, function (err, success) {
if (err) {
callback('Fallback method failed for: ' + url + '. ' + err)
} else {
callback(null, 'Fallback method succeeded for: ' + url)
// remove this dataset from the processing object
delete processing[host + id]
}
})
}
})
}
socrata.toGeojson = function (json, meta, callback) {
if (!json || !json.length) {
callback('Error converting data to GeoJSON: JSON not returned from Socrata or blank JSON returned', null)
} else {
var geojson = { type: 'FeatureCollection', features: [] }
var geojsonFeature
var newFields = []
var locationField = meta.location_field
var fields = meta.fields
var fieldTypes = meta.field_types
json.forEach(function (feature, i) {
var lat, lon
geojsonFeature = { type: 'Feature', geometry: {}, id: i + 1 }
if (feature && locationField && feature[locationField]) {
lon = parseFloat(feature[locationField].longitude)
lat = parseFloat(feature[locationField].latitude)
if ((lon < -180 || lon > 180) || (lat < -90 || lat > 90)) {
geojsonFeature.geometry = null
geojsonFeature.properties = feature
geojson.features.push(geojsonFeature)
} else {
geojsonFeature.geometry.coordinates = [lon, lat]
geojsonFeature.geometry.type = 'Point'
delete feature.location
geojsonFeature.properties = feature
geojson.features.push(geojsonFeature)
}
} else if (feature && feature.latitude && feature.longitude) {
lon = parseFloat(feature.longitude)
lat = parseFloat(feature.latitude)
if ((lon < -180 || lon > 180) || (lat < -90 || lat > 90)) {
geojsonFeature.geometry = null
geojsonFeature.properties = feature
geojson.features.push(geojsonFeature)
} else {
geojsonFeature.geometry.coordinates = [lon, lat]
geojsonFeature.geometry.type = 'Point'
geojsonFeature.properties = feature
geojson.features.push(geojsonFeature)
}
} else {
geojsonFeature.geometry = null
geojsonFeature.properties = feature
geojson.features.push(geojsonFeature)
}
// make sure each feature has flattened object props
fields.forEach(function (f, j) {
if (f.substring(0, 1) !== ':') {
if (typeof geojson.features[i].properties[f] === 'object') {
for (var v in geojson.features[i].properties[f]) {
var newAttr = f + '_' + v
geojson.features[i].properties[newAttr] = geojson.features[i].properties[f][v]
newFields.push(newAttr)
}
delete geojson.features[i].properties[f]
}
}
// ensure all fields present, if undefined set to null
if (geojson.features[i].properties[f] === undefined) {
geojson.features[i].properties[f] = null
}
// type casting for numbers
if (fieldTypes[j] === 'number' && geojson.features[i].properties[f] !== null) {
geojson.features[i].properties[f] = parseFloat(geojson.features[i].properties[f])
}
})
})
// 2nd loop over the data to ensure all new fields are present in each feature
if (newFields && newFields.length) {
geojson.features.forEach(function (feature) {
newFields.forEach(function (field) {
if (!feature.properties[field]) {
feature.properties[field] = null
}
})
})
}
callback(null, geojson)
}
}
// compares the sha on the cached data and the hosted data
// this method name is special reserved name that will get called by the cache model
socrata.checkExpiration = function (host, id, callback) {
var table = ['Socrata', id, 0].join(':')
koop.Cache.getInfo(table, function (err, info) {
if (err) {
callback(err)
}
// no info means it's the first request for this resource, no need to check cache
if (info) {
// check if enough time has passed to call to Socrata
// convert info fields into date objects because they are stored as strings in the cache
if ((new Date() - new Date(info.checked_at)) > socrata.expirationPeriod) {
socrata.getMeta(host, id, function (err, meta) {
if (err) {
callback(err)
}
// now check if the resource expired
if (meta.updated_at > new Date(info.updated_at)) {
// it's expired so remove the item and go fetch it again
socrata.dropItem(host, id, 0, function (err, success) {
if (err) {
callback(err)
}
socrata.getResource(host, id, 0, {}, function (err, res) {
if (err) {
callback(err)
}
callback(null, true)
})
})
} else {
callback(null, false)
}
})
} else {
callback(null, false)
}
}
})
}
// drops the item from the cache
socrata.dropItem = function (host, itemId, options, callback) {
var dir = ['socrata', host, itemId].join(':')
var errors = []
koop.Cache.remove('Socrata', itemId, options, function (err, res) {
if (err) errors.push(err)
koop.files.removeDir('files/' + dir, function (err, res) {
if (err) errors.push(err)
koop.files.removeDir('tiles/' + dir, function (err, res) {
if (err) errors.push(err)
koop.files.removeDir('thumbs/' + dir, function (err, res) {
if (err) errors.push(err)
callback(errors.join(', '), true)
})
})
})
})
}
socrata.updateInfo = function (table, newInfo, callback) {
koop.Cache.getInfo(table, function (err, info) {
if (err) {
info = {}
}
info.checked_at = new Date()
// we don't want to save any status on completed datasets
if (newInfo.status !== 'complete') {
info.status = newInfo.status
}
if (newInfo.errors) {
info.errors = newInfo.errors
}
if (newInfo.field_types) {
info.field_types = newInfo.field_types
}
if (newInfo.field_types) {
info.fields = newInfo.fields
}
koop.Cache.updateInfo(table, info, function (err, success) {
if (err) {
callback(err)
}
callback(null, true)
})
})
}
socrata.insert = function (key, meta, geojson, callback) {
var table = ['Socrata', key, 0].join(':')
geojson.name = meta.name
geojson.location_field = meta.location_field
geojson.updated_at = meta.updated_at
koop.Cache.insert('Socrata', key, geojson, 0, function (err, success) {
if (err) {
callback(err, false)
} else {
meta.checked_at = new Date()
socrata.updateInfo(table, meta, function (err, success) {
if (err) {
callback(err, false)
} else {
callback(null, true)
}
})
}
})
}
socrata.setFail = function (table, errors, callback) {
var feature = { type: 'Feature', geometry: null, id: 0 }
var geojson = { type: 'FeatureCollection', features: [feature] }
var key = table.split(':')[1]
// create a table with blank features so we can attach a failed status
koop.Cache.insert('Socrata', key, geojson, 0, function (err, success) {
if (err) {
callback(err)
}
var info = {}
info.status = 'processing'
info.errors = errors
socrata.updateInfo(table, info, callback)
})
}
socrata.getCount = function (key, options, callback) {
koop.Cache.getCount(key, options, callback)
}
return socrata
}
module.exports = Socrata