GetFeature.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // @require WFS_URL
  2. if (!WFS_URL) throw "Missing WFS_URL"
  3. function p5WFS_ParseFeatureMember(featureMember) { // @returns object from xml element <gml:featureMemeber>
  4. if (!featureMember) return null
  5. if (!featureMember.children.length) return null
  6. var featureNode = featureMember.children[0]
  7. if (!featureNode) return null
  8. if (!featureNode.children.length) return null
  9. var item = {}
  10. var i = 0
  11. for (i = 0; i < featureNode.children.length; i++) {
  12. var field = featureNode.children[i]
  13. item[ field.localName ] = field.textContent
  14. }
  15. return item
  16. }
  17. /**
  18. * @usage
  19. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  20. 'featureID': 'CRM_PROCES.' + id,
  21. }).then(function (features) {
  22. // ...
  23. }).catch(function (e) {
  24. // ...
  25. })
  26. * @usage - expected one feature
  27. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  28. 'featureID': 'CRM_PROCES.' + id,
  29. }).then(function (features) {
  30. if (!features) throw "Nie odnaleziono rekordu id = " + id
  31. if (!features.length) throw "Nie odnaleziono rekordu id = " + id
  32. if (1 !== features.length) throw "Bład: API zwróciło za dużo rekordów"
  33. return features[0]
  34. console.log('fetched features', features)
  35. }).then(function (feature) {
  36. // ...
  37. }).catch(function (e) {
  38. // ...
  39. })
  40. * @usage - POST
  41. p5WFS_GetFeature('p5_default_db:CRM_PROCES', {
  42. // 'Filter': '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>',
  43. 'sortBy': 'SORT_PRIO+A,ID',
  44. 'ogc:Filter': '<GetFeature xmlns="http://www.opengis.net/wfs/2.0"'+
  45. ' xmlns:p5_default_db="' + BASE_WFS_URL + '/default_db"' +
  46. ' xmlns:ogc="http://www.opengis.net/ogc"' +
  47. ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' +
  48. ' service="WFS"' +
  49. ' version="2.0.2"' +
  50. ' xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd">' +
  51. '<ogc:Filter>' +
  52. '<ogc:PropertyIsEqualTo><ogc:PropertyName>' + "PARENT_ID" + '</ogc:PropertyName><ogc:Literal>' + parent_id + '</ogc:Literal></ogc:PropertyIsEqualTo>' +
  53. '</ogc:Filter>' +
  54. '</GetFeature>'
  55. }).then(function (features) {
  56. ...
  57. */
  58. function p5WFS_GetFeature(typeName, query) { // @returns Promise
  59. var postData = null
  60. var link = WFS_URL + '?SERVICE=WFS&VERSION=1.0.0' +
  61. '&REQUEST=GetFeature' +
  62. '&TYPENAME=' + typeName +
  63. '&SRSNAME=EPSG:3003'
  64. // '&Filter=<ogc:Filter><ogc:Or><ogc:PropertyIsEqualTo><ogc:PropertyName>ID</ogc:PropertyName><ogc:Literal>' + id + '</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Or></ogc:Filter>'
  65. var query = query || {}
  66. if ('featureID' in query) link += '&featureID=' + query['featureID']
  67. if ('Filter' in query && 'string' === typeof query['Filter']) {
  68. var ogcFilter = ('<ogc:Filter>' !== query['Filter'].substr(0, '<ogc:Filter>'.length))
  69. ? '<ogc:Filter>' + query['Filter'] + '</ogc:Filter>'
  70. : query['Filter']
  71. ;
  72. link += '&Filter=' + ogcFilter
  73. }
  74. if ('ogc:Filter' in query && 'string' === typeof query['ogc:Filter']) {
  75. postData = query['ogc:Filter']
  76. }
  77. if ('sortBy' in query) link += '&sortBy=' + query['sortBy']
  78. if ('maxFeatures' in query) link += '&maxFeatures=' + query['maxFeatures']
  79. if ('count' in query) link += '&count=' + query['count']
  80. var method = (postData) ? 'POST' : 'GET'
  81. return window.fetch(link, Object.assign({
  82. method: method,
  83. credentials: 'same-origin',
  84. }, ('POST' === method)
  85. ? {
  86. body: postData
  87. }
  88. : {}
  89. )).then(function (response) {
  90. return response.text()
  91. }).then(function (responseText) {
  92. return (new DOMParser()).parseFromString(responseText, "text/xml");
  93. }).then(function (xmlResponse) {
  94. if (!xmlResponse.children.length) throw "Missing response xml root element"
  95. return xmlResponse.children[0]
  96. }).then(function (xmlRoot) {
  97. if ('FeatureCollection' !== xmlRoot.localName) throw "Missing FeatureCollection as root element in wfs response"
  98. var features = []
  99. for (var i = 0; i < xmlRoot.children.length; i++) {
  100. features.push(p5WFS_ParseFeatureMember(xmlRoot.children[i]))
  101. }
  102. return features.filter(function (item) { return !!item; })
  103. })
  104. }
  105. global.p5WFS_ParseFeatureMember = p5WFS_ParseFeatureMember
  106. global.p5WFS_GetFeature = p5WFS_GetFeature