-
Notifications
You must be signed in to change notification settings - Fork 1
/
external_geocoders.js
144 lines (136 loc) · 3.79 KB
/
external_geocoders.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
const fetch = require("node-fetch");
const geocoderUtils = require('./geocoder_utils');
const AbortController = require("abort-controller");
const OSM_TYPES = {
relation : "R",
node : "N",
way : "W"
}
function newTimeout(time){
const controller = new AbortController()
const signal = controller.signal
setTimeout(() => {
controller.abort()
}, time);
return signal;
}
async function arcgis_geocode (loc,geocoderName) {
let signal = newTimeout(GEOCODERS[geocoderName].timeout);
try {
let res = await fetch(buildUrl({
url : GEOCODERS[geocoderName].url,
name : geocoderName,
loc : loc
}),{ signal }).then(checkStatus);
let json = await res.json();
if (json.candidates.length > 0) {
let normalizedRes = geocoderUtils.normalize(loc,geocoderName, json)
return normalizedRes;
} else {
throw new Error('No candidates found');
}
} catch(err) {
console.log(`Failed [${geocoderName}] geocoding for [${loc}] - [${err}]`.red);
}
}
async function osm_geocode (loc,geocoderName) {
let signal = newTimeout(GEOCODERS[geocoderName].timeout);
try {
let res = await fetch(buildUrl({
url : GEOCODERS[geocoderName].url,
loc : loc,
name : geocoderName
}),{ signal }).then(checkStatus);
let candidates = await res.json();
if (candidates.length > 0) {
let json = candidates[0];
let resAdmArea = await fetch(buildUrl({
url : GEOCODERS[geocoderName].url.replace("search", "details"),
name : geocoderName,
qs : {
osmtype : OSM_TYPES[json["osm_type"]],
osmid : json["osm_id"],
format : 'json'
}
})).then(checkStatus);
let resAdmAreajson = await resAdmArea.json();
let normalizedRes = geocoderUtils.normalize(loc,geocoderName, {...resAdmAreajson,...json})
return normalizedRes;
} else {
throw new Error('No candidates found');
}
} catch(err) {
console.log(`Failed [${geocoderName}] geocoding for [${loc}] - [${err}]`.red);
}
}
const GEOCODERS = {
arcgis : {
name : 'arcgis',
url : 'https://cloudlab.esri.es/server/rest/services/ESP_AdminPlaces/GeocodeServer/findAddressCandidates',
timeout : 1000,
qs : function(location) {
return {
SingleLineCityName: location,
f: 'json',
outFields: '*',
outSR: '{"wkid":4326,"latestWkid":4326}',
maxLocations: '1'
}
},
geocode : function(loc) {
return arcgis_geocode(loc,"arcgis")
}
},
osm : {
name : 'nominatim',
url : 'https://nominatim.openstreetmap.org/search',
timeout : 7000,
qs : function(location) {
return {
q: location,
email: 'rauljimenezortega@gmail.com',
limit: 1,
format: 'json',
countrycodes: 'ES'
}
},
geocode : function(loc) {
return osm_geocode(loc,"osm");
}
},
arcgisGlobal : {
name : 'arcgisGlobal',
url : 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates',
timeout : 30000,
qs : function(location) {
return {
SingleLineCityName: location,
f: 'json',
outFields: '*',
outSR: '{"wkid":4326,"latestWkid":4326}',
maxLocations: '1',
sourceCountry: 'ES'
}
},
geocode : function(loc) {
return arcgis_geocode(loc,"arcgisGlobal")
}
}
};
function checkStatus(res) {
if (res.ok) { // res.status >= 200 && res.status < 300
return res;
} else {
throw new Error(res.statusText);
}
}
function buildUrl(obj) {
let url = new URL(obj.url);
let params = obj.hasOwnProperty("qs")
? obj.qs
: GEOCODERS[obj.name].qs(obj.loc);
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]));
console.log(`Fetching attempt url : [${url.href}]`.yellow);
return url;
}
module.exports = GEOCODERS;