This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
forked from zirman/vb-incidents-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassage.js
152 lines (115 loc) · 3.79 KB
/
massage.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
/*jshint devel: true, node: true, bitwise: false, camelcase: true, curly: true,
eqeqeq: true, forin: true, freeze: true, immed: true, newcap: true,
noarg: true, noempty: true, nonbsp: false, nonew: true, plusplus: true,
quotmark: single, undef: true, unused: vars, strict: true, trailing: true,
white: true, onevar: true, indent: 2, maxparams: 3, maxdepth: 3, maxlen: 80 */
'use strict';
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var http = require('http');
// Connection URL
var url = 'mongodb://localhost:27017/incidentreports';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log('Connected correctly to server');
var collection = db.collection('incidents');
collection.find().toArray(function(err, incidents) {
assert.equal(null, err);
incidents.forEach(function (incident, index) {
if (index !== 1) {
return;
}
if (! incident.geoJson) {
getGeoJson(incident.Location, function (geoJson) {
collection.update({
CaseNo: incident.CaseNo
}, {
$set: {
geoJson: geoJson
}
}, function(err, result) {
assert.equal(err, null);
assert.equal(1, result.result.n);
console.log('Updated geoJson:', geoJson);
});
});
} else {
console.log(incident);
}
});
});
function getGeoJson(location, callback) {
console.log('getGeoJson');
var address =
parseInt(location.BlockNo, 10) + ' ' + location.Street + ', ' +
location.City + ', ' + location.State + ' ' + location.Zip;
return forwardGeolocation(address, callback);
}
function forwardGeolocation(address, callback) {
console.log('forwardGeolocation');
var req = http.request(
'http://heliosapi.homes.com/v1/location/geocode?api_key=1-hdca-B6CIa13hYOvKIzda3ucN3v&location=' + address, function (res) {
if (res.statusCode !== 200) {
console.log('error code: ' + res.statusCode);
callback();
}
res.setEncoding('utf8');
res.on('data', function (chunk) {
var json = JSON.parse(chunk);
callback({
type: 'Point',
coordinates: [
json.location.center.lon,
json.location.center.lat
]
});
});
//console.log('STATUS: ' + res.statusCode);
//console.log('HEADERS: ' + JSON.stringify(res.headers.body));
/*
var location = json.location;
return {
searchType: location.searchType,
city: location.city,
state: location.state,
zip: location.zip,
formattedAddress: location.formattedAddress,
center: getCenter(location.center),
boundingBox: getBoundingBox(location.bbox),
bingCenter: getCenter(location.bing_center),
bingBoundingBox: getBoundingBox(location.bing_bbox),
mongoCenter: getCenter(location.mongo_center),
mongoBoundingBox: getBoundingBox(location.mongo_bbox)
};
function getCenter(obj) {
if (! _.isObject(obj)) {
return obj;
}
return {
latitude: getNumber(obj.lat),
longitude: getNumber(obj.lon)
};
}
function getBoundingBox(obj) {
if (typeof obj === 'object') {
return obj;
}
return {
minLatitude: getNumber(obj.lat[0]),
maxLatitude: getNumber(obj.lat[1]),
minLongitude: getNumber(obj.lon[0]),
maxLongitude: getNumber(obj.lon[1])
};
}
function getNumber(value) {
if (typeof value === 'undefined') {
return value;
}
return Number(value);
}
*/
});
req.end();
}
});