@nextgis/geocoder 1.16.7 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,454 +1,205 @@
1
- /** Bundle of @nextgis/geocoder; version: 1.16.7; author: NextGIS */
1
+ /** Bundle of @nextgis/geocoder; version: 1.17.0; author: NextGIS */
2
2
  import CancelablePromise from '@nextgis/cancelable-promise';
3
3
  import { objectAssign, fixUrlStr } from '@nextgis/utils';
4
4
  import NgwConnector from '@nextgis/ngw-connector';
5
5
 
6
- /******************************************************************************
7
- Copyright (c) Microsoft Corporation.
8
-
9
- Permission to use, copy, modify, and/or distribute this software for any
10
- purpose with or without fee is hereby granted.
11
-
12
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
13
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
14
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
15
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
16
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
17
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
18
- PERFORMANCE OF THIS SOFTWARE.
19
- ***************************************************************************** */
20
- /* global Reflect, Promise */
21
-
22
- var extendStatics = function(d, b) {
23
- extendStatics = Object.setPrototypeOf ||
24
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
25
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
26
- return extendStatics(d, b);
27
- };
28
-
29
- function __extends(d, b) {
30
- if (typeof b !== "function" && b !== null)
31
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
32
- extendStatics(d, b);
33
- function __() { this.constructor = d; }
34
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
35
- }
36
-
37
- var __assign = function() {
38
- __assign = Object.assign || function __assign(t) {
39
- for (var s, i = 1, n = arguments.length; i < n; i++) {
40
- s = arguments[i];
41
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
42
- }
43
- return t;
44
- };
45
- return __assign.apply(this, arguments);
46
- };
47
-
48
- function __generator(thisArg, body) {
49
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
50
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
51
- function verb(n) { return function (v) { return step([n, v]); }; }
52
- function step(op) {
53
- if (f) throw new TypeError("Generator is already executing.");
54
- while (_) try {
55
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
56
- if (y = 0, t) op = [op[0] & 2, t.value];
57
- switch (op[0]) {
58
- case 0: case 1: t = op; break;
59
- case 4: _.label++; return { value: op[1], done: false };
60
- case 5: _.label++; y = op[1]; op = [0]; continue;
61
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
62
- default:
63
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
64
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
65
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
66
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
67
- if (t[2]) _.ops.pop();
68
- _.trys.pop(); continue;
69
- }
70
- op = body.call(thisArg, _);
71
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
72
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
73
- }
74
- }
75
-
76
- function __values(o) {
77
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
78
- if (m) return m.call(o);
79
- if (o && typeof o.length === "number") return {
80
- next: function () {
81
- if (o && i >= o.length) o = void 0;
82
- return { value: o && o[i++], done: !o };
83
- }
84
- };
85
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
86
- }
87
-
88
- function __await(v) {
89
- return this instanceof __await ? (this.v = v, this) : new __await(v);
90
- }
91
-
92
- function __asyncGenerator(thisArg, _arguments, generator) {
93
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
94
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
95
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
96
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
97
- function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
98
- function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
99
- function fulfill(value) { resume("next", value); }
100
- function reject(value) { resume("throw", value); }
101
- function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
102
- }
103
-
104
- function __asyncValues(o) {
105
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
106
- var m = o[Symbol.asyncIterator], i;
107
- return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
108
- function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
109
- function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
6
+ class BaseGeocoder {
7
+ constructor(options) {
8
+ if (options) {
9
+ objectAssign(this, options);
10
+ }
11
+ }
110
12
  }
111
13
 
112
- var BaseGeocoder = /** @class */ (function () {
113
- function BaseGeocoder(options) {
114
- if (options) {
115
- objectAssign(this, options);
116
- }
117
- }
118
- return BaseGeocoder;
119
- }());
120
-
121
- var ID = 0;
122
- var Geocoder = /** @class */ (function (_super) {
123
- __extends(Geocoder, _super);
124
- function Geocoder(options) {
125
- var _this = _super.call(this, options) || this;
126
- _this.providers = [];
127
- if (options && options.providers) {
128
- _this.providers = options.providers;
129
- }
130
- return _this;
131
- }
132
- Geocoder.prototype.abort = function () {
133
- this.providers.forEach(function (x) {
134
- if (x.abort) {
135
- x.abort();
136
- }
137
- });
138
- };
139
- Geocoder.prototype.search = function (query) {
140
- return __asyncGenerator(this, arguments, function search_1() {
141
- var _i, _a, provider, results, results_1, results_1_1, r, e_1_1;
142
- var e_1, _b;
143
- return __generator(this, function (_c) {
144
- switch (_c.label) {
145
- case 0:
146
- _i = 0, _a = this.providers;
147
- _c.label = 1;
148
- case 1:
149
- if (!(_i < _a.length)) return [3 /*break*/, 19];
150
- provider = _a[_i];
151
- _c.label = 2;
152
- case 2:
153
- _c.trys.push([2, 17, , 18]);
154
- results = provider.search(query);
155
- _c.label = 3;
156
- case 3:
157
- _c.trys.push([3, 10, 11, 16]);
158
- results_1 = (e_1 = void 0, __asyncValues(results));
159
- _c.label = 4;
160
- case 4: return [4 /*yield*/, __await(results_1.next())];
161
- case 5:
162
- if (!(results_1_1 = _c.sent(), !results_1_1.done)) return [3 /*break*/, 9];
163
- r = results_1_1.value;
164
- return [4 /*yield*/, __await(__assign(__assign({}, r), { provider: provider, _id: ID++ }))];
165
- case 6: return [4 /*yield*/, _c.sent()];
166
- case 7:
167
- _c.sent();
168
- _c.label = 8;
169
- case 8: return [3 /*break*/, 4];
170
- case 9: return [3 /*break*/, 16];
171
- case 10:
172
- e_1_1 = _c.sent();
173
- e_1 = { error: e_1_1 };
174
- return [3 /*break*/, 16];
175
- case 11:
176
- _c.trys.push([11, , 14, 15]);
177
- if (!(results_1_1 && !results_1_1.done && (_b = results_1.return))) return [3 /*break*/, 13];
178
- return [4 /*yield*/, __await(_b.call(results_1))];
179
- case 12:
180
- _c.sent();
181
- _c.label = 13;
182
- case 13: return [3 /*break*/, 15];
183
- case 14:
184
- if (e_1) throw e_1.error;
185
- return [7 /*endfinally*/];
186
- case 15: return [7 /*endfinally*/];
187
- case 16: return [3 /*break*/, 18];
188
- case 17:
189
- _c.sent();
190
- return [3 /*break*/, 18];
191
- case 18:
192
- _i++;
193
- return [3 /*break*/, 1];
194
- case 19: return [2 /*return*/];
195
- }
196
- });
197
- });
198
- };
199
- Geocoder.prototype.result = function (item) {
200
- if (item.provider) {
201
- return item.provider.result(item);
202
- }
203
- return CancelablePromise.resolve(undefined);
204
- };
205
- return Geocoder;
206
- }(BaseGeocoder));
14
+ let ID = 0;
15
+ class Geocoder extends BaseGeocoder {
16
+ constructor(options) {
17
+ super(options);
18
+ this.providers = [];
19
+ if (options && options.providers) {
20
+ this.providers = options.providers;
21
+ }
22
+ }
23
+ abort() {
24
+ this.providers.forEach((x) => {
25
+ if (x.abort) {
26
+ x.abort();
27
+ }
28
+ });
29
+ }
30
+ async *search(query) {
31
+ for (const provider of this.providers) {
32
+ try {
33
+ const results = provider.search(query);
34
+ for await (const r of results) {
35
+ yield { ...r, provider, _id: ID++ };
36
+ }
37
+ }
38
+ catch (er) {
39
+ //
40
+ }
41
+ }
42
+ }
43
+ result(item) {
44
+ if (item.provider) {
45
+ return item.provider.result(item);
46
+ }
47
+ return CancelablePromise.resolve(undefined);
48
+ }
49
+ }
207
50
 
208
- var BaseProvider = /** @class */ (function (_super) {
209
- __extends(BaseProvider, _super);
210
- function BaseProvider() {
211
- return _super !== null && _super.apply(this, arguments) || this;
212
- }
213
- return BaseProvider;
214
- }(BaseGeocoder));
51
+ class BaseProvider extends BaseGeocoder {
52
+ }
215
53
 
216
- var NgwProvider = /** @class */ (function (_super) {
217
- __extends(NgwProvider, _super);
218
- function NgwProvider(options) {
219
- var _this = _super.call(this, options) || this;
220
- _this.caseInsensitive = true;
221
- _this.searchUrl = 'https://maxim.nextgis.com';
222
- _this.searchResources = [];
223
- _this.requests = [];
224
- _this.connector =
225
- options.connector ||
226
- new NgwConnector({
227
- baseUrl: _this.searchUrl,
228
- });
229
- _this.searchResources = options.searchResources;
230
- return _this;
231
- }
232
- NgwProvider.prototype.abort = function () {
233
- this.requests.forEach(function (x) {
234
- x.cancel();
235
- });
236
- this.requests = [];
237
- };
238
- NgwProvider.prototype.search = function (like) {
239
- return __asyncGenerator(this, arguments, function search_1() {
240
- var _loop_1, this_1, _i, _a, resource;
241
- var _this = this;
242
- return __generator(this, function (_b) {
243
- switch (_b.label) {
244
- case 0:
245
- _loop_1 = function (resource) {
246
- var limit, request, resp, entries, _loop_2, _c, _d, item;
247
- return __generator(this, function (_e) {
248
- switch (_e.label) {
249
- case 0:
250
- limit = resource.limit || 300;
251
- request = this_1.connector.get('feature_layer.store', {
252
- headers: { RANGE: "items=0-".concat(limit) },
253
- }, {
254
- id: resource.id,
255
- like: like,
256
- });
257
- this_1.requests.push(request);
258
- request.catch(function (er) {
259
- console.warn(er);
260
- });
261
- return [4 /*yield*/, __await(request)];
262
- case 1:
263
- resp = _e.sent();
264
- entries = [];
265
- if (resp) {
266
- resp.forEach(function (x) {
267
- entries.push(__assign({ query: like, resourceId: resource.id }, x));
268
- });
269
- }
270
- _loop_2 = function (item) {
271
- return __generator(this, function (_f) {
272
- switch (_f.label) {
273
- case 0:
274
- item.result = function () { return _this.result(item); };
275
- return [4 /*yield*/, __await(item)];
276
- case 1: return [4 /*yield*/, _f.sent()];
277
- case 2:
278
- _f.sent();
279
- return [2 /*return*/];
280
- }
281
- });
282
- };
283
- _c = 0, _d = this_1._items(entries);
284
- _e.label = 2;
285
- case 2:
286
- if (!(_c < _d.length)) return [3 /*break*/, 5];
287
- item = _d[_c];
288
- return [5 /*yield**/, _loop_2(item)];
289
- case 3:
290
- _e.sent();
291
- _e.label = 4;
292
- case 4:
293
- _c++;
294
- return [3 /*break*/, 2];
295
- case 5: return [2 /*return*/];
296
- }
297
- });
298
- };
299
- this_1 = this;
300
- _i = 0, _a = this.searchResources;
301
- _b.label = 1;
302
- case 1:
303
- if (!(_i < _a.length)) return [3 /*break*/, 4];
304
- resource = _a[_i];
305
- return [5 /*yield**/, _loop_1(resource)];
306
- case 2:
307
- _b.sent();
308
- _b.label = 3;
309
- case 3:
310
- _i++;
311
- return [3 /*break*/, 1];
312
- case 4: return [2 /*return*/];
313
- }
314
- });
315
- });
316
- };
317
- NgwProvider.prototype.result = function (item) {
318
- var req = this.connector
319
- .get('feature_layer.feature.item_extent', null, {
320
- id: item.resourceId,
321
- fid: item.featureId,
322
- })
323
- .then(function (x) {
324
- var e = x.extent;
325
- return {
326
- text: item.text,
327
- extent: [e.minLon, e.minLat, e.maxLon, e.maxLat],
328
- };
329
- });
330
- this.requests.push(req);
331
- return req;
332
- };
333
- NgwProvider.prototype._items = function (entries) {
334
- var _this = this;
335
- var items = [];
336
- entries.forEach(function (entry) {
337
- var meta = _this.searchResources.find(function (x) { return x.id === entry.resourceId; });
338
- if (meta) {
339
- items.push({
340
- query: entry.query,
341
- text: entry.label,
342
- featureId: entry.id,
343
- resourceId: entry.resourceId,
344
- });
345
- }
346
- });
347
- return items;
348
- };
349
- return NgwProvider;
350
- }(BaseProvider));
54
+ class NgwProvider extends BaseProvider {
55
+ constructor(options) {
56
+ super(options);
57
+ this.caseInsensitive = true;
58
+ this.searchUrl = 'https://maxim.nextgis.com';
59
+ this.searchResources = [];
60
+ this.requests = [];
61
+ this.connector =
62
+ options.connector ||
63
+ new NgwConnector({
64
+ baseUrl: this.searchUrl,
65
+ });
66
+ this.searchResources = options.searchResources;
67
+ }
68
+ abort() {
69
+ this.requests.forEach((x) => {
70
+ x.cancel();
71
+ });
72
+ this.requests = [];
73
+ }
74
+ async *search(like) {
75
+ for (const resource of this.searchResources) {
76
+ const limit = resource.limit || 300;
77
+ const request = this.connector.get('feature_layer.store', {
78
+ headers: { RANGE: `items=0-${limit}` },
79
+ }, {
80
+ id: resource.id,
81
+ like,
82
+ });
83
+ this.requests.push(request);
84
+ request.catch((er) => {
85
+ console.warn(er);
86
+ });
87
+ const resp = await request;
88
+ const entries = [];
89
+ if (resp) {
90
+ resp.forEach((x) => {
91
+ entries.push({
92
+ query: like,
93
+ resourceId: resource.id,
94
+ ...x,
95
+ });
96
+ });
97
+ }
98
+ for (const item of this._items(entries)) {
99
+ item.result = () => this.result(item);
100
+ yield item;
101
+ }
102
+ }
103
+ }
104
+ result(item) {
105
+ const req = this.connector
106
+ .get('feature_layer.feature.item_extent', null, {
107
+ id: item.resourceId,
108
+ fid: item.featureId,
109
+ })
110
+ .then((x) => {
111
+ const e = x.extent;
112
+ return {
113
+ text: item.text,
114
+ extent: [e.minLon, e.minLat, e.maxLon, e.maxLat],
115
+ };
116
+ });
117
+ this.requests.push(req);
118
+ return req;
119
+ }
120
+ _items(entries) {
121
+ const items = [];
122
+ entries.forEach((entry) => {
123
+ const meta = this.searchResources.find((x) => x.id === entry.resourceId);
124
+ if (meta) {
125
+ items.push({
126
+ query: entry.query,
127
+ text: entry.label,
128
+ featureId: entry.id,
129
+ resourceId: entry.resourceId,
130
+ });
131
+ }
132
+ });
133
+ return items;
134
+ }
135
+ }
351
136
 
352
- var NominatimProvider = /** @class */ (function (_super) {
353
- __extends(NominatimProvider, _super);
354
- function NominatimProvider(options) {
355
- var _this = _super.call(this, options) || this;
356
- _this.searchUrl = 'https://nominatim.openstreetmap.org';
357
- _this._requests = [];
358
- return _this;
359
- }
360
- NominatimProvider.prototype.abort = function () {
361
- this._requests.forEach(function (x) { return x.cancel(); });
362
- this._requests = [];
363
- };
364
- NominatimProvider.prototype.search = function (val) {
365
- return __asyncGenerator(this, arguments, function search_1() {
366
- var url, request, items, _loop_1, _i, items_1, i;
367
- var _this = this;
368
- return __generator(this, function (_a) {
369
- switch (_a.label) {
370
- case 0:
371
- url = fixUrlStr(
372
- // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
373
- "".concat(this.searchUrl, "/search?format=geojson&polygon_geojson=1&q=").concat(val));
374
- request = new CancelablePromise(function (resolve, reject, onCancel) {
375
- var xhr = new XMLHttpRequest();
376
- xhr.open('GET', url, true);
377
- xhr.onload = function () {
378
- try {
379
- var geojson = JSON.parse(xhr.responseText);
380
- var features = geojson.features.map(function (x) {
381
- return {
382
- geom: x,
383
- extent: x.bbox,
384
- text: x.properties && x.properties.display_name,
385
- query: val,
386
- };
387
- });
388
- resolve(features);
389
- }
390
- catch (er) {
391
- reject(er);
392
- }
393
- };
394
- xhr.onerror = function (er) {
395
- reject(er);
396
- };
397
- onCancel(function () {
398
- xhr.abort();
399
- });
400
- xhr.send();
401
- }).catch(function (er) {
402
- if (er.name === 'CancelError') ;
403
- throw er;
404
- });
405
- this._requests.push(request);
406
- return [4 /*yield*/, __await(request)];
407
- case 1:
408
- items = _a.sent();
409
- if (!items) return [3 /*break*/, 5];
410
- _loop_1 = function (i) {
411
- return __generator(this, function (_b) {
412
- switch (_b.label) {
413
- case 0:
414
- i.result = function () { return _this.result(i); };
415
- return [4 /*yield*/, __await(i)];
416
- case 1: return [4 /*yield*/, _b.sent()];
417
- case 2:
418
- _b.sent();
419
- return [2 /*return*/];
420
- }
421
- });
422
- };
423
- _i = 0, items_1 = items;
424
- _a.label = 2;
425
- case 2:
426
- if (!(_i < items_1.length)) return [3 /*break*/, 5];
427
- i = items_1[_i];
428
- return [5 /*yield**/, _loop_1(i)];
429
- case 3:
430
- _a.sent();
431
- _a.label = 4;
432
- case 4:
433
- _i++;
434
- return [3 /*break*/, 2];
435
- case 5: return [2 /*return*/];
436
- }
437
- });
438
- });
439
- };
440
- NominatimProvider.prototype.result = function (model) {
441
- return CancelablePromise.resolve({
442
- text: model.text,
443
- extent: model.extent || [],
444
- geom: model.geom,
445
- });
446
- };
447
- return NominatimProvider;
448
- }(BaseProvider));
137
+ class NominatimProvider extends BaseProvider {
138
+ constructor(options) {
139
+ super(options);
140
+ this.searchUrl = 'https://nominatim.openstreetmap.org';
141
+ this._requests = [];
142
+ }
143
+ abort() {
144
+ this._requests.forEach((x) => x.cancel());
145
+ this._requests = [];
146
+ }
147
+ async *search(val) {
148
+ // RFC2616 accept - language string
149
+ const url = fixUrlStr(
150
+ // `${this.baseUrl}/search?format=geojson&polygon_geojson=1${window.locale === 'ru' ? '&accept-language=ru-RU,en-US' : ''}&q=${val}`
151
+ `${this.searchUrl}/search?format=geojson&polygon_geojson=1&q=${val}`);
152
+ const request = new CancelablePromise((resolve, reject, onCancel) => {
153
+ const xhr = new XMLHttpRequest();
154
+ xhr.open('GET', url, true);
155
+ xhr.onload = () => {
156
+ try {
157
+ const geojson = JSON.parse(xhr.responseText);
158
+ const features = geojson.features.map((x) => {
159
+ return {
160
+ geom: x,
161
+ extent: x.bbox,
162
+ text: x.properties && x.properties.display_name,
163
+ query: val,
164
+ };
165
+ });
166
+ resolve(features);
167
+ }
168
+ catch (er) {
169
+ reject(er);
170
+ }
171
+ };
172
+ xhr.onerror = (er) => {
173
+ reject(er);
174
+ };
175
+ onCancel(() => {
176
+ xhr.abort();
177
+ });
178
+ xhr.send();
179
+ }).catch((er) => {
180
+ if (er.name === 'CancelError') ;
181
+ throw er;
182
+ });
183
+ this._requests.push(request);
184
+ const items = await request;
185
+ if (items) {
186
+ for (const i of items) {
187
+ i.result = () => this.result(i);
188
+ yield i;
189
+ }
190
+ }
191
+ }
192
+ result(model) {
193
+ return CancelablePromise.resolve({
194
+ text: model.text,
195
+ extent: model.extent || [],
196
+ geom: model.geom,
197
+ });
198
+ }
199
+ }
449
200
 
450
- function create(options) {
451
- return new Geocoder(options);
201
+ function create(options) {
202
+ return new Geocoder(options);
452
203
  }
453
204
 
454
205
  export { BaseProvider, Geocoder, NgwProvider, NominatimProvider, create };