layer/layerRec/basicFC.js

  1. 'use strict';
  2. const shared = require('./shared.js')();
  3. const placeholderFC = require('./placeholderFC.js')();
  4. /**
  5. * @class BasicFC
  6. */
  7. class BasicFC extends placeholderFC.PlaceholderFC {
  8. // base class for feature class object. deals with stuff specific to a feature class (or raster equivalent)
  9. get queryable () { return this._queryable; }
  10. set queryable (value) { this._queryable = value; }
  11. // non-attributes have no geometry.
  12. // TODO decide on proper defaulting or handling of non-geometry layers.
  13. get geomType () { return Promise.resolve('none'); }
  14. /**
  15. * @param {Object} parent the Record object that this Feature Class belongs to
  16. * @param {String} idx the service index of this Feature Class. an integer in string format. use '0' for non-indexed sources.
  17. * @param {Object} config the config object for this sublayer
  18. */
  19. constructor (parent, idx, config) {
  20. super(parent, config.name || '');
  21. this._idx = idx;
  22. this.queryable = config.state.query;
  23. this.extent = config.extent; // if missing, will fill more values after layer loads
  24. // TODO do we need to store a copy of the config? for the memories?
  25. }
  26. // returns an object with minScale and maxScale values for the feature class
  27. getScaleSet () {
  28. // basic case - we get it from the esri layer
  29. // TODO need to test for missing layer??
  30. const l = this._parent._layer;
  31. return {
  32. minScale: l.minScale,
  33. maxScale: l.maxScale
  34. };
  35. }
  36. isOffScale (mapScale) {
  37. const scaleSet = this.getScaleSet();
  38. // GIS for dummies.
  39. // scale increases as you zoom out, decreases as you zoom in
  40. // minScale means if you zoom out beyond this number, hide the layer
  41. // maxScale means if you zoom in past this number, hide the layer
  42. // 0 value for min or max scale means there is no hiding in effect
  43. const result = {
  44. offScale: false,
  45. zoomIn: false
  46. };
  47. // check if out of scale and set zoom direction to scaleSet
  48. if (mapScale < scaleSet.maxScale && scaleSet.maxScale !== 0) {
  49. result.offScale = true;
  50. result.zoomIn = false;
  51. } else if (mapScale > scaleSet.minScale && scaleSet.minScale !== 0) {
  52. result.offScale = true;
  53. result.zoomIn = true;
  54. }
  55. return result;
  56. }
  57. // TODO docs
  58. getVisibility () {
  59. return this._parent._layer.visible;
  60. }
  61. // TODO docs
  62. setVisibility (value) {
  63. // basic case - set layer visibility
  64. this._parent._layer.setVisibility(value);
  65. }
  66. // this will actively download / refresh the internal symbology
  67. // mergeAllLayers indicates we should collate entire parent legend into one block
  68. // e.g. for basemap tile. this._idx would have value 0, but we want all indexes
  69. loadSymbology (mergeAllLayers = false) {
  70. // get symbology from service legend.
  71. // this is used for non-feature based sources (tiles, image, raster).
  72. // wms will override with own special logic.
  73. const url = this._parent._layer.url;
  74. if (url) {
  75. // fetch legend from server, convert to local format, process local format
  76. const legendIndex = mergeAllLayers ? undefined : this._idx;
  77. return this._parent._apiRef.symbology.mapServerToLocalLegend(url, legendIndex)
  78. .then(legendData => {
  79. this.symbology = shared.makeSymbologyArray(legendData.layers[0].legend);
  80. });
  81. } else {
  82. // this shouldn't happen. non-url layers should be files, which are features,
  83. // which will have a basic renderer and will use FeatureFC override.
  84. throw new Error('encountered layer with no renderer and no url');
  85. }
  86. }
  87. zoomToBoundary (map) {
  88. return this._parent.zoomToExtent(map, this.extent);
  89. }
  90. }
  91. module.exports = () => ({
  92. BasicFC
  93. });