-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathbulk.ts
112 lines (103 loc) · 3.13 KB
/
bulk.ts
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
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
cleanUrl,
ISpatialReference,
IPoint
} from "@esri/arcgis-rest-request";
import {
ARCGIS_ONLINE_BULK_GEOCODING_URL,
IEndpointOptions
} from "./helpers.js";
// It would be better if doc did not display these properties in alphabetical order
export interface IAddressBulk {
/**
* A unique id must be passed along for each individual address.
*/
OBJECTID: number;
address?: string;
address2?: string;
address3?: string;
neighborhood?: string;
city?: string;
subregion?: string;
/**
* The World Geocoding Service considers US states regions.
*/
region?: string;
postal?: string | number;
postalExt?: string | number;
countryCode?: string;
}
export interface IBulkGeocodeOptions extends IEndpointOptions {
addresses: IAddressBulk[];
}
export interface IBulkGeocodeResponse {
spatialReference: ISpatialReference;
locations: Array<{
address: string;
location?: IPoint; // candidates with a score of 0 wont include a location
score: number;
attributes: object;
}>;
}
/**
* Used to geocode a [batch](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-geocode-addresses.htm) of addresses.
*
* ```js
* import { bulkGeocode } from '@esri/arcgis-rest-geocoding';
* import { ApplicationCredentialsManager } from '@esri/arcgis-rest-request';
*
* const addresses = [
* { "OBJECTID": 1, "SingleLine": "380 New York Street 92373" },
* { "OBJECTID": 2, "SingleLine": "1 World Way Los Angeles 90045" }
* ];
*
* bulkGeocode({ addresses, authentication: session })
* .then((response) => {
* response.locations[0].location; // => { x: -117, y: 34, spatialReference: { wkid: 4326 } }
* });
* ```
*
* @param requestOptions - Request options to pass to the geocoder, including an array of addresses and authentication session.
* @returns A Promise that will resolve with the data from the response. The spatial reference will be added to address locations unless `rawResponse: true` was passed.
*/
export function bulkGeocode(
requestOptions: IBulkGeocodeOptions // must POST, which is the default
): Promise<IBulkGeocodeResponse> {
const options: IBulkGeocodeOptions = {
endpoint: ARCGIS_ONLINE_BULK_GEOCODING_URL,
params: {},
...requestOptions
};
options.params.addresses = {
records: requestOptions.addresses.map((address) => {
return { attributes: address };
})
};
// the SAS service does not support anonymous requests
if (
!requestOptions.authentication &&
options.endpoint === ARCGIS_ONLINE_BULK_GEOCODING_URL
) {
return Promise.reject(
"bulk geocoding using the ArcGIS service requires authentication"
);
}
return request(
`${cleanUrl(options.endpoint)}/geocodeAddresses`,
options
).then((response) => {
if (options.rawResponse) {
return response;
}
const sr = response.spatialReference;
response.locations.forEach(function (address: { location: IPoint }) {
if (address.location) {
address.location.spatialReference = sr;
}
});
return response;
});
}