<commit_before>/*!
 * Leaflet Map Service
 *
 * Copyright(c) <NAME> <<EMAIL>>
 * Licensed under MIT
 */

import { Injectable } from '@angular/core';
import { Map, MapOptions, WMS, WMSOptions } from 'leaflet';

@Injectable()
export class LeafletMapService {
  private _map: Promise<Map>;
  private _mapResolver: (value?: Map) => void;
  private _wmsLayers: Array<WMS> = [];

  constructor() {
    this._map = new Promise<Map>((resolve: () => void) => {
      this._mapResolver = resolve;
    });
  }

  createMap(el: HTMLElement, mapOptions: MapOptions): Promise<void> {
    return Promise.resolve().then(() => {
      const map = L.map(el, mapOptions);
      this._mapResolver(<Map>map);
      return;
    });
  }

  getMap(): Promise<Map> { return this._map; }

  addWMSLayer(url: string, options: WMSOptions): Promise<WMS> {
    return this.getMap()
      .then((map: Map) => {
        let layer = (L as any).tileLayer.wms(url, options);

        // add the layer to the map
        map.addLayer(layer);

        // save the reference of the layer to the layers propery
        this._wmsLayers.push(layer);

        // return the layer
        return layer;
      })
      ;
  }

  addWMSLayers(items: Array<any>): Promise<WMS> {
    return this.getMap()
      .then((map: Map) => {
        let layers = items.map((item: any) => {
          let layer = (L as any).tileLayer.wms(item.url, item.options);

          // the created layer to the map
          map.addLayer(layer);

          // return the layer
          return layer;
        });

        // append to the existing layers
        this._wmsLayers.concat(layers);

        return layers;
      })
      ;
  }

  clearWMSLayers(): Promise<void> {
    return this.getMap()
      .then((map: Map) => {
        // remove any existing layers
        this._wmsLayers.forEach((value: WMS) => {
          map.removeLayer(value);
        });

        // set the layers to empty array
        this._wmsLayers = [];
      })
      ;
  }

  getWMSLayers(): Promise<Array<WMS>> {
    return this.getMap()
      .then((map: Map) => {
        return this._wmsLayers;
      });
  }

}

<commit_msg>Improve layer add and remove logic<commit_after>/*!
 * Leaflet Map Service
 *
 * Copyright(c) <NAME> <<EMAIL>>
 * Licensed under MIT
 */

import { Injectable } from '@angular/core';
import { Map, MapOptions, WMS, WMSOptions } from 'leaflet';
import * as _ from 'lodash';

@Injectable()
export class LeafletMapService {
  private _map: Promise<Map>;
  private _mapResolver: (value?: Map) => void;
  private _wmsLayers: any = {};

  constructor() {
    this._map = new Promise<Map>((resolve: () => void) => {
      this._mapResolver = resolve;
    });
  }

  createMap(el: HTMLElement, mapOptions: MapOptions): Promise<void> {
    return Promise.resolve().then(() => {
      const map = L.map(el, mapOptions);
      this._mapResolver(<Map>map);
      return;
    });
  }

  getMap(): Promise<Map> { return this._map; }

  addWMSLayer(id: string, url: string, options: WMSOptions): Promise<WMS> {
    return this.getMap()
      .then((map: Map) => {
        if (_.has(this._wmsLayers, id)) {
          throw new Error('ID already exists. Provide another ID for this layer');
        }

        let layer = (L as any).tileLayer.wms(url, options);

        // add the layer to the map
        map.addLayer(layer);

        // save the reference of the layer to the layers propery
        this._wmsLayers[id] = layer;

        // return the layer
        return layer;
      })
      ;
  }

  addWMSLayers(url: string, items: Array<{id: string, options: WMSOptions}>): Promise<Array<WMS>> {
    return this.getMap()
      .then((map: Map) => {
        let layers = _.chain(items)
          .filter((item) => {
            let result = _.has(this._wmsLayers, item.id);

            if (result) {
              throw new Error(`ID ${item.id} exists. please provide a new ID for this layer.`);
            }

            return !result;
          })
          .map((item) => {
            let layer = (L as any).tileLayer.wms(url, item.options);

            // the created layer to the map
            map.addLayer(layer);

            // store the key and its layer to the key-value store
            this._wmsLayers[item.id] = layer;

            return layer;
          })
          .value()
          ;

        console.log(layers);

        return layers;
      })
      ;
  }

  clearWMSLayers(): Promise<void> {
    return this.getMap()
      .then((map: Map) => {
        // remove any existing layers
        _.each(this._wmsLayers, (value: WMS, id: string) => {
          map.removeLayer(value);
        });

        // set the layers to empty array
        this._wmsLayers = {};
      })
      ;
  }

  removeWMSLAyer(id: string): Promise<void> {
    return this.getMap()
      .then((map: Map) => {
        map.removeLayer(this._wmsLayers[id]);

        // remove the property from the object store.
        delete this._wmsLayers[id];
      })
      ;
  }

  getWMSLayers(): Promise<any> {
    return this.getMap()
      .then((map: Map) => {
        return this._wmsLayers;
      });
  }

}


{'fraction_non_alphanumeric': 0.14249611197511663, 'fraction_numerical': 0.0, 'mean_word_length': 2.088235294117647, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '220522', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1606, 'n_words': 404}