view client/src/map/Maplayer.vue @ 703:e9c28c42c927

client: improve WFS loader * Remove superfluous projection setting for riverkilometre. * Adjust comment to reflect that we do not need to set srsName anymore.
author Bernhard Reiter <bernhard@intevation.de>
date Fri, 21 Sep 2018 00:24:41 +0200
parents aaf5dbfb9512
children 6aa09d12157f
line wrap: on
line source

<template>
    <div id="map" :class="mapStyle"></div>
</template>

<style lang="scss">
.mapsplit {
  height: 50vh;
}

.mapfull {
  height: 100vh;
}
</style>

<script>
import { HTTP } from "../application/lib/http";
import "ol/ol.css";
import { Map, View } from "ol";
import { bbox as bboxFilter } from "ol/format/filter.js";
import { WFS, GeoJSON } from "ol/format.js";
import { mapGetters } from "vuex";
import Draw from "ol/interaction/Draw.js";
import { Vector as VectorSource } from "ol/source.js";
import { Vector as VectorLayer } from "ol/layer.js";

export default {
  name: "maplayer",
  props: ["drawMode", "lat", "long", "zoom", "split"],
  data() {
    return {
      projection: "EPSG:3857",
      openLayersMap: null,
      interaction: null,
      vectorLayer: null,
      vectorSource: null
    };
  },
  computed: {
    ...mapGetters("mapstore", ["layers"]),
    mapStyle() {
      return {
        mapfull: !this.split,
        mapsplit: this.split
      };
    },
    layerData() {
      const l = this.layers.map(x => {
        return x.data;
      });
      return [...l, this.vectorLayer];
    }
  },
  methods: {
    createVectorSource() {
      this.vectorSource = new VectorSource({ wrapX: false });
    },
    createVectorLayer() {
      this.vectorLayer = new VectorLayer({
        source: this.vectorSource
      });
    },
    removeCurrentInteraction() {
      this.openLayersMap.removeInteraction(this.interaction);
      this.interaction = null;
    },
    createInteraction() {
      return new Draw({
        source: this.vectorSource,
        type: this.drawMode
      });
    },
    activateInteraction() {
      const interaction = this.createInteraction(this.drawMode);
      this.interaction = interaction;
      this.openLayersMap.addInteraction(interaction);
    },
    buildVectorLoader(featureRequestOptions, endpoint, vectorSource) {
      // build a function to be used for VectorSource.setLoader()
      // make use of WFS().writeGetFeature to build the request
      // and use our HTTP library to actually do it
      // NOTE: a) the geometryName has to be given in featureRequestOptions,
      //          because we want to load depending on the bbox
      //  b) the VectorSource has to have the option strategy: bbox
      featureRequestOptions["outputFormat"] = "application/json";
      var loader = function(extent, resolution, projection) {
        featureRequestOptions["bbox"] = extent;
        featureRequestOptions["srsName"] = projection.getCode();
        var featureRequest = new WFS().writeGetFeature(featureRequestOptions);
        // DEBUG console.log(featureRequest);
        HTTP.post(
          endpoint,
          new XMLSerializer().serializeToString(featureRequest),
          {
            headers: {
              "X-Gemma-Auth": localStorage.getItem("token"),
              "Content-type": "text/xml; charset=UTF-8"
            }
          }
        )
          .then(response => {
            var features = new GeoJSON().readFeatures(
              JSON.stringify(response.data)
            );
            vectorSource.addFeatures(features);
            console.log(
              "loaded",
              features.length,
              featureRequestOptions.featureTypes,
              "features"
            );
            // DEBUG console.log("loaded ", features, "for", vectorSource);
            // eslint-disable-next-line
        }).catch(error => {
            vectorSource.removeLoadedExtent(extent);
          });
      };
      return loader;
    }
  },
  watch: {
    drawMode() {
      if (this.interaction) {
        this.removeCurrentInteraction();
      } else {
        this.activateInteraction();
      }
    },
    split() {
      const map = this.openLayersMap;
      this.$nextTick(() => {
        map.updateSize();
      });
    }
  },
  mounted() {
    this.createVectorSource();
    this.createVectorLayer();
    this.openLayersMap = new Map({
      layers: this.layerData,
      target: "map",
      controls: [],
      view: new View({
        center: [this.long, this.lat],
        zoom: this.zoom,
        projection: this.projection
      })
    });

    var featureRequest2 = new WFS().writeGetFeature({
      // srsName: "urn:ogc:def:crs:EPSG::4326",
      srsName: "EPSG:3857",
      featureNS: "gemma",
      featurePrefix: "gemma",
      featureTypes: ["fairway_dimensions"],
      outputFormat: "application/json"
    });

    HTTP.post(
      "/internal/wfs",
      new XMLSerializer().serializeToString(featureRequest2),
      {
        headers: {
          "X-Gemma-Auth": localStorage.getItem("token"),
          "Content-type": "text/xml; charset=UTF-8"
        }
      }
    ).then(response => {
      var features = new GeoJSON().readFeatures(JSON.stringify(response.data));
      var vectorSrc = this.layers[2].data.getSource();
      vectorSrc.addFeatures(features);
      // would scale to the extend of all resulting features
      // this.openLayersMap.getView().fit(vectorSrc.getExtent());
    });

    // FIXME hardwired for now
    var featureRequestOptions3 = {
      featurePrefix: "ws-wamos",
      featureTypes: ["ienc_wtware"],
      geometryName: "geom"
    };

    this.layers[3].data
      .getSource()
      .setLoader(
        this.buildVectorLoader(
          featureRequestOptions3,
          "/external/d4d",
          this.layers[3].data.getSource()
        )
      );

    // FIXME hardwired for now
    var featureRequest4 = new WFS().writeGetFeature({
      featurePrefix: "ws-wamos",
      featureTypes: ["ienc_wtwaxs"],
      outputFormat: "application/json"
    });

    HTTP.post(
      "/external/d4d",
      new XMLSerializer().serializeToString(featureRequest4),
      {
        headers: {
          "X-Gemma-Auth": localStorage.getItem("token"),
          "Content-type": "text/xml; charset=UTF-8"
        }
      }
    ).then(response => {
      var features = new GeoJSON().readFeatures(JSON.stringify(response.data));
      var vectorSrc = this.layers[4].data.getSource();
      vectorSrc.addFeatures(features);
    });

    // FIXME this is hardwired for now to try for an external point layer
    var featureRequestOptions5 = {
      featurePrefix: "ws-wamos",
      featureTypes: ["ienc_dismar"],
      geometryName: "geom",
      /* restrict loading approximately to extend of danube in Austria */
      filter: bboxFilter("geom", [13.3, 48.0, 17.1, 48.6], "EPSG:4326")
    };

    this.layers[5].data
      .getSource()
      .setLoader(
        this.buildVectorLoader(
          featureRequestOptions5,
          "/external/d4d",
          this.layers[5].data.getSource()
        )
      );
    this.layers[5].data.setVisible(this.layers[5].isVisible);

    // FIXME this is hardwired for now
    var featureRequestOptions6 = {
      featureNS: "gemma",
      featurePrefix: "gemma",
      featureTypes: ["distance_marks_geoserver"],
      geometryName: "geom"
    };

    this.layers[6].data
      .getSource()
      .setLoader(
        this.buildVectorLoader(
          featureRequestOptions6,
          "/internal/wfs",
          this.layers[6].data.getSource()
        )
      );
  }
};
</script>