-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_geocode.js
More file actions
54 lines (42 loc) · 1.5 KB
/
batch_geocode.js
File metadata and controls
54 lines (42 loc) · 1.5 KB
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
import { bulkGeocode } from "@esri/arcgis-rest-geocoding";
import { ApiKeyManager } from "@esri/arcgis-rest-request";
import * as fs from 'fs';
import dotenv from "dotenv"
dotenv.config()
//Esri auth
const apiKey = process.env.KEY;
const authentication = ApiKeyManager.fromKey(apiKey);
//Read csv file and build object to pass in geocode function
const data = fs.readFileSync('data/Base_inicial_geocodingv2.csv').toString();
const headers = data.slice(0, data.indexOf("\r\n")).split(',')
const rows = data.slice(data.indexOf("\r\n") + 1).split("\r\n");
const total_addresses = rows.map(function (row) {
const values = row.split(',');
const element = headers.reduce(function (object, header, index) {
object[header] = values[index];
return object;
}, {});
return element;
});
total_addresses.forEach(function (entry) {
entry.OBJECTID = parseInt(entry.OBJECTID, 10);
});
let i = 9
//Define amount of addresses to process
// bulk Geocode accepts max batch of 1000 elements
const addresses = total_addresses.slice(i * 1000, (i * 1000) + 1000)
//Use the geocode function and save results to json file
bulkGeocode({ addresses, authentication: authentication })
.then((response) => {
var json_response = JSON.stringify(response);
fs.writeFile('results/geocode_results' + String(i) + '.json', json_response, 'utf8', err => {
if (err) {
console.error(err);
} else {
console.log("file written successfully");
}
});
})
.catch((err) => {
console.log(err);
});