-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathgeocode.ts
179 lines (167 loc) · 4.83 KB
/
geocode.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
cleanUrl,
appendCustomParams,
IExtent,
ISpatialReference,
IPoint,
warn
} from "@esri/arcgis-rest-request";
import { ARCGIS_ONLINE_GEOCODING_URL, IEndpointOptions } from "./helpers.js";
import { arcgisToGeoJSON } from "@terraformer/arcgis";
export interface IGeocodeOptions extends IEndpointOptions {
/**
* use this if all your address info is contained in a single string.
*/
singleLine?: string;
address?: string;
address2?: string;
address3?: string;
neighborhood?: string;
city?: string;
subregion?: string;
outFields?: "*" | string[];
/**
* The World Geocoding Service expects US states to be passed in as a 'region'.
*/
region?: string;
postal?: string | number;
postalExt?: string | number;
countryCode?: string;
/**
* You can create an autocomplete experience by making a call to suggest with partial text and then passing through the magicKey and complete address that are returned to geocode.
*
* ```js
* import { suggest, geocode } from '@esri/arcgis-rest-geocoding';
*
* suggest("LAX")
* .then((response) => {
* geocode({
* singleLine: response.suggestions[1].text,
* magicKey: response.suggestions[0].magicKey
* })
* })
* ```
*/
magicKey?: string;
}
export interface IGeocodeResponse {
spatialReference: ISpatialReference;
candidates: Array<{
address: string;
location: IPoint;
extent?: IExtent;
score: number;
attributes: object;
}>;
geoJson?: {
type: string;
features: Array<{
type: string;
geometry: object;
properties: any;
}>;
};
}
/**
* Used to determine the location of a single address or point of interest. See the [REST Documentation](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-find-address-candidates.htm) for more information.
*
* ```js
* import { geocode } from '@esri/arcgis-rest-geocoding';
*
* geocode("LAX")
* .then((response) => {
* response.candidates[0].location; // => { x: -118.409, y: 33.943, spatialReference: ... }
* });
*
* geocode({
* address: "1600 Pennsylvania Ave",
* postal: "20500",
* countryCode: "USA"
* })
* .then((response) => {
* response.candidates[1].location; // => { x: -77.036533, y: 38.898719, spatialReference: ... }
* });
* ```
*
* @param address String representing the address or point of interest or RequestOptions to pass to the endpoint.
* @returns A Promise that will resolve with address candidates for the request. The spatial reference will be added to candidate locations and extents unless `rawResponse: true` was passed.
*/
export function geocode(
address: string | IGeocodeOptions
): Promise<IGeocodeResponse> {
let options: IGeocodeOptions = {};
let endpoint: string;
if (typeof address === "string") {
options.params = { singleLine: address };
endpoint = ARCGIS_ONLINE_GEOCODING_URL;
} else {
endpoint = address.endpoint || ARCGIS_ONLINE_GEOCODING_URL;
options = appendCustomParams<IGeocodeOptions>(
address,
[
"singleLine",
"address",
"address2",
"address3",
"neighborhood",
"city",
"subregion",
"region",
"postal",
"postalExt",
"countryCode",
"outFields",
"magicKey"
],
{ params: { ...address.params } }
);
if (options.params.postal && typeof options.params.postal === "number") {
warn(
"The postal code should be a string. " +
"Issues can arise when using it as a number, especially if they start with zero."
);
}
}
// add spatialReference property to individual matches
return request(`${cleanUrl(endpoint)}/findAddressCandidates`, options).then(
(response) => {
if (typeof address !== "string" && address.rawResponse) {
return response;
}
const sr: ISpatialReference = response.spatialReference;
response.candidates.forEach(function (candidate: {
location: IPoint;
extent?: IExtent;
}) {
candidate.location.spatialReference = sr;
if (candidate.extent) {
candidate.extent.spatialReference = sr;
}
});
// geoJson
if (sr.wkid === 4326) {
const features = response.candidates.map((candidate: any) => {
return {
type: "Feature",
geometry: arcgisToGeoJSON(candidate.location),
properties: Object.assign(
{
address: candidate.address,
score: candidate.score
},
candidate.attributes
)
};
});
response.geoJson = {
type: "FeatureCollection",
features
};
}
return response;
}
);
}