GetFeature.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // @require WFS_URL
  2. if (!WFS_URL) throw "Missing WFS_URL"
  3. // wfsFeature json format:
  4. /*
  5. {
  6. $fieldName:
  7. - string => field value
  8. - array of objects => ref features as objects (wfsFeature)
  9. - array of strings => ref features as xlink url
  10. }
  11. */
  12. function p5WFS_ParseFeatureFieldRecurse(tagNode) { // @returns object from xml element
  13. var item = {}
  14. var i = 0
  15. for (i = 0; i < tagNode.children.length; i++) {
  16. var fieldNode = tagNode.children[i]
  17. if (!fieldNode.children.length) {
  18. if (fieldNode.textContent) item[ fieldNode.localName ] = fieldNode.textContent
  19. else {
  20. var xlink = fieldNode.getAttribute('xlink:href')
  21. if (xlink) {
  22. if (!(fieldNode.tagName in item)) item[ fieldNode.tagName ] = []
  23. item[ fieldNode.tagName ].push( xlink )
  24. }
  25. }
  26. }
  27. else {
  28. if (!(fieldNode.tagName in item)) item[ fieldNode.tagName ] = []
  29. item[ fieldNode.tagName ].push( p5WFS_ParseFeatureFieldRecurse(fieldNode) )
  30. }
  31. }
  32. return item
  33. }
  34. function p5WFS_ParseFeatureMember(featureMember) { // @returns object from xml element <gml:featureMemeber>
  35. if (!featureMember) return null
  36. if (!featureMember.children.length) return null
  37. var featureNode = featureMember.children[0]
  38. if (!featureNode) return null
  39. if (!featureNode.children.length) return null
  40. return p5WFS_ParseFeatureFieldRecurse(featureNode)
  41. }
  42. /**
  43. * @usage
  44. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  45. 'featureID': 'CRM_PROCES.' + id,
  46. }).then(function (features) {
  47. // ...
  48. }).catch(function (e) {
  49. // ...
  50. })
  51. * @usage - expected one feature
  52. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  53. 'featureID': 'CRM_PROCES.' + id,
  54. }).then(function (features) {
  55. if (!features) throw "Nie odnaleziono rekordu id = " + id
  56. if (!features.length) throw "Nie odnaleziono rekordu id = " + id
  57. if (1 !== features.length) throw "Bład: API zwróciło za dużo rekordów"
  58. return features[0]
  59. console.log('fetched features', features)
  60. }).then(function (feature) {
  61. // ...
  62. }).catch(function (e) {
  63. // ...
  64. })
  65. * @usage - POST
  66. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  67. // 'Filter': '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>',
  68. 'sortBy': 'SORT_PRIO+A,ID',
  69. 'ogc:Filter': '<GetFeature xmlns="http://www.opengis.net/wfs/2.0"'+
  70. ' xmlns:p5_default_db="' + BASE_WFS_URL + '/default_db"' +
  71. ' xmlns:ogc="http://www.opengis.net/ogc"' +
  72. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
  73. ' service="WFS"' +
  74. ' version="2.0.2"' +
  75. ' xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd">' +
  76. '<ogc:Filter>' +
  77. '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>' +
  78. '</ogc:Filter>' +
  79. '</GetFeature>'
  80. }).then(function (features) {
  81. ...
  82. */
  83. function p5WFS_GetFeature(typeName, query) { // @returns Promise
  84. var postData = null
  85. var link = WFS_URL + '?SERVICE=WFS&VERSION=1.0.0' +
  86. '&REQUEST=GetFeature' +
  87. '&TYPENAME=' + typeName +
  88. '&SRSNAME=EPSG:3003'
  89. // '&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>ID</ogc:PropertyName><ogc:Literal>' + id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>'
  90. var query = query || {}
  91. if ('featureID' in query) link += '&featureID=' + query['featureID']
  92. if ('Filter' in query && 'string' === typeof query['Filter']) {
  93. var ogcFilter = ('<ogc:Filter>' !== query['Filter'].substr(0, '<ogc:Filter>'.length))
  94. ? '<ogc:Filter>' + query['Filter'] + '</ogc:Filter>'
  95. : query['Filter']
  96. ;
  97. link += '&Filter=' + ogcFilter
  98. }
  99. if ('ogc:Filter' in query && 'string' === typeof query['ogc:Filter']) {
  100. var headerOgcFilter = '<GetFeature xmlns="http://www.opengis.net/wfs/2.0"' +
  101. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
  102. ' xmlns:ogc="http://www.opengis.net/ogc"' +
  103. ' xmlns:wfs="http://www.opengis.net/wfs"' +
  104. ' service="WFS"' +
  105. ' version="2.0.2"' + '>'
  106. if ('<GetFeature' === query['ogc:Filter'].substr(0, '<GetFeature'.length)) {
  107. postData = query['ogc:Filter']
  108. } else if ('<ogc:Filter>' === query['ogc:Filter'].substr(0, '<ogc:Filter>'.length)) {
  109. postData = headerOgcFilter + '<wfs:Query>' + query['ogc:Filter'] + '</wfs:Query>' + '</GetFeature>'
  110. } else if ('<wfs:Query>' === query['ogc:Filter'].substr(0, '<wfs:Query>'.length)) {
  111. postData = headerOgcFilter + query['ogc:Filter'] + '</GetFeature>'
  112. } else {
  113. throw 'Error - wrong syntax ogc:Filter'
  114. }
  115. }
  116. if ('sortBy' in query) link += '&sortBy=' + query['sortBy']
  117. if ('maxFeatures' in query) link += '&maxFeatures=' + query['maxFeatures']
  118. if ('count' in query) link += '&count=' + query['count']
  119. if ('primaryKey' in query) link += '&primaryKey=' + query['primaryKey']
  120. if ('resolve' in query) link += '&resolve=' + query['resolve']
  121. if ('resolveDepth' in query) link += '&resolveDepth=' + query['resolveDepth']
  122. if ('xlink' in query) link += '&xlink=' + query['xlink'] // NOTE: replace '#' to '/'
  123. if ('root' in query) link += '&root=' + query['root'] // NOTE: require admin perms (admin level 0)
  124. var method = (postData) ? 'POST' : 'GET'
  125. return window.fetch(link, Object.assign({
  126. method: method,
  127. credentials: 'include',
  128. }, ('POST' === method)
  129. ? {
  130. body: postData
  131. }
  132. : {}
  133. )).then(function (response) {
  134. return response.text()
  135. }).then(function (responseText) {
  136. return (new DOMParser()).parseFromString(responseText, "text/xml");
  137. }).then(function (xmlResponse) {
  138. if (!xmlResponse.children.length) throw "Missing response xml root element"
  139. return xmlResponse.children[0]
  140. }).then(function (xmlRoot) {
  141. if ('html' === xmlRoot.localName) {
  142. console.log(xmlRoot) // html / body / parseerror / div
  143. throw "Parse xml error"
  144. }
  145. if ('FeatureCollection' !== xmlRoot.localName) {
  146. if ('ServiceExceptionReport' === xmlRoot.tagName) {
  147. if (xmlRoot.children[0] && xmlRoot.children[0].textContent) throw xmlRoot.children[0].textContent
  148. throw "WFS API Exception"
  149. }
  150. throw "Missing FeatureCollection as root element in wfs response"
  151. }
  152. var features = []
  153. for (var i = 0; i < xmlRoot.children.length; i++) {
  154. features.push(p5WFS_ParseFeatureMember(xmlRoot.children[i]))
  155. }
  156. return features.filter(function (item) { return !!item; })
  157. })
  158. }
  159. global.p5WFS_ParseFeatureMember = p5WFS_ParseFeatureMember
  160. global.p5WFS_ParseFeatureFieldRecurse = p5WFS_ParseFeatureFieldRecurse
  161. global.p5WFS_GetFeature = p5WFS_GetFeature