| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // @require WFS_URL
- if (!WFS_URL) throw "Missing WFS_URL"
- // wfsFeature json format:
- /*
- {
- $fieldName:
- - string => field value
- - array of objects => ref features as objects (wfsFeature)
- - array of strings => ref features as xlink url
- }
- */
- function p5WFS_ParseFeatureFieldRecurse(tagNode) { // @returns object from xml element
- var item = {}
- var i = 0
- for (i = 0; i < tagNode.children.length; i++) {
- var fieldNode = tagNode.children[i]
- if (!fieldNode.children.length) {
- if (fieldNode.textContent) item[ fieldNode.localName ] = fieldNode.textContent
- else {
- var xlink = fieldNode.getAttribute('xlink:href')
- if (xlink) {
- if (!(fieldNode.tagName in item)) item[ fieldNode.tagName ] = []
- item[ fieldNode.tagName ].push( xlink )
- }
- }
- }
- else {
- if (!(fieldNode.tagName in item)) item[ fieldNode.tagName ] = []
- item[ fieldNode.tagName ].push( p5WFS_ParseFeatureFieldRecurse(fieldNode) )
- }
- }
- return item
- }
- 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
- return p5WFS_ParseFeatureFieldRecurse(featureNode)
- }
- /**
- * @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']) {
- var headerOgcFilter = '<GetFeature xmlns="http://www.opengis.net/wfs/2.0"' +
- ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
- ' xmlns:ogc="http://www.opengis.net/ogc"' +
- ' xmlns:wfs="http://www.opengis.net/wfs"' +
- ' service="WFS"' +
- ' version="2.0.2"' + '>'
- if ('<GetFeature' === query['ogc:Filter'].substr(0, '<GetFeature'.length)) {
- postData = query['ogc:Filter']
- } else if ('<ogc:Filter>' === query['ogc:Filter'].substr(0, '<ogc:Filter>'.length)) {
- postData = headerOgcFilter + '<wfs:Query>' + query['ogc:Filter'] + '</wfs:Query>' + '</GetFeature>'
- } else if ('<wfs:Query>' === query['ogc:Filter'].substr(0, '<wfs:Query>'.length)) {
- postData = headerOgcFilter + query['ogc:Filter'] + '</GetFeature>'
- } else {
- throw 'Error - wrong syntax ogc:Filter'
- }
- }
- if ('sortBy' in query) link += '&sortBy=' + query['sortBy']
- if ('maxFeatures' in query) link += '&maxFeatures=' + query['maxFeatures']
- if ('count' in query) link += '&count=' + query['count']
- if ('primaryKey' in query) link += '&primaryKey=' + query['primaryKey']
- if ('resolve' in query) link += '&resolve=' + query['resolve']
- if ('resolveDepth' in query) link += '&resolveDepth=' + query['resolveDepth']
- if ('xlink' in query) link += '&xlink=' + query['xlink'] // NOTE: replace '#' to '/'
- if ('root' in query) link += '&root=' + query['root'] // NOTE: require admin perms (admin level 0)
- var method = (postData) ? 'POST' : 'GET'
- return window.fetch(link, Object.assign({
- method: method,
- credentials: 'include',
- }, ('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 ('html' === xmlRoot.localName) {
- console.log(xmlRoot) // html / body / parseerror / div
- throw "Parse xml error"
- }
- if ('FeatureCollection' !== xmlRoot.localName) {
- if ('ServiceExceptionReport' === xmlRoot.tagName) {
- if (xmlRoot.children[0] && xmlRoot.children[0].textContent) throw xmlRoot.children[0].textContent
- throw "WFS API Exception"
- }
- 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_ParseFeatureFieldRecurse = p5WFS_ParseFeatureFieldRecurse
- global.p5WFS_GetFeature = p5WFS_GetFeature
|