All files / src/app/map map.component.ts

60.61% Statements 60/99
0% Branches 0/4
37.5% Functions 9/24
61.46% Lines 59/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308                          1x   1x 1x         22x     22x     22x     22x     22x 22x     22x     1x 22x           1x 22x         22x             22x       22x     22x                         22x     22x       22x     22x       22x   22x                         1x       1x     1x         1x       1x     1x               10x 10x     10x                                                                                           1x     22x 22x   22x 22x   22x         11x 11x   11x 11x     22x 22x   22x 22x   22x                                 1x                             1x                       1x                         1x                                 1x                         1x 1x 1x 1x 1x   1x  
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { loadModules } from 'esri-loader';
 
import { defaultBasemap, initialExtent, mapsAspectRatio } from './map-defaults';
import { navigationEvents } from './navigation-events';
import { TopographyService } from './topography/topography.service';
import { topographicCategories } from './topography/utils';
 
@Component({
  selector: 'app-map',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
 
  @ViewChild('availableSpace') private availableSpace: ElementRef<HTMLDivElement>;
  @ViewChild('mapContainer') private mapContainer: ElementRef<HTMLDivElement>;
 
  mapView: __esri.MapView;
  mapViewReady: Promise<void>;
 
  private _basemapHidden = false;
  private basemap: __esri.Basemap;
 
  private _topographyHidden = true;
  topographyLayer: __esri.GroupLayer;
 
  private _drawingsHidden = true;
  drawingsLayer: __esri.GraphicsLayer;
 
  private _resizeOnChange = false;
  private fixedExtent: __esri.Extent | undefined;
 
  private _navigationEnabled = true;
  private navigationBlockingListeners: IHandle[] = [];
 
  constructor(
    private topographyService: TopographyService
  ) {}
 
  ngOnInit() {
    this.mapViewReady = this.init();
  }
 
  /**
   * Initialise the component's MapView with a default basemap and and initial extent.
   */
  private async init(): Promise<void> {
    const [
      Map,
      MapView,
      GroupLayer,
      GraphicsLayer
    ] = await loadModules([
      'esri/Map',
      'esri/views/MapView',
      'esri/layers/GroupLayer',
      'esri/layers/GraphicsLayer'
    ]);
 
    const componentMap: __esri.Map = new Map({
      basemap: defaultBasemap
    });
 
    this.basemap = componentMap.basemap;
 
    // Initialise the component's MapView.
    this.mapView = new MapView({
      ui: {
        components: ['attribution']
      },
      constraints: {
        minZoom: 8,
        snapToZoom: false
      },
      container: this.mapContainer.nativeElement,
      extent: initialExtent,
      map: componentMap
    });
 
    this.resizeMapView();
 
    // Initialise the component's layer to print topographic information on the map.
    this.topographyLayer = new GroupLayer({
      id: 'topography',
      visible: false
    });
    this.mapView.map.add(this.topographyLayer);
 
    // Initialise the component's layer to print user drawings on the map.
    this.drawingsLayer = new GraphicsLayer({
      id: 'drawings',
      visible: false
    });
    this.mapView.map.add(this.drawingsLayer);
 
    return this.mapView.when();
  }
 
  get basemapHidden() {
    return this._basemapHidden;
  }
 
  set basemapHidden(value: boolean) {
    this._basemapHidden = value;
    this.mapView.map.basemap = !value ? this.basemap : {} as __esri.Basemap;
  }
 
  get topographyHidden() {
    return this._topographyHidden;
  }
 
  set topographyHidden(value: boolean) {
    this._topographyHidden = value;
 
    if (this.topographyLayer) {
      this.topographyLayer.visible = !value;
    }
  }
 
  get drawingsHidden() {
    return this._drawingsHidden;
  }
 
  set drawingsHidden(value: boolean) {
    this._drawingsHidden = value;
 
    if (this.drawingsLayer) {
      this.drawingsLayer.visible = !value;
    }
  }
 
  get resizeOnChange() {
    return this._resizeOnChange;
  }
 
  set resizeOnChange(value: boolean) {
    this._resizeOnChange = value;
 
    if (value) {
      window.addEventListener(
        'resize',
        () => {
          this.resizeMapView();
          if (this.fixedExtent) {
            this.setExtent(this.fixedExtent);
            this.mapView.goTo(this.fixedExtent);
          }
        },
        true
      );
    }
  }
 
  get navigationEnabled() {
    return this._navigationEnabled;
  }
 
  set navigationEnabled(value: boolean) {
    if (value) {
      this.fixedExtent = undefined;
 
      this.navigationBlockingListeners.forEach(
        listener => listener.remove()
      );
 
    } else if (this._navigationEnabled && !value) {
      this.fixedExtent = this.mapView.extent.clone();
 
      // Stop any potential current movement on the map.
      this.mapView.goTo(this.fixedExtent);
 
      this.navigationBlockingListeners = navigationEvents.map(
        (navigationEvent) => this.mapView.on(
          navigationEvent.name,
          navigationEvent.modifiers,
          (event) => event.stopPropagation()
      ));
    }
 
    this._navigationEnabled = value;
  }
 
  /**
   * Resize the component's MapView to fit inside its parent view while keeping a given aspect ratio.
   */
  private resizeMapView() {
    // Set the component's container width and height to 0 to prevent
    // it from influencing the availableSpace container's size.
    this.mapContainer.nativeElement.style.width = '0';
    this.mapContainer.nativeElement.style.height = '0';
 
    const availableWidth = this.availableSpace.nativeElement.clientWidth;
    const availableHeight = this.availableSpace.nativeElement.offsetHeight;
 
    const availableSpaceRatio = availableWidth / availableHeight;
 
    let height: number;
    let width: number;
    if (mapsAspectRatio < availableSpaceRatio) {
      height = availableHeight;
      width = height * mapsAspectRatio;
    } else {
      width = availableWidth;
      height = width / mapsAspectRatio;
    }
 
    width = Math.round(width);
    height = Math.round(height);
 
    const left = (this.availableSpace.nativeElement.offsetWidth - width) / 2;
    const top = (this.availableSpace.nativeElement.offsetHeight - height) / 2;
 
    Object.assign(
      this.mapContainer.nativeElement.style,
      {
        height: `${height}px`,
        width: `${width}px`,
        position: 'relative',
        top: `${top}px`,
        left: `${left}px`
      }
    );
  }
 
  /**
   * Set the extent covered by the component's MapView.
   *
   * @param extent The Extent to be covered by the component's MapView.
   */
  setExtent(extent: __esri.Extent) {
    const extentClone = extent.clone();
    this.mapView.extent = extentClone;
    this.mapView.extent.xmin = extentClone.xmin;
    this.mapView.extent.xmax = extentClone.xmax;
    this.mapView.extent.ymin = extentClone.ymin;
    this.mapView.extent.ymax = extentClone.ymax;
  }
 
  /**
   * Move the position of the component's MapView center by a given offset.
   *
   * @param x The offset on the x axis to move the MapView's center by.
   * @param y The offset on the y axis to move the MapView's center by.
   */
  moveMapCenter(x: number, y: number): void {
    const mapScreenCenter = this.mapView.toScreen(this.mapView.center);
    mapScreenCenter.x += x;
    mapScreenCenter.y += y;
    this.mapView.goTo(this.mapView.toMap(mapScreenCenter));
  }
 
  /**
   * Modify the component's MapView scale (zoom level).
   *
   * @param scaleModifier The modification to apply to the MapView's scale.
   */
  modifyMapScale(scaleModifier: number) {
    const percent = this.mapView.scale / 100;
    this.mapView.goTo({
      scale: this.mapView.scale + percent * scaleModifier,
    });
    // this.mapView.scale += percent * scaleModifier;
  }
 
  /**
   * Load topographic information from the application's ArcGIS server for
   * the current Extent of the component's MapView and add the associated
   * graphics to the component's topographyLayer.
   */
  async loadTopography() {
    this.topographyLayer.removeAll();
 
    const topographyTypesGraphics = await this.topographyService.loadAllTopographyTypesGraphics(
      this.mapView.extent,
      this.mapView.scale
    );
 
    this.topographyLayer.addMany(topographyTypesGraphics);
  }
 
  /**
   * Make the layers for the TopographyTypes belonging to some category visible and
   * hide all the others.
   *
   * @param category The topographic category whose TopographyTypes must be made visible on the map.
   */
  setVisibleTopographicCategory(category: 'roads' | 'city') {
    this.topographyLayer.layers.forEach(
      (layer) => layer.visible = false
    );
 
    topographicCategories[category].forEach(
      (topographyTypeName) => this.topographyLayer.findLayerById(topographyTypeName).visible = true
    );
  }
 
  /**
   * Clear all the graphics from the maps topography and drawings layers, and hide them.
   */
  clearLayersGraphics() {
    this.topographyLayer.removeAll();
    this.topographyHidden = true;
    this.drawingsLayer.removeAll();
    this.drawingsHidden = true;
  }
}