@nextgis/ngw-map 1.5.1 → 1.8.1

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,4 +1,4 @@
1
- /** Bundle of @nextgis/ngw-map; version: 1.5.1; author: NextGIS */
1
+ /** Bundle of @nextgis/ngw-map; version: 1.8.1; author: NextGIS */
2
2
  var events = {exports: {}};
3
3
 
4
4
  var R = typeof Reflect === 'object' ? Reflect : null;
@@ -2197,8 +2197,10 @@ class WebMapLayers extends WebMapMain {
2197
2197
  */
2198
2198
  async fitLayer(layerDef, options) {
2199
2199
  const layer = this.getLayer(layerDef);
2200
- if (layer && layer.getExtent) {
2201
- const extent = await layer.getExtent();
2200
+ // TODO: remove backward compatibility for `getExtent`
2201
+ const getBounds = layer && (layer.getBounds || layer.getExtent);
2202
+ if (getBounds) {
2203
+ const extent = await getBounds.call(layer);
2202
2204
  if (extent) {
2203
2205
  this.fitBounds(extent, options);
2204
2206
  }
@@ -2401,8 +2403,10 @@ class WebMapLayers extends WebMapMain {
2401
2403
  if (opacity !== undefined && opacity <= 1) {
2402
2404
  this.setLayerOpacity(_adapter, opacity);
2403
2405
  }
2404
- if (options.fit && _adapter.getExtent) {
2405
- const extent = await _adapter.getExtent();
2406
+ // TODO: remove backward compatibility for `getExtent`
2407
+ const getBounds = layer && (_adapter.getBounds || _adapter.getExtent);
2408
+ if (options.fit && getBounds) {
2409
+ const extent = await getBounds.call(_adapter);
2406
2410
  if (extent) {
2407
2411
  await this.fitBounds(extent);
2408
2412
  }
@@ -2525,10 +2529,18 @@ class WebMapLayers extends WebMapMain {
2525
2529
  opt.paint = opt.paint || {};
2526
2530
  return this.addLayer(adapter || 'GEOJSON', opt);
2527
2531
  }
2528
- /** Shortcut for {@link WebMapLayers.addGeoJsonLayer} to initialize adapter with generic types for working in typescript */
2532
+ /** Shortcut for {@link WebMapLayers.addGeoJsonLayer} to create GeoJson adapter with generic types for working in typescript */
2529
2533
  addFeatureLayer(options = {}) {
2530
2534
  return this.addGeoJsonLayer(options);
2531
2535
  }
2536
+ /** Shortcut for {@link WebMapLayers.addLayer} to create TileLayer adapter */
2537
+ addTileLayer(url, options) {
2538
+ return this.addLayer('TILE', { ...options, url });
2539
+ }
2540
+ /** Shortcut for {@link WebMapLayers.addLayer} to create TileLayer adapter */
2541
+ addImageLayer(url, options) {
2542
+ return this.addLayer('IMAGE', { ...options, url });
2543
+ }
2532
2544
  /**
2533
2545
  * Show added layer on the map by it definition.
2534
2546
  */
@@ -2571,11 +2583,11 @@ class WebMapLayers extends WebMapMain {
2571
2583
  const order = l.options.baselayer ? 0 : l.options.order;
2572
2584
  // do not show baselayer if another on the map
2573
2585
  if (l.options.baselayer && this._baselayers.length) {
2574
- const anotherVisibleLayerBaseLayer = this._baselayers.find((x) => {
2586
+ const anotherVisibleBaselayers = this._baselayers.filter((x) => {
2575
2587
  return x !== l.id && this.isLayerVisible(x);
2576
2588
  });
2577
- if (anotherVisibleLayerBaseLayer) {
2578
- await this.hideLayer(anotherVisibleLayerBaseLayer);
2589
+ for (const bl of anotherVisibleBaselayers) {
2590
+ await this.hideLayer(bl);
2579
2591
  }
2580
2592
  }
2581
2593
  if (l.showLayer) {
@@ -4402,6 +4414,17 @@ function fetchNgwLayerExtent({ resourceId, connector, cache = true, }) {
4402
4414
  }
4403
4415
  });
4404
4416
  }
4417
+ function fetchNgwLayerItemExtent({ resourceId, featureId, connector, cache = true, }) {
4418
+ return connector
4419
+ .get('feature_layer.feature.item_extent', { cache }, { id: resourceId, fid: featureId })
4420
+ .then((resp) => {
4421
+ if (resp) {
4422
+ const { maxLat, maxLon, minLat, minLon } = resp.extent;
4423
+ const extentArray = [minLon, minLat, maxLon, maxLat];
4424
+ return extentArray;
4425
+ }
4426
+ });
4427
+ }
4405
4428
  function fetchNgwResourceExtent(item, connector) {
4406
4429
  if (item.webmap) {
4407
4430
  return CancelablePromise.resolve(getNgwWebmapExtent(item.webmap));
@@ -4750,14 +4773,18 @@ async function createGeoJsonAdapter(props) {
4750
4773
  }
4751
4774
  return layer;
4752
4775
  }
4776
+ /** @deprecated use {@link NgwGeoJsonAdapter.getBounds} instead */
4753
4777
  getExtent() {
4778
+ return this.getBounds();
4779
+ }
4780
+ async getBounds() {
4754
4781
  const hasData = this.getLayers && this.getLayers().length;
4755
4782
  if (this.options.strategy === 'BBOX' || hasData) {
4756
4783
  return fetchNgwResourceExtent(item, connector);
4757
4784
  }
4758
4785
  else {
4759
- if (super.getExtent) {
4760
- return super.getExtent();
4786
+ if (super.getBounds) {
4787
+ return super.getBounds();
4761
4788
  }
4762
4789
  }
4763
4790
  }
@@ -4891,10 +4918,10 @@ async function createGeoJsonAdapter(props) {
4891
4918
  return NgwGeoJsonAdapter;
4892
4919
  }
4893
4920
 
4894
- function getLayerAdapterOptions(options, webMap, baseUrl) {
4921
+ function ngwApiToAdapterOptions({ options, webMap, baseUrl, }) {
4895
4922
  let adapter = options.adapter || 'IMAGE';
4896
4923
  let url;
4897
- const layerAdapters = webMap.getLayerAdapters();
4924
+ const layerAdapters = webMap && webMap.getLayerAdapters();
4898
4925
  const isImageAllowed = layerAdapters ? layerAdapters.IMAGE : true;
4899
4926
  const resourceId = options.resource;
4900
4927
  const nd = options.tileNoData ? options.tileNoData : 200;
@@ -4984,10 +5011,10 @@ async function createRasterAdapter({ layerOptions, webMap, connector, item, }) {
4984
5011
  adapter = 'TILE';
4985
5012
  }
4986
5013
  }
4987
- const adapterClass = webMap.mapAdapter.layerAdapters[adapter];
4988
- if (adapterClass) {
5014
+ const AdapterClass = webMap.mapAdapter.layerAdapters[adapter];
5015
+ if (AdapterClass) {
4989
5016
  const resourceId = await resourceIdFromLayerOptions(layerOptions, connector);
4990
- return class Adapter extends adapterClass {
5017
+ return class RasterAdapter extends AdapterClass {
4991
5018
  constructor(map, _options) {
4992
5019
  var _a;
4993
5020
  super(map, _options);
@@ -4995,7 +5022,11 @@ async function createRasterAdapter({ layerOptions, webMap, connector, item, }) {
4995
5022
  // options = {};
4996
5023
  this.item = item;
4997
5024
  this.resourceId = resourceId;
4998
- const opt = getLayerAdapterOptions(layerOptions, webMap, connector.options.baseUrl || '');
5025
+ const opt = ngwApiToAdapterOptions({
5026
+ options: layerOptions,
5027
+ webMap,
5028
+ baseUrl: connector.options.baseUrl || '',
5029
+ });
4999
5030
  if (opt) {
5000
5031
  const layerAdapterOptions = {
5001
5032
  ...opt,
@@ -5238,6 +5269,9 @@ class BaseProperty {
5238
5269
  getParent() {
5239
5270
  return this.item.tree.getParent();
5240
5271
  }
5272
+ getChildren() {
5273
+ return this.item.tree.getChildren();
5274
+ }
5241
5275
  isGroup() {
5242
5276
  const children = this.item.tree.getDescendants();
5243
5277
  return children.length;
@@ -5352,6 +5386,24 @@ class CheckProperty extends BaseProperty {
5352
5386
  return property && property.get();
5353
5387
  }));
5354
5388
  }
5389
+ block(options) {
5390
+ this._blocked = true;
5391
+ this._block(options);
5392
+ }
5393
+ unBlock(options) {
5394
+ this._blocked = false;
5395
+ if (this.getValue()) {
5396
+ this._unBlock(options);
5397
+ }
5398
+ }
5399
+ blockChild(options) {
5400
+ this.item.tree
5401
+ .getDescendants()
5402
+ .forEach((x) => this._blockChild(x, options));
5403
+ }
5404
+ unblockChild(options) {
5405
+ this.item.tree.getChildren().forEach((x) => this._unBlockChild(x, options));
5406
+ }
5355
5407
  _prepareValue(value) {
5356
5408
  return value;
5357
5409
  }
@@ -5373,30 +5425,12 @@ class CheckProperty extends BaseProperty {
5373
5425
  this.unblockChild(options);
5374
5426
  }
5375
5427
  }
5376
- block(options) {
5377
- this._blocked = true;
5378
- this._block(options);
5379
- }
5380
5428
  _block(options) {
5381
5429
  this._turnOff(options);
5382
5430
  }
5383
- unBlock(options) {
5384
- this._blocked = false;
5385
- if (this.getValue()) {
5386
- this._unBlock(options);
5387
- }
5388
- }
5389
5431
  _unBlock(options) {
5390
5432
  this._turnOn(options);
5391
5433
  }
5392
- blockChild(options) {
5393
- this.item.tree
5394
- .getDescendants()
5395
- .forEach((x) => this._blockChild(x, options));
5396
- }
5397
- unblockChild(options) {
5398
- this.item.tree.getChildren().forEach((x) => this._unBlockChild(x, options));
5399
- }
5400
5434
  _blockChild(item, options) {
5401
5435
  const prop = item.properties &&
5402
5436
  item.properties.property(this.name);
@@ -5454,22 +5488,22 @@ class ItemProperties {
5454
5488
  }
5455
5489
  _setPropertyHandler(propOpt) {
5456
5490
  const handlers = ItemProperties.handlers;
5457
- let handler = propOpt.handler;
5458
- if (!handler && propOpt.type) {
5491
+ let Handler = propOpt.handler;
5492
+ if (!Handler && propOpt.type) {
5459
5493
  switch (propOpt.type) {
5460
5494
  case 'boolean':
5461
- handler = handlers.CheckProperty;
5495
+ Handler = handlers.CheckProperty;
5462
5496
  break;
5463
5497
  case 'string':
5464
- handler = handlers.BaseProperty;
5498
+ Handler = handlers.BaseProperty;
5465
5499
  break;
5466
5500
  default:
5467
- handler = handlers.BaseProperty;
5501
+ Handler = handlers.BaseProperty;
5468
5502
  }
5469
5503
  }
5470
- if (handler && propOpt.name) {
5504
+ if (Handler && propOpt.name) {
5471
5505
  const options = { ...(propOpt || {}) };
5472
- this._properties[propOpt.name] = new handler(propOpt.name, this.item, options);
5506
+ this._properties[propOpt.name] = new Handler(propOpt.name, this.item, options);
5473
5507
  this._propertiesList.push(propOpt.name);
5474
5508
  }
5475
5509
  }
@@ -5525,6 +5559,55 @@ class Item {
5525
5559
  }
5526
5560
  }
5527
5561
 
5562
+ class WebmapLayerOpacityPropertyHandler extends BaseProperty {
5563
+ getProperty() {
5564
+ const layer = this.item.layer;
5565
+ if (layer) {
5566
+ return layer && layer.options && layer.options.opacity !== undefined
5567
+ ? layer.options.opacity
5568
+ : 1;
5569
+ }
5570
+ return 1;
5571
+ }
5572
+ update(value, options) {
5573
+ options = options || {};
5574
+ if (this.isGroup()) {
5575
+ this._value = this._prepareValue(value, options);
5576
+ const children = this.getChildren();
5577
+ for (const child of children) {
5578
+ const property = child.properties.property(this.name);
5579
+ if (property) {
5580
+ if (options.propagation) {
5581
+ property.set(value, options);
5582
+ }
5583
+ else {
5584
+ property.update(property.get(), options);
5585
+ }
5586
+ }
5587
+ }
5588
+ }
5589
+ const layer = this.item.layer;
5590
+ if (layer) {
5591
+ const parents = this.getParents();
5592
+ const coef = parents.reduce((s, x) => {
5593
+ const prop = x.properties.property(this.name);
5594
+ const multiplier = prop ? prop.getValue() : 1;
5595
+ return s * multiplier;
5596
+ }, 1);
5597
+ const val = value * coef;
5598
+ this.item.webMap.setLayerOpacity(layer, this._prepareValue(val));
5599
+ }
5600
+ }
5601
+ _prepareValue(value, options) {
5602
+ if (options && options.propagation && this.isGroup()) {
5603
+ return 1;
5604
+ }
5605
+ // value = Number(value);
5606
+ // return value < 0.1 ? 0.1 : value;
5607
+ return Number(value);
5608
+ }
5609
+ }
5610
+
5528
5611
  class NgwWebmapItem extends Item {
5529
5612
  constructor(webMap, item, options, connector, parent, noInit) {
5530
5613
  super({ ...NgwWebmapItem.options, ...options });
@@ -5606,6 +5689,9 @@ class NgwWebmapItem extends Item {
5606
5689
  fit() {
5607
5690
  if (this.item.item_type === 'layer') ;
5608
5691
  }
5692
+ getLayer() {
5693
+ return this.layer;
5694
+ }
5609
5695
  getItemOptions(item) {
5610
5696
  const transparency = item.item_type === 'layer' && item.layer_transparency;
5611
5697
  const opacity = typeof transparency === 'number' ? (100 - transparency) / 100 : undefined;
@@ -5707,6 +5793,10 @@ NgwWebmapItem.options = {
5707
5793
  }
5708
5794
  },
5709
5795
  },
5796
+ {
5797
+ name: 'opacity',
5798
+ handler: WebmapLayerOpacityPropertyHandler,
5799
+ },
5710
5800
  ],
5711
5801
  };
5712
5802
 
@@ -5893,17 +5983,26 @@ class NgwWebmapLayerAdapter {
5893
5983
  delete this.response;
5894
5984
  delete this._webmapLayersIds;
5895
5985
  }
5896
- showLayer() {
5986
+ async showLayer() {
5987
+ if (this.layer && this.layer.properties) {
5988
+ return this.layer.properties.property('visibility').set(true);
5989
+ }
5990
+ }
5991
+ async hideLayer() {
5897
5992
  if (this.layer && this.layer.properties) {
5898
- this.layer.properties.property('visibility').set(true);
5993
+ return this.layer.properties.property('visibility').set(false);
5899
5994
  }
5900
5995
  }
5901
- hideLayer() {
5996
+ async setOpacity(val) {
5902
5997
  if (this.layer && this.layer.properties) {
5903
- this.layer.properties.property('visibility').set(false);
5998
+ return this.layer.properties.property('opacity').set(val);
5904
5999
  }
5905
6000
  }
6001
+ /** @deprecated use {@link NgwWebmapLayerAdapter.getBounds} instead */
5906
6002
  getExtent() {
6003
+ return this.getBounds();
6004
+ }
6005
+ getBounds() {
5907
6006
  const webmap = this.response && this.response.webmap;
5908
6007
  if (webmap) {
5909
6008
  return getNgwWebmapExtent(webmap);
@@ -6064,10 +6163,14 @@ class NgwWebmapLayerAdapter {
6064
6163
  item.resourceId = resourceId;
6065
6164
  item.updateWmsParams = (params) => updateImageParams(params, resourceId);
6066
6165
  const adapter = item.layer_adapter.toUpperCase();
6067
- const layerAdapterOptions = getLayerAdapterOptions({
6068
- adapter,
6069
- resource: resourceId,
6070
- }, webMap, this.options.connector.options.baseUrl || '');
6166
+ const layerAdapterOptions = ngwApiToAdapterOptions({
6167
+ options: {
6168
+ adapter,
6169
+ resource: resourceId,
6170
+ },
6171
+ webMap,
6172
+ baseUrl: this.options.connector.options.baseUrl || '',
6173
+ });
6071
6174
  item = {
6072
6175
  ...item,
6073
6176
  ...layerAdapterOptions,
@@ -6117,12 +6220,16 @@ async function createWebMapAdapter({ layerOptions: options, webMap, connector, }
6117
6220
  }
6118
6221
 
6119
6222
  class NgwResource {
6223
+ /** @deprecated use {@link NgwResource.getBounds} instead */
6120
6224
  async getExtent() {
6225
+ return this.getBounds();
6226
+ }
6227
+ async getBounds() {
6121
6228
  if (this._extent) {
6122
6229
  return this._extent;
6123
6230
  }
6124
6231
  if (this.item) {
6125
- this._extent = await fetchNgwResourceExtent(this.item, this.connector).then();
6232
+ this._extent = await fetchNgwResourceExtent(this.item, this.connector);
6126
6233
  return this._extent;
6127
6234
  }
6128
6235
  }
@@ -6442,7 +6549,7 @@ const vectorLayerGeomToPaintTypeAlias = {
6442
6549
  MULTILINESTRINGZ: 'line',
6443
6550
  MULTIPOLYGONZ: 'polygon',
6444
6551
  };
6445
- function addNgwLayer(options, webMap, connector) {
6552
+ function createNgwLayerAdapter(options, webMap, connector) {
6446
6553
  const headers = connector.getAuthorizationHeaders();
6447
6554
  if (headers) {
6448
6555
  options.headers = headers;
@@ -6558,11 +6665,11 @@ class IdentifyItem {
6558
6665
  constructor(options) {
6559
6666
  const f = options.feature;
6560
6667
  this.id = f.id;
6668
+ this.geom = f.geom;
6561
6669
  this.label = f.label;
6562
- this.layerId = f.layerId;
6563
6670
  this.parent = f.parent;
6564
6671
  this.fields = f.fields;
6565
- this.geom = f.geom;
6672
+ this.layerId = f.layerId;
6566
6673
  this.connector = options.connector;
6567
6674
  }
6568
6675
  identify(options = {}) {
@@ -6592,6 +6699,16 @@ class IdentifyItem {
6592
6699
  return this._resource.feature_layer;
6593
6700
  });
6594
6701
  }
6702
+ getBounds() {
6703
+ if (this._extent) {
6704
+ return CancelablePromise.resolve(this._extent);
6705
+ }
6706
+ return fetchNgwLayerItemExtent({
6707
+ connector: this.connector,
6708
+ featureId: this.id,
6709
+ resourceId: this.layerId,
6710
+ });
6711
+ }
6595
6712
  geojson() {
6596
6713
  if (this._geojson) {
6597
6714
  return CancelablePromise.resolve(this._geojson);
@@ -7037,7 +7154,7 @@ class NgwMap extends WebMap {
7037
7154
  options.adapterOptions.setViewDelay = this.options.setViewDelay;
7038
7155
  }
7039
7156
  }
7040
- const adapter = addNgwLayer(options, this, this.connector);
7157
+ const adapter = createNgwLayerAdapter(options, this, this.connector);
7041
7158
  const adapterOpts = {
7042
7159
  visibility: true,
7043
7160
  // TODO: do not merge options, use only `adapterOptions`
@@ -7184,10 +7301,10 @@ class NgwMap extends WebMap {
7184
7301
  }
7185
7302
  const ngwLayer = id && this._ngwLayers[id];
7186
7303
  if (ngwLayer) {
7187
- if (ngwLayer.layer.getExtent) {
7188
- const extent = await ngwLayer.layer.getExtent();
7189
- if (extent) {
7190
- this.fitBounds(extent, options);
7304
+ if (ngwLayer.layer.getBounds) {
7305
+ const bounds = await ngwLayer.layer.getBounds();
7306
+ if (bounds) {
7307
+ this.fitBounds(bounds, options);
7191
7308
  }
7192
7309
  }
7193
7310
  else {