GetFeature.js

This is about glossary term: GetFeature.js

// @require WFS_URL
if (!WFS_URL) throw "Missing WFS_URL"

function p5WFS_ParseFeatureMember(featureMember) { // @returns object from xml element <gml:featureMemeber>
	if (!featureMember) return null
	if (!featureMember.children.length) return null
	var featureNode = featureMember.children[0]
	if (!featureNode) return null
	if (!featureNode.children.length) return null
	var item = {}
	var i = 0
	for (i = 0; i < featureNode.children.length; i++) {
		var field = featureNode.children[i]
		item[ field.localName ] = field.textContent
	}
	return item
}

/**
 * @usage

	p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
		'featureID': 'CRM_PROCES.' + id,
	}).then(function (features) {
		// ...
	}).catch(function (e) {
		// ...
	})

 * @usage - expected one feature

	p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
		'featureID': 'CRM_PROCES.' + id,
	}).then(function (features) {
		if (!features) throw "Nie odnaleziono rekordu id = " + id
		if (!features.length) throw "Nie odnaleziono rekordu id = " + id
		if (1 !== features.length) throw "Bład: API zwróciło za dużo rekordów"
		return features[0]
		console.log('fetched features', features)
	}).then(function (feature) {
		// ...
	}).catch(function (e) {
		// ...
	})

 * @usage - POST

	p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
	 // 'Filter': '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>',
	 'sortBy': 'SORT_PRIO+A,ID',
	 'ogc:Filter': '<GetFeature xmlns="http://www.opengis.net/wfs/2.0"'+
		 ' xmlns:p5_default_db="' + BASE_WFS_URL + '/default_db"' +
		 ' xmlns:ogc="http://www.opengis.net/ogc"' +
		 ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
		 ' service="WFS"' +
		 ' version="2.0.2"' +
		 ' xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd">' +
		 '<ogc:Filter>' +
		 '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>' +
		 '</ogc:Filter>' +
		 '</GetFeature>'
	 }).then(function (features) {
 	...

 */
function p5WFS_GetFeature(typeName, query) { // @returns Promise
	var postData = null
	var link = WFS_URL + '?SERVICE=WFS&VERSION=1.0.0' +
		'&REQUEST=GetFeature' +
		'&TYPENAME=' + typeName +
		'&SRSNAME=EPSG:3003'
	//	'&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>ID</ogc:PropertyName><ogc:Literal>' + id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>'
	var query = query || {}
	if ('featureID' in query) link += '&featureID=' + query['featureID']
	if ('Filter' in query && 'string' === typeof query['Filter']) {
		var ogcFilter = ('<ogc:Filter>' !== query['Filter'].substr(0, '<ogc:Filter>'.length))
			? '<ogc:Filter>' + query['Filter'] + '</ogc:Filter>'
			: query['Filter']
		;
		link += '&Filter=' + ogcFilter
	}
	if ('ogc:Filter' in query && 'string' === typeof query['ogc:Filter']) {
		postData = query['ogc:Filter']
	}
	if ('sortBy' in query) link += '&sortBy=' + query['sortBy']
	var method = (postData) ? 'POST' : 'GET' // TODO: if long 'ogc:Filter' then POST

	return window.fetch(link, Object.assign({
		method: method,
		credentials: 'same-origin',
	}, ('POST' === method)
		? {
			body: postData
		}
		: {}
	)).then(function (response) {
		return response.text()
	}).then(function (responseText) {
		return (new DOMParser()).parseFromString(responseText, "text/xml");
	}).then(function (xmlResponse) {
		if (!xmlResponse.children.length) throw "Missing response xml root element"
		return xmlResponse.children[0]
	}).then(function (xmlRoot) {
		if ('FeatureCollection' !== xmlRoot.localName) throw "Missing FeatureCollection as root element in wfs response"
		var features = []
		for (var i = 0; i < xmlRoot.children.length; i++) {
			features.push(p5WFS_ParseFeatureMember(xmlRoot.children[i]))
		}
		return features.filter(function (item) { return !!item; })
	})
}

global.p5WFS_ParseFeatureMember = p5WFS_ParseFeatureMember
global.p5WFS_GetFeature = p5WFS_GetFeature
Note: This example may differ from attached file
GetFeature.js