{"action":"created","comment":{"url":"https://api.github.com/repos/camptocamp/ngeo/pulls/comments/48824916","id":48824916,"diff_hunk":"@@ -0,0 +1,316 @@\n+goog.provide('ngeo.LayerHelper');\n+\n+goog.require('ngeo');\n+goog.require('ol.Collection');\n+goog.require('ol.format.WMTSCapabilities');\n+goog.require('ol.layer.Group');\n+goog.require('ol.layer.Image');\n+goog.require('ol.layer.Tile');\n+goog.require('ol.source.ImageWMS');\n+goog.require('ol.source.WMTS');\n+\n+\n+\n+/**\n+ * Provides helpers fonctions that helps you to create and manage layers.\n+ *\n+ * @param {angular.$q} $q Angular promises/deferred service.\n+ * @param {angular.$http} $http Angular http service.\n+ * @constructor\n+ * @ngdoc service\n+ * @ngname ngeoLayerHelper\n+ * @ngInject\n+ */\n+ngeo.LayerHelper = function($q, $http) {\n+\n+  /**\n+   * @type {boolean}\n+   * @private\n+   */\n+  this.initialized_ = false;\n+\n+  /**\n+   * @type {ol.Map}\n+   * @private\n+   */\n+  this.map_ = null;\n+\n+  /**\n+   * @type {angular.$q}\n+   * @private\n+   */\n+  this.$q_ = $q;\n+\n+  /**\n+   * @type {angular.$http}\n+   * @private\n+   */\n+  this.$http_ = $http;\n+\n+  /**\n+   * @type {string}\n+   * @export\n+   */\n+  this.helperID = 'helperID';\n+};\n+\n+\n+/**\n+ * Init the service.\n+ * @param {!ol.Map} map\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.init = function(map) {\n+  if (!this.initialized_) {\n+    this.map_ = map;\n+    this.initialized_ = true;\n+  }\n+};\n+\n+\n+/**\n+ * Add a HelperID to the given layer. Layers with this ID can be\n+ * managed by this sevice.\n+ * @param {ol.layer.Layer} layer The layer that needs an ID.\n+ * @param {string} layerURL Part of the ID.\n+ * @param {string} layerName Part of the ID.\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.setHelperID = function(layer, layerURL, layerName) {\n+  layer.set(this.helperID, this.makeHelperID(layerURL, layerName));\n+};\n+\n+\n+/**\n+ * Return an ID based on two strings ('layerURL_layerName');\n+ * @param {string} layerURL Part of the ID.\n+ * @param {string} layerName Part of the ID.\n+ * @return {string}\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.makeHelperID = function(layerURL, layerName) {\n+  return layerURL + '_' + layerName;\n+};\n+\n+\n+/**\n+ * Create and return a basic WMS layer with only a source URL and a dot\n+ * separated layers names (see {@link ol.source.ImageWMS}).\n+ * This layer will be taged by a a helperID.\n+ * @param {string} sourceURL The source URL.\n+ * @param {string} sourceLayersName A dot separated names string.\n+ * @return {ol.layer.Image}\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.createBasicWMSLayer = function(sourceURL,\n+    sourceLayersName) {\n+  var layer = new ol.layer.Image({\n+    source: new ol.source.ImageWMS({\n+      url: sourceURL,\n+      params: {'LAYERS': sourceLayersName}\n+    })\n+  });\n+  this.setHelperID(layer, sourceURL, sourceLayersName);\n+  return layer;\n+};\n+\n+\n+/**\n+ * Create and return a promise that provides a WMTS layer with source on\n+ * success, no layer else.\n+ * The WMTS layer source will be configured by the capabilities that are\n+ * loaded from the given capabilitiesUrl.\n+ * This layer will be taged by a a helperID.\n+ * @param {string} capabilitiesURL The getCapabilities url.\n+ * @param {string} layerName The name of te layer.\n+ * @return {angular.$q.Promise} A Promise with a layer (with source) on success,\n+ *     no layer else.\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.createWMTSLayerFromCapabilitites = function(\n+    capabilitiesURL, layerName) {\n+  var parser = new ol.format.WMTSCapabilities();\n+  var layer = new ol.layer.Tile();\n+  var deferred = this.$q_.defer();\n+  this.setHelperID(layer, capabilitiesURL, layerName);\n+\n+  this.$http_.get(capabilitiesURL).then(function(response) {\n+    var result;\n+    if (response.data) {\n+      result = parser.read(response.data);\n+    }\n+    if (goog.isDef(result)) {\n+      var options = ol.source.WMTS.optionsFromCapabilities(result,\n+          {layer: layerName, requestEncoding: 'REST'});\n+      layer.setSource(new ol.source.WMTS(options));\n+      deferred.resolve(layer);\n+    } else {\n+      deferred.resolve();\n+    }\n+  }, function(response) {\n+    deferred.resolve();\n+  });\n+\n+  return deferred.promise;\n+};\n+\n+\n+/**\n+ * Create and return an ol.layer.Group. You can pass a collection of layers to\n+ * directly add them in the returned group.\n+ * @param {ol.Collection.<ol.layer.Base>=} opt_layers The layer to add to the\n+ * returned Group.\n+ * @return {ol.layer.Group}\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.createBasicGroup = function(opt_layers) {\n+  var group = new ol.layer.Group();\n+  if (goog.isDefAndNotNull(opt_layers)) {\n+    group.setLayers(opt_layers);\n+  }\n+  return group;\n+};\n+\n+\n+/**\n+ * Get the position of the layer in the map's layers array or -1 if the layer\n+ * can't be found.\n+ * The layer must be taged by a helperID (return -1 else).\n+ * @param {ol.layer.Layer} layer The concerned layer.\n+ * @return {number} index or -1 if not found.\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.getLayerIndex = function(layer) {\n+  var layerId = layer.get(this.helperID);\n+  if (!goog.isDef(layerId)) {\n+    return -1;\n+  }\n+\n+  var i, layers = /** @type {Array.<ol.layer.Layer>} */\n+      (this.map_.getLayers().getArray());\n+  for (i = 0; i < layers.length; i++) {\n+    if (layers[i].get(this.helperID) === layerId) {\n+      return i;\n+    }\n+  }\n+  return -1;\n+};\n+\n+\n+/**\n+ * Retrieve a layer from the given array of layers and based on the given\n+ * layers's helperID. Return null if no layer match.\n+ * @param {Array.<ol.layer.Layer>} layers An array of layers.\n+ * @param {string} layerID an ID like one created by the setHelperID methode\n+ *   in this service\n+ * @return {ol.layer.Layer?} layer or null;\n+ * @export\n+ */\n+ngeo.LayerHelper.prototype.findLayer = function(layers, layerID) {\n+  var i, layer;\n+  for (i = 0; i < layers.length; i++) {\n+    layer = layers[i];\n+    if (layer.get(this.helperID) === layerID) {\n+      return layer;\n+    }\n+  }\n+  return null;\n+};\n+\n+\n+/**\n+ * Add the given layer on the map if it doesn't already exists and if the layer\n+ * has a source.\n+ * The layer must be taged by a helperID.","path":"src/services/layerHelper.js","position":224,"original_position":224,"commit_id":"a7f73506421f5f1249af0f7cb2aab2fd9f80cc18","original_commit_id":"a7f73506421f5f1249af0f7cb2aab2fd9f80cc18","user":{"login":"fgravin","id":1491924,"avatar_url":"https://avatars.githubusercontent.com/u/1491924?v=3","gravatar_id":"","url":"https://api.github.com/users/fgravin","html_url":"https://github.com/fgravin","followers_url":"https://api.github.com/users/fgravin/followers","following_url":"https://api.github.com/users/fgravin/following{/other_user}","gists_url":"https://api.github.com/users/fgravin/gists{/gist_id}","starred_url":"https://api.github.com/users/fgravin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fgravin/subscriptions","organizations_url":"https://api.github.com/users/fgravin/orgs","repos_url":"https://api.github.com/users/fgravin/repos","events_url":"https://api.github.com/users/fgravin/events{/privacy}","received_events_url":"https://api.github.com/users/fgravin/received_events","type":"User","site_admin":false},"body":"tagged","created_at":"2016-01-05T09:13:34Z","updated_at":"2016-01-05T09:13:34Z","html_url":"https://github.com/camptocamp/ngeo/pull/483#discussion_r48824916","pull_request_url":"https://api.github.com/repos/camptocamp/ngeo/pulls/483","_links":{"self":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/comments/48824916"},"html":{"href":"https://github.com/camptocamp/ngeo/pull/483#discussion_r48824916"},"pull_request":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/483"}}},"pull_request":{"url":"https://api.github.com/repos/camptocamp/ngeo/pulls/483","id":55066831,"html_url":"https://github.com/camptocamp/ngeo/pull/483","diff_url":"https://github.com/camptocamp/ngeo/pull/483.diff","patch_url":"https://github.com/camptocamp/ngeo/pull/483.patch","issue_url":"https://api.github.com/repos/camptocamp/ngeo/issues/483","number":483,"state":"open","locked":false,"title":"Add layerHelper service","user":{"login":"ger-benjamin","id":1792111,"avatar_url":"https://avatars.githubusercontent.com/u/1792111?v=3","gravatar_id":"","url":"https://api.github.com/users/ger-benjamin","html_url":"https://github.com/ger-benjamin","followers_url":"https://api.github.com/users/ger-benjamin/followers","following_url":"https://api.github.com/users/ger-benjamin/following{/other_user}","gists_url":"https://api.github.com/users/ger-benjamin/gists{/gist_id}","starred_url":"https://api.github.com/users/ger-benjamin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ger-benjamin/subscriptions","organizations_url":"https://api.github.com/users/ger-benjamin/orgs","repos_url":"https://api.github.com/users/ger-benjamin/repos","events_url":"https://api.github.com/users/ger-benjamin/events{/privacy}","received_events_url":"https://api.github.com/users/ger-benjamin/received_events","type":"User","site_admin":false},"body":"Will be used by the layertree and the backgroundmanager","created_at":"2016-01-05T08:47:37Z","updated_at":"2016-01-05T09:13:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b98e370f752029583d2d980493ef779dc0dac510","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/camptocamp/ngeo/pulls/483/commits","review_comments_url":"https://api.github.com/repos/camptocamp/ngeo/pulls/483/comments","review_comment_url":"https://api.github.com/repos/camptocamp/ngeo/pulls/comments{/number}","comments_url":"https://api.github.com/repos/camptocamp/ngeo/issues/483/comments","statuses_url":"https://api.github.com/repos/camptocamp/ngeo/statuses/a7f73506421f5f1249af0f7cb2aab2fd9f80cc18","head":{"label":"ger-benjamin:layerHelper","ref":"layerHelper","sha":"a7f73506421f5f1249af0f7cb2aab2fd9f80cc18","user":{"login":"ger-benjamin","id":1792111,"avatar_url":"https://avatars.githubusercontent.com/u/1792111?v=3","gravatar_id":"","url":"https://api.github.com/users/ger-benjamin","html_url":"https://github.com/ger-benjamin","followers_url":"https://api.github.com/users/ger-benjamin/followers","following_url":"https://api.github.com/users/ger-benjamin/following{/other_user}","gists_url":"https://api.github.com/users/ger-benjamin/gists{/gist_id}","starred_url":"https://api.github.com/users/ger-benjamin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ger-benjamin/subscriptions","organizations_url":"https://api.github.com/users/ger-benjamin/orgs","repos_url":"https://api.github.com/users/ger-benjamin/repos","events_url":"https://api.github.com/users/ger-benjamin/events{/privacy}","received_events_url":"https://api.github.com/users/ger-benjamin/received_events","type":"User","site_admin":false},"repo":{"id":45966295,"name":"ngeo","full_name":"ger-benjamin/ngeo","owner":{"login":"ger-benjamin","id":1792111,"avatar_url":"https://avatars.githubusercontent.com/u/1792111?v=3","gravatar_id":"","url":"https://api.github.com/users/ger-benjamin","html_url":"https://github.com/ger-benjamin","followers_url":"https://api.github.com/users/ger-benjamin/followers","following_url":"https://api.github.com/users/ger-benjamin/following{/other_user}","gists_url":"https://api.github.com/users/ger-benjamin/gists{/gist_id}","starred_url":"https://api.github.com/users/ger-benjamin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ger-benjamin/subscriptions","organizations_url":"https://api.github.com/users/ger-benjamin/orgs","repos_url":"https://api.github.com/users/ger-benjamin/repos","events_url":"https://api.github.com/users/ger-benjamin/events{/privacy}","received_events_url":"https://api.github.com/users/ger-benjamin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ger-benjamin/ngeo","description":"Library combining OpenLayers 3 and AngularJS","fork":true,"url":"https://api.github.com/repos/ger-benjamin/ngeo","forks_url":"https://api.github.com/repos/ger-benjamin/ngeo/forks","keys_url":"https://api.github.com/repos/ger-benjamin/ngeo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ger-benjamin/ngeo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ger-benjamin/ngeo/teams","hooks_url":"https://api.github.com/repos/ger-benjamin/ngeo/hooks","issue_events_url":"https://api.github.com/repos/ger-benjamin/ngeo/issues/events{/number}","events_url":"https://api.github.com/repos/ger-benjamin/ngeo/events","assignees_url":"https://api.github.com/repos/ger-benjamin/ngeo/assignees{/user}","branches_url":"https://api.github.com/repos/ger-benjamin/ngeo/branches{/branch}","tags_url":"https://api.github.com/repos/ger-benjamin/ngeo/tags","blobs_url":"https://api.github.com/repos/ger-benjamin/ngeo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ger-benjamin/ngeo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ger-benjamin/ngeo/git/refs{/sha}","trees_url":"https://api.github.com/repos/ger-benjamin/ngeo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ger-benjamin/ngeo/statuses/{sha}","languages_url":"https://api.github.com/repos/ger-benjamin/ngeo/languages","stargazers_url":"https://api.github.com/repos/ger-benjamin/ngeo/stargazers","contributors_url":"https://api.github.com/repos/ger-benjamin/ngeo/contributors","subscribers_url":"https://api.github.com/repos/ger-benjamin/ngeo/subscribers","subscription_url":"https://api.github.com/repos/ger-benjamin/ngeo/subscription","commits_url":"https://api.github.com/repos/ger-benjamin/ngeo/commits{/sha}","git_commits_url":"https://api.github.com/repos/ger-benjamin/ngeo/git/commits{/sha}","comments_url":"https://api.github.com/repos/ger-benjamin/ngeo/comments{/number}","issue_comment_url":"https://api.github.com/repos/ger-benjamin/ngeo/issues/comments{/number}","contents_url":"https://api.github.com/repos/ger-benjamin/ngeo/contents/{+path}","compare_url":"https://api.github.com/repos/ger-benjamin/ngeo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ger-benjamin/ngeo/merges","archive_url":"https://api.github.com/repos/ger-benjamin/ngeo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ger-benjamin/ngeo/downloads","issues_url":"https://api.github.com/repos/ger-benjamin/ngeo/issues{/number}","pulls_url":"https://api.github.com/repos/ger-benjamin/ngeo/pulls{/number}","milestones_url":"https://api.github.com/repos/ger-benjamin/ngeo/milestones{/number}","notifications_url":"https://api.github.com/repos/ger-benjamin/ngeo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ger-benjamin/ngeo/labels{/name}","releases_url":"https://api.github.com/repos/ger-benjamin/ngeo/releases{/id}","created_at":"2015-11-11T07:25:12Z","updated_at":"2015-11-11T07:25:15Z","pushed_at":"2016-01-05T09:09:54Z","git_url":"git://github.com/ger-benjamin/ngeo.git","ssh_url":"git@github.com:ger-benjamin/ngeo.git","clone_url":"https://github.com/ger-benjamin/ngeo.git","svn_url":"https://github.com/ger-benjamin/ngeo","homepage":null,"size":13946,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"camptocamp:master","ref":"master","sha":"dabc3ca80d762a82a36ddeed5d4076a4c94a91ef","user":{"login":"camptocamp","id":28109,"avatar_url":"https://avatars.githubusercontent.com/u/28109?v=3","gravatar_id":"","url":"https://api.github.com/users/camptocamp","html_url":"https://github.com/camptocamp","followers_url":"https://api.github.com/users/camptocamp/followers","following_url":"https://api.github.com/users/camptocamp/following{/other_user}","gists_url":"https://api.github.com/users/camptocamp/gists{/gist_id}","starred_url":"https://api.github.com/users/camptocamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/camptocamp/subscriptions","organizations_url":"https://api.github.com/users/camptocamp/orgs","repos_url":"https://api.github.com/users/camptocamp/repos","events_url":"https://api.github.com/users/camptocamp/events{/privacy}","received_events_url":"https://api.github.com/users/camptocamp/received_events","type":"Organization","site_admin":false},"repo":{"id":20212761,"name":"ngeo","full_name":"camptocamp/ngeo","owner":{"login":"camptocamp","id":28109,"avatar_url":"https://avatars.githubusercontent.com/u/28109?v=3","gravatar_id":"","url":"https://api.github.com/users/camptocamp","html_url":"https://github.com/camptocamp","followers_url":"https://api.github.com/users/camptocamp/followers","following_url":"https://api.github.com/users/camptocamp/following{/other_user}","gists_url":"https://api.github.com/users/camptocamp/gists{/gist_id}","starred_url":"https://api.github.com/users/camptocamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/camptocamp/subscriptions","organizations_url":"https://api.github.com/users/camptocamp/orgs","repos_url":"https://api.github.com/users/camptocamp/repos","events_url":"https://api.github.com/users/camptocamp/events{/privacy}","received_events_url":"https://api.github.com/users/camptocamp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/camptocamp/ngeo","description":"Library combining OpenLayers 3 and AngularJS","fork":false,"url":"https://api.github.com/repos/camptocamp/ngeo","forks_url":"https://api.github.com/repos/camptocamp/ngeo/forks","keys_url":"https://api.github.com/repos/camptocamp/ngeo/keys{/key_id}","collaborators_url":"https://api.github.com/repos/camptocamp/ngeo/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/camptocamp/ngeo/teams","hooks_url":"https://api.github.com/repos/camptocamp/ngeo/hooks","issue_events_url":"https://api.github.com/repos/camptocamp/ngeo/issues/events{/number}","events_url":"https://api.github.com/repos/camptocamp/ngeo/events","assignees_url":"https://api.github.com/repos/camptocamp/ngeo/assignees{/user}","branches_url":"https://api.github.com/repos/camptocamp/ngeo/branches{/branch}","tags_url":"https://api.github.com/repos/camptocamp/ngeo/tags","blobs_url":"https://api.github.com/repos/camptocamp/ngeo/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/camptocamp/ngeo/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/camptocamp/ngeo/git/refs{/sha}","trees_url":"https://api.github.com/repos/camptocamp/ngeo/git/trees{/sha}","statuses_url":"https://api.github.com/repos/camptocamp/ngeo/statuses/{sha}","languages_url":"https://api.github.com/repos/camptocamp/ngeo/languages","stargazers_url":"https://api.github.com/repos/camptocamp/ngeo/stargazers","contributors_url":"https://api.github.com/repos/camptocamp/ngeo/contributors","subscribers_url":"https://api.github.com/repos/camptocamp/ngeo/subscribers","subscription_url":"https://api.github.com/repos/camptocamp/ngeo/subscription","commits_url":"https://api.github.com/repos/camptocamp/ngeo/commits{/sha}","git_commits_url":"https://api.github.com/repos/camptocamp/ngeo/git/commits{/sha}","comments_url":"https://api.github.com/repos/camptocamp/ngeo/comments{/number}","issue_comment_url":"https://api.github.com/repos/camptocamp/ngeo/issues/comments{/number}","contents_url":"https://api.github.com/repos/camptocamp/ngeo/contents/{+path}","compare_url":"https://api.github.com/repos/camptocamp/ngeo/compare/{base}...{head}","merges_url":"https://api.github.com/repos/camptocamp/ngeo/merges","archive_url":"https://api.github.com/repos/camptocamp/ngeo/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/camptocamp/ngeo/downloads","issues_url":"https://api.github.com/repos/camptocamp/ngeo/issues{/number}","pulls_url":"https://api.github.com/repos/camptocamp/ngeo/pulls{/number}","milestones_url":"https://api.github.com/repos/camptocamp/ngeo/milestones{/number}","notifications_url":"https://api.github.com/repos/camptocamp/ngeo/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/camptocamp/ngeo/labels{/name}","releases_url":"https://api.github.com/repos/camptocamp/ngeo/releases{/id}","created_at":"2014-05-27T08:34:54Z","updated_at":"2015-12-23T05:21:15Z","pushed_at":"2016-01-05T09:09:48Z","git_url":"git://github.com/camptocamp/ngeo.git","ssh_url":"git@github.com:camptocamp/ngeo.git","clone_url":"https://github.com/camptocamp/ngeo.git","svn_url":"https://github.com/camptocamp/ngeo","homepage":null,"size":19760,"stargazers_count":53,"watchers_count":53,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":31,"mirror_url":null,"open_issues_count":36,"forks":31,"open_issues":36,"watchers":53,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/483"},"html":{"href":"https://github.com/camptocamp/ngeo/pull/483"},"issue":{"href":"https://api.github.com/repos/camptocamp/ngeo/issues/483"},"comments":{"href":"https://api.github.com/repos/camptocamp/ngeo/issues/483/comments"},"review_comments":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/483/comments"},"review_comment":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/camptocamp/ngeo/pulls/483/commits"},"statuses":{"href":"https://api.github.com/repos/camptocamp/ngeo/statuses/a7f73506421f5f1249af0f7cb2aab2fd9f80cc18"}}}}