changeset 5177:6a22e007b160

merge with detectvisiblesoundings
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 22 Apr 2020 13:55:33 +0200
parents 6dc74d7e6582 (current diff) 698d0971ddc5 (diff)
children 2e97bd2952cf
files
diffstat 1 files changed, 107 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/Pdftool.vue	Wed Apr 15 11:28:30 2020 +0200
+++ b/client/src/components/Pdftool.vue	Wed Apr 22 13:55:33 2020 +0200
@@ -119,6 +119,9 @@
 import { displayError } from "@/lib/errors";
 import { pdfgen, templateLoader } from "@/lib/mixins";
 import sanitize from "sanitize-filename";
+import { WFS } from "ol/format";
+import { equalTo as equalToFilter } from "ol/format/filter";
+import { intersects } from "ol/extent";
 
 const paperSizes = {
   // in millimeter, landscape [width, height]
@@ -205,18 +208,6 @@
     ...mapGetters("map", ["openLayersMap"]),
     generatePdfLable() {
       return this.$gettext("Generate PDF");
-    },
-    filename() {
-      let filename = "map";
-      if (this.bottleneckForPrint) {
-        // TODO: Check if the view contains the selected bottleneck
-        // to avoid including bottleneck info in pdf in case view has changed to another location
-        filename = `BN-${sanitize(this.bottleneckForPrint).replace(/ /g, "-")}`;
-        if (this.selectedSurvey) {
-          filename += "-sr" + this.selectedSurvey.date_info.replace(/-/g, "");
-        }
-      }
-      return `${filename}-${this.dateForPDF()}.pdf`;
     }
   },
   methods: {
@@ -261,7 +252,69 @@
       this.form.paperSize = this.templateData.properties.paperSize;
       this.form.resolution = this.templateData.properties.resolution;
     },
+    numberSoundingsVisible() {
+      return new Promise((resolve, reject) => {
+        const map = this.openLayersMap();
+        const currentExtent = map.getView().calculateExtent(map.getSize());
+        const params = {
+          srsName: "EPSG:3857",
+          featureNS: "gemma",
+          featurePrefix: "gemma",
+          featureTypes: ["sounding_results_areas_geoserver"],
+          outputFormat: "application/json",
+          resultType: "hits",
+          bbox: currentExtent,
+          geometryName: "areas"
+        };
+        if (this.selectedSurvey) {
+          params["filter"] = equalToFilter(
+            "bottleneck_id",
+            this.selectedSurvey.bottleneck_id
+          );
+        }
+        const getSoundingResultFeatures = new WFS().writeGetFeature(params);
+        HTTP.post(
+          "/internal/wfs",
+          new XMLSerializer().serializeToString(getSoundingResultFeatures),
+          {
+            headers: {
+              "X-Gemma-Auth": localStorage.getItem("token"),
+              "Content-type": "text/xml; charset=UTF-8"
+            }
+          }
+        )
+          .then(response => {
+            resolve(response);
+          })
+          .catch(error => {
+            reject(error);
+          });
+      });
+    },
     download() {
+      this.numberSoundingsVisible()
+        .then(response => {
+          const parser = new DOMParser();
+          const responseXML = parser.parseFromString(response.data, "text/xml");
+          const totalNumber = responseXML
+            .getElementsByTagName("wfs:FeatureCollection")[0]
+            .getAttribute("numberOfFeatures");
+          this.generatePDF(totalNumber > 0);
+        })
+        .catch(error => {
+          console.log(error);
+          let message = "Backend not reachable";
+          if (error.response) {
+            const { status, data } = error.response;
+            message = `${status}: ${data.message || data}`;
+          }
+          displayError({
+            title: this.$gettext("Backend Error"),
+            message: message
+          });
+        });
+    },
+    generatePDF(soundingsVisible) {
       /**
        * In order to generate the image with the appropriate resolution
        * we have to temporaily scale the visible part of the map.
@@ -383,7 +436,8 @@
                   e.offset || defaultOffset,
                   e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
                   e.color || defaultTextColor,
-                  e.brcolor || defaultBorderColor
+                  e.brcolor || defaultBorderColor,
+                  soundingsVisible
                 );
                 break;
               }
@@ -392,7 +446,8 @@
                   e.position,
                   e.offset || defaultOffset,
                   e.rounding === 0 || e.rounding ? e.rounding : defaultRounding,
-                  e.brcolor || defaultBorderColor
+                  e.brcolor || defaultBorderColor,
+                  soundingsVisible
                 );
                 break;
               }
@@ -427,7 +482,33 @@
               }
             }
           });
-          this.pdf.doc.save(this.filename);
+          // Check if the bottlenck in the current view Extent
+          const isBottlenckVisible = () => {
+            const currentExtent = map.getView().calculateExtent(map.getSize());
+            const btnExtent = map
+              .getLayer("BOTTLENECKS")
+              .getSource()
+              .getFeatures()
+              .find(f => f.get("objnam") === this.bottleneckForPrint)
+              .getGeometry()
+              .getExtent();
+            return intersects(currentExtent, btnExtent);
+          };
+          let filename = "map";
+          if (
+            this.bottleneckForPrint &&
+            (soundingsVisible || isBottlenckVisible())
+          ) {
+            filename = `BN-${sanitize(this.bottleneckForPrint).replace(
+              / /g,
+              "-"
+            )}`;
+            if (this.selectedSurvey) {
+              filename +=
+                "-sr" + this.selectedSurvey.date_info.replace(/-/g, "");
+            }
+          }
+          this.pdf.doc.save(`${filename}-${this.dateForPDF()}.pdf`);
         }
         map.setSize(this.mapSize);
         map.getView().setResolution(this.resolution);
@@ -656,8 +737,9 @@
       this.pdf.doc.setFontStyle("normal");
       this.pdf.doc.text(size < 3 ? x1 - 0.5 : x1 - 1.3, y3 + 1, "N");
     },
-    addLegend(position, offset, rounding, brcolor) {
+    addLegend(position, offset, rounding, brcolor, soundingsVisible) {
       if (
+        soundingsVisible &&
         this.bottleneckForPrint &&
         this.selectedSurvey &&
         this.openLayersMap()
@@ -697,8 +779,16 @@
         );
       }
     },
-    addBottleneckInfo(position, offset, rounding, color, brcolor) {
+    addBottleneckInfo(
+      position,
+      offset,
+      rounding,
+      color,
+      brcolor,
+      soundingsVisible
+    ) {
       if (
+        soundingsVisible &&
         this.bottleneckForPrint &&
         this.selectedSurvey &&
         this.openLayersMap()