forked from veltman/csvgeocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
110 lines (82 loc) · 2.25 KB
/
handlers.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
var csv = require("dsv")(",");
module.exports = {
google: function(body) {
var response = JSON.parse(body);
//Success, return a lat/lng object
if (response.results && response.results.length) {
return response.results[0].geometry.location;
}
//No match, return a string
if (response.status === "ZERO_RESULTS" || response.status === "OK") {
return "NO MATCH";
}
//Other error, return a string
return response.status + (response.error_message && response.error_message.length ? ": " + response.error_message : "");
},
mapbox: function(body) {
var response = JSON.parse(body);
if (response.features === undefined) {
return response.message;
} else if (!response.features.length) {
return "NO MATCH";
}
return {
lat: response.features[0].center[1],
lng: response.features[0].center[0]
};
},
mapzen: function(body) {
var response = JSON.parse(body);
if (response.features === undefined) {
return response.message;
} else if (!response.features.length) {
return "NO MATCH";
}
return {
lat: response.features[0].geometry.coordinates[1],
lng: response.features[0].geometry.coordinates[0]
};
},
tamu: function(body) {
var parsed;
if (!body.length) {
return "NO RESPONSE BODY RETURNED, CHECK YOUR API KEY";
}
try {
parsed = csv.parseRows(body);
} catch(e) {
return "ERROR PARSING RESPONSE: "+body;
}
if (parsed[0].length < 5) {
return "UNEXPECTED RESPONSE FORMAT FROM TAMU GEOCODER: "+csv.formatRows([parsed[0]]);
}
if (!parsed.length || +parsed[0][2] !== 200 || !+parsed[0][3] || !+parsed[0][4]) {
return "NO MATCH";
}
return {
lat: parsed[0][3],
lng: parsed[0][4]
}
},
osm: function(body) {
var parsed;
if (!body.length) {
return "NO RESPONSE BODY RETURNED, CHECK YOUR API KEY";
}
try {
parsed = JSON.parse(body);
} catch(e) {
return "ERROR PARSING RESPONSE: "+body;
}
if (!Array.isArray(parsed)) {
return "UNEXPECTED RESPONSE: "+body;
}
if (!parsed.length) {
return "NO MATCH";
}
return {
lat: parsed[0].lat,
lng: parsed[0].lon
};
}
};