<commit_before>import Ember from 'ember';
import BaseLayerComponent from 'ember-flexberry-gis/components/base-layer';

let projectBounds = function (boundingBox, crs) {
  let sw = crs.project(boundingBox.getSouthWest());
  let ne = crs.project(boundingBox.getNorthEast());

  return [
    sw.x,
    sw.y,
    ne.x,
    ne.y
  ];
};

/**
  Esri dynamic map layer component for leaflet map.

  @class EsriDynamicLayerComponent
  @extends BaseLayerComponent
 */
export default BaseLayerComponent.extend({
  leafletOptions: [
    'url', 'format', 'transparent', 'f', 'attribution', 'layers', 'layerDefs', 'opacity',
    'position', 'maxZoom', 'minZoom', 'dynamicLayers', 'token', 'proxy', 'useCors'
  ],

  /**
   Url of ArcGIS REST service endpoint
  */
  url: null,

  layersIds: Ember.computed('url', 'esriLayers', function () {
    let esriLayers = this.get('esriLayers');

    if (Ember.isArray(esriLayers) && esriLayers.length !== 0) {
      return new Ember.RSVP.Promise((resolve, reject) => {
        resolve(esriLayers.map(layer => layer.id));
      });
    } else {
      let params = {
        f: 'json'
      };

      let url = this.get('url');
      url = url + L.Util.getParamString(params, url);

      return new Ember.RSVP.Promise((resolve, reject) => {

        Ember.$.ajax({
          url,
          success: function (data) {
            let result = JSON.parse(data);
            resolve(result.layers.filter(layer => !layer.subLayerIds).map(layer => layer.id));
          },
          error: function () {
            reject();
          }
        });
      });
    }
  }),

  _queryLayer(layerId, params) {
    params = Ember.$.extend(true, params, {
      f: 'json'
    });
    return new Ember.RSVP.Promise((resolve, reject) => {
      let url = this.get('url') + '/' + layerId + '/query';
      let crs = this.get('crs');
      url = url + L.Util.getParamString(params, url);

      Ember.$.ajax({
        url,
        success: function (data) {
          let result = JSON.parse(data);
          if (result.error) {
            reject(result.error);
            return;
          }

          let featureCollection = L.esri.Util.responseToFeatureCollection(result);
          let coordsToLatLng = function (coords) {
            var point = L.point(coords[0], coords[1]);
            var ll = crs.projection.unproject(point);
            if (coords[2]) {
              ll.alt = coords[2];
            }

            return ll;
          };

          resolve(L.geoJSON(featureCollection, {
            coordsToLatLng
          }));
        },
        error: function (e) {
          reject(e);
        }
      });
    });
  },

  _query(e) {
    let layerId = this.get('layerModel.id').toString();
    let layerLinks = e.layerLinks.filter(link => link.get('layerModel.id').toString() === layerId);
    if (!layerLinks.length) {
      return;
    }

    let queryFilter = e.queryFilter;

    e.results.push(new Ember.RSVP.Promise((resolve, reject) => {
      let result = L.featureGroup();

      this.get('layersIds')
        .then(layerIds => {
          let allQueries = [];
          layerIds.forEach((layerId) => {
            layerLinks.forEach((link) => {
              let params = {
                where: link.get('linkParameter').map(link => link.get('layerField') + '=' + queryFilter[link.get('queryKey')]).join(' and ')
              };

              allQueries.push(this._queryLayer(layerId, params));
            });
          });

          Ember.RSVP.all(allQueries).then(layers => {
            layers.forEach(layer => result.addLayer(layer));
            resolve(result);
          });
        })
        .catch((error) => {
          reject(error);
        });
    }));
  },

  _identifyEsri(boundingBox) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      let url = this.get('url') + '/identify';
      let crs = this.get('crs');
      let map = this.get('leafletMap');
      let size = map.getSize();

      let params = {
        f: 'json',
        geometryType: 'esriGeometryEnvelope',
        layers: 'visible',
        returnGeometry: true,
        tolerance: 5,
        geometry: projectBounds(boundingBox, crs),
        imageDisplay: [size.x, size.y, 96],
        mapExtent: projectBounds(map.getBounds(), crs)
      };

      url = url + L.Util.getParamString(params, url);

      let _this = this;

      Ember.$.ajax({
        url,
        success: function (data) {
          if (!Ember.isNone(data.error) && data.error) {
            resolve(Ember.A([]));
          } else {
            let result = JSON.parse(data);
            let featureCollection = L.esri.Util.responseToFeatureCollection(result);
            _this.injectLeafletLayersIntoGeoJSON(featureCollection);
            resolve(Ember.A(Ember.get(featureCollection, 'features') || []));
          }
        },
        error: function (e) {
          reject(e);<commit_msg>Handle error on esri identify<commit_after>import Ember from 'ember';
import BaseLayerComponent from 'ember-flexberry-gis/components/base-layer';

let projectBounds = function (boundingBox, crs) {
  let sw = crs.project(boundingBox.getSouthWest());
  let ne = crs.project(boundingBox.getNorthEast());

  return [
    sw.x,
    sw.y,
    ne.x,
    ne.y
  ];
};

/**
  Esri dynamic map layer component for leaflet map.

  @class EsriDynamicLayerComponent
  @extends BaseLayerComponent
 */
export default BaseLayerComponent.extend({
  leafletOptions: [
    'url', 'format', 'transparent', 'f', 'attribution', 'layers', 'layerDefs', 'opacity',
    'position', 'maxZoom', 'minZoom', 'dynamicLayers', 'token', 'proxy', 'useCors'
  ],

  /**
   Url of ArcGIS REST service endpoint
  */
  url: null,

  layersIds: Ember.computed('url', 'esriLayers', function () {
    let esriLayers = this.get('esriLayers');

    if (Ember.isArray(esriLayers) && esriLayers.length !== 0) {
      return new Ember.RSVP.Promise((resolve, reject) => {
        resolve(esriLayers.map(layer => layer.id));
      });
    } else {
      let params = {
        f: 'json'
      };

      let url = this.get('url');
      url = url + L.Util.getParamString(params, url);

      return new Ember.RSVP.Promise((resolve, reject) => {

        Ember.$.ajax({
          url,
          success: function (data) {
            let result = JSON.parse(data);
            resolve(result.layers.filter(layer => !layer.subLayerIds).map(layer => layer.id));
          },
          error: function () {
            reject();
          }
        });
      });
    }
  }),

  _queryLayer(layerId, params) {
    params = Ember.$.extend(true, params, {
      f: 'json'
    });
    return new Ember.RSVP.Promise((resolve, reject) => {
      let url = this.get('url') + '/' + layerId + '/query';
      let crs = this.get('crs');
      url = url + L.Util.getParamString(params, url);

      Ember.$.ajax({
        url,
        success: function (data) {
          let result = JSON.parse(data);
          if (result.error) {
            reject(result.error);
            return;
          }

          let featureCollection = L.esri.Util.responseToFeatureCollection(result);
          let coordsToLatLng = function (coords) {
            var point = L.point(coords[0], coords[1]);
            var ll = crs.projection.unproject(point);
            if (coords[2]) {
              ll.alt = coords[2];
            }

            return ll;
          };

          resolve(L.geoJSON(featureCollection, {
            coordsToLatLng
          }));
        },
        error: function (e) {
          reject(e);
        }
      });
    });
  },

  _query(e) {
    let layerId = this.get('layerModel.id').toString();
    let layerLinks = e.layerLinks.filter(link => link.get('layerModel.id').toString() === layerId);
    if (!layerLinks.length) {
      return;
    }

    let queryFilter = e.queryFilter;

    e.results.push(new Ember.RSVP.Promise((resolve, reject) => {
      let result = L.featureGroup();

      this.get('layersIds')
        .then(layerIds => {
          let allQueries = [];
          layerIds.forEach((layerId) => {
            layerLinks.forEach((link) => {
              let params = {
                where: link.get('linkParameter').map(link => link.get('layerField') + '=' + queryFilter[link.get('queryKey')]).join(' and ')
              };

              allQueries.push(this._queryLayer(layerId, params));
            });
          });

          Ember.RSVP.all(allQueries).then(layers => {
            layers.forEach(layer => result.addLayer(layer));
            resolve(result);
          });
        })
        .catch((error) => {
          reject(error);
        });
    }));
  },

  _identifyEsri(boundingBox) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      let url = this.get('url') + '/identify';
      let crs = this.get('crs');
      let map = this.get('leafletMap');
      let size = map.getSize();

      let params = {
        f: 'json',
        geometryType: 'esriGeometryEnvelope',
        layers: 'visible',
        returnGeometry: true,
        tolerance: 5,
        geometry: projectBounds(boundingBox, crs),
        imageDisplay: [size.x, size.y, 96],
        mapExtent: projectBounds(map.getBounds(), crs)
      };

      url = url + L.Util.getParamString(params, url);

      let _this = this;

      Ember.$.ajax({
        url,
        success: function (data) {
          let result = JSON.parse(data);
          if (!Ember.isNone(result.error)) {
            reject(result.error);
          } else {
            let featureCollection = L.esri.Util.responseToFeatureCollection(result);
            _this.injectLeafletLayersIntoGeoJSON(featureCollection);
            resolve(Ember.A(Ember.get(featureCollection, 'features') || []));
          }
        },
        error: function (e) {
          reject(e);
{'fraction_non_alphanumeric': 0.15233340126350112, 'fraction_numerical': 0.0016303240269003464, 'mean_word_length': 2.046244568590937, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '6806205', 'n_tokens_mistral': 3075, 'n_tokens_neox': 2996, 'n_words': 568}