Skip to content

Commit

Permalink
feature: post method for wms and wfs (origo-map#1756)
Browse files Browse the repository at this point in the history
* post method for wms and wfs

* Update wfssource.js

* Fixed wms
  • Loading branch information
jokd committed May 2, 2023
1 parent 2f01fb3 commit b3beca3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/layer/wfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export default function wfs(layerOptions, viewer) {
wfsOptions.visible = true;
}
sourceOptions.strategy = layerOptions.strategy ? layerOptions.strategy : sourceOptions.strategy;
sourceOptions.requestMethod = layerOptions.requestMethod ? layerOptions.requestMethod : sourceOptions.requestMethod;
switch (sourceOptions.strategy) {
case 'all':
sourceOptions.loadingstrategy = LoadingStrategy.all;
Expand Down
29 changes: 26 additions & 3 deletions src/layer/wfssource.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ class WfsSource extends VectorSource {
.catch(() => failure());
}

/**
* Set request method for layer
* @param {any} method
*/
setMethod(method) {
this._options.requestMethod = method;
}

/**
* Set filter on layer
* @param {any} cql
Expand Down Expand Up @@ -128,9 +136,24 @@ class WfsSource extends VectorSource {
url = encodeURI(url);

// Actually fetch some features
const JsonFeatures = await fetch(url).then(response => response.json({
cache: false
}));
let JsonFeatures;
if (this._options.requestMethod && this._options.requestMethod.toLowerCase() === 'post') { // POST request
const split = url.split('?');
JsonFeatures = await fetch(split[0], {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
body: split[1]
}).then(response => response.json({
cache: false
}));
} else { // GET request
JsonFeatures = await fetch(url).then(response => response.json({
cache: false
}));
}

const features = super.getFormat().readFeatures(JsonFeatures);
// Delete the geometry if it is a table (ignoring geometry) as the GeoJSON loader creates an empty geometry and that will
// mess up saving the feature
Expand Down
34 changes: 34 additions & 0 deletions src/layer/wms.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function createTileSource(options) {
STYLES: options.style
}
};
if (options.imageLoadFunction) {
sourceOptions.tileLoadFunction = options.imageLoadFunction;
}
if (options.params) {
Object.keys(options.params).forEach((element) => {
sourceOptions.params[element] = options.params[element];
Expand All @@ -41,6 +44,9 @@ function createImageSource(options) {
STYLES: options.style
}
};
if (options.imageLoadFunction) {
sourceOptions.imageLoadFunction = options.imageLoadFunction;
}
if (options.params) {
Object.keys(options.params).forEach((element) => {
sourceOptions.params[element] = options.params[element];
Expand Down Expand Up @@ -98,6 +104,29 @@ function createWmsStyle({ wmsOptions, source, viewer, initialStyle = false }) {
return styleName;
}

function imageLoadFunction(loadedImage, src) {
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.addEventListener('loadend', function loaded() {
const data = this.response;
if (data) {
const img = loadedImage.getImage();
const url = URL.createObjectURL(data);
img.addEventListener('loadend', () => {
URL.revokeObjectURL(url);
});
img.src = url;
}
});
const split = src.split('?');
xhr.open('POST', split[0]);
xhr.setRequestHeader(
'Content-type',
'application/x-www-form-urlencoded'
);
xhr.send(split[1]);
}

function createWmsLayer(wmsOptions, source, viewer) {
const wmsOpts = wmsOptions;
const wmsSource = source;
Expand Down Expand Up @@ -153,6 +182,7 @@ const wms = function wms(layerOptions, viewer) {
sourceOptions.filterType = wmsOptions.filterType;
sourceOptions.params = wmsOptions.sourceParams;
sourceOptions.format = wmsOptions.format ? wmsOptions.format : sourceOptions.format;
sourceOptions.requestMethod = wmsOptions.requestMethod ? wmsOptions.requestMethod : sourceOptions.requestMethod;
if (!wmsOptions.stylePicker) {
const styleSettings = viewer.getStyle(wmsOptions.styleName);
const wmsStyleObject = styleSettings ? styleSettings[0].find(s => s.wmsStyle) : undefined;
Expand All @@ -172,6 +202,10 @@ const wms = function wms(layerOptions, viewer) {
}
}

if (sourceOptions.requestMethod && sourceOptions.requestMethod === 'post') {
sourceOptions.imageLoadFunction = imageLoadFunction;
}

if (renderMode === 'image') {
const source = createImageSource(sourceOptions);
createWmsLayer(wmsOptions, source, viewer);
Expand Down

0 comments on commit b3beca3

Please sign in to comment.