From 8fcd4c0b36f540d013dc72e903b24861ae04573e Mon Sep 17 00:00:00 2001
From: Armand Bahi <armand.bahi@veremes.com>
Date: Mon, 5 Nov 2018 12:36:28 +0100
Subject: [PATCH] Limite le calcul des polygones jointifs aux types polygon,
 multipolygon, geometry, geometrycollection

---
 .../javascript/app/vmap/tools/insert.js       | 61 ++++++++++++++++++-
 .../module/template/tools/insert.html         |  3 +-
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/src/module_vmap/module/javascript/app/vmap/tools/insert.js b/src/module_vmap/module/javascript/app/vmap/tools/insert.js
index 6e01c71f..afc68e8e 100644
--- a/src/module_vmap/module/javascript/app/vmap/tools/insert.js
+++ b/src/module_vmap/module/javascript/app/vmap/tools/insert.js
@@ -258,6 +258,11 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController = function ($scope,
      */
     $scope['aQueryableBOs'] = oVmap.getMapManager().getQueryableBusinessObjectsAsArray(true);
 
+    /**
+     * Business objects avaliable for avoiding intersections
+     */
+    $scope['aAvoidIntersectionsBOs'] = [];
+
     /**
      * Object to insert
      * oInsertObject.sFormDefinitionName name of the form (insert, upload, display)
@@ -367,8 +372,10 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController = function ($scope,
 
     // Au changement d'objet métier en cours d'insertion
     this.$scope_.$watch('selectedBoId', function () {
-        this_.updateInsertForm();
-        this_.selectCurrentBoForSnapping();
+        this_.updateInsertForm(null, function(){
+            this_.selectCurrentBoForSnapping();
+            this_.loadAvoidIntersectionBos();
+        });
     });
 
     // Affiche les modales en plein écran pour la version mobile
@@ -412,6 +419,7 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.loadInsert
     });
 
     this.checkEditionScale();
+    this.loadAvoidIntersectionBos();
 };
 
 /**
@@ -434,6 +442,40 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.selectCurr
     this.checkEditionScale();
 };
 
+
+/**
+ * Load this.aAvoidIntersectionsBOs
+ *
+ */
+nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.loadAvoidIntersectionBos = function () {
+    oVmap.log('nsVmap.nsToolsManager.Insert.inserttoolController.loadAvoidIntersectionBos');
+
+    var this_ = this;
+
+    // Types de géométries pour le calcul des jointures
+    var aActiveGeomTypes = ['POLYGON', 'MULTIPOLYGON', 'GEOMETRY', 'GEOMETRYCOLLECTION'];
+
+    // Objets métiers actifs pour la calcul des jointures
+    this.$scope_['aAvoidIntersectionsBOs'] = [];
+
+    if (goog.isDefAndNotNull(this.$scope_['oInsertObject']['sGeomType'])) {
+
+        // Si la couche en cours d'insertion est dans aActiveGeomTypes
+        if (aActiveGeomTypes.indexOf(this.$scope_['oInsertObject']['sGeomType']) !== -1) {
+            for (var i = 0; i < this_.$scope_['aQueryableBOs'].length; i++) {
+
+                // Si la couche cible est dans aActiveGeomTypes
+                if (goog.isDefAndNotNull(this_.$scope_['aQueryableBOs'][i]['bo_geom_type'])) {
+                    if(aActiveGeomTypes.indexOf(this_.$scope_['aQueryableBOs'][i]['bo_geom_type']) !== -1){
+
+                        this.$scope_['aAvoidIntersectionsBOs'].push(this_.$scope_['aQueryableBOs'][i]);
+                    }
+                }
+            }
+        }
+    }
+}
+
 /**
  * Vérifie que l'échelle en cours respecte les spécification de l'objet métier
  */
@@ -1144,7 +1186,6 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.updateCirc
     return feature;
 };
 
-
 /**
  * Edit the given feature to avoid intersections with layers defined in snapOptions.avoidIntersections
  * @param {ol.Feature} olFeature
@@ -1167,6 +1208,13 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.avoidInter
     }
 }
 
+/**
+ * Modify the feature geometry to avoid intersections with BOs in aIntersectionBos
+ *
+ * @param  {string} aIntersectionBos
+ * @param  {ol.Feature} olFeature
+ * @param  {object|undefines} opt_options
+ */
 nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.setDiffGeom_ = function (aIntersectionBos, olFeature, opt_options) {
     oVmap.log('nsVmap.nsToolsManager.Insert.inserttoolController.setDiffGeom_');
 
@@ -1201,6 +1249,13 @@ nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.setDiffGeo
 
 }
 
+/**
+ * Get the diff intersection geometry
+ *
+ * @param  {string} sBoId
+ * @param  {string} sEWKTGeom
+ * @return {promise}
+ */
 nsVmap.nsToolsManager.Insert.prototype.inserttoolController.prototype.getDiffGeom_ = function (sBoId, sEWKTGeom) {
     oVmap.log('nsVmap.nsToolsManager.Insert.inserttoolController.getDiffGeom_');
 
diff --git a/src/module_vmap/module/template/tools/insert.html b/src/module_vmap/module/template/tools/insert.html
index f10c71ac..a8937a7c 100644
--- a/src/module_vmap/module/template/tools/insert.html
+++ b/src/module_vmap/module/template/tools/insert.html
@@ -285,8 +285,7 @@
                             </div>
                         </div>
                         <div class="row"
-                                ng-repeat="bo in aQueryableBOs"
-                                ng-if="bo.bo_geom_type === 'POLYGON' || bo.bo_geom_type === 'MULTIPOLYGON' || bo.bo_geom_type === 'GEOMETRYCOLLECTION' || bo.bo_geom_type === 'GEOMETRY'">
+                                ng-repeat="bo in aAvoidIntersectionsBOs">
                             <div class="col-sm-5 text-right">
                                 <label>{{bo.bo_title}}</label>
                             </div>
-- 
GitLab