ogc-parser.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. 'use strict';
  2. const assert = {
  3. strictEqual(actual, expected, ...args) {
  4. args = args || [];
  5. if (actual !== expected) {
  6. throw new Error(`${actual} (actual) should equal ${expected} (expected): ${[...args].join(' ')}`);
  7. }
  8. },
  9. notStrictEqual(actual, expected, ...args) {
  10. args = args || [];
  11. if (actual === expected) {
  12. throw new Error(`${actual} (actual) should NOT equal ${expected} (expected): ${[...args].join(' ')}`);
  13. }
  14. },
  15. };
  16. /*
  17. function dumpBuf(buf) {
  18. for (let i = 0; i < buf.length; i += 32) {
  19. const p = [];
  20. const a = [];
  21. for (let j = i; j < i + 32 && j < buf.length; ++j) {
  22. const b = buf[j];
  23. p.push(b.toString(16).padStart(2, '0'));
  24. a.push(b >= 32 && b < 128 ? String.fromCharCode(b) : '.');
  25. if (j % 4 === 3) {
  26. p.push(' ');
  27. }
  28. }
  29. console.log(i.toString(16).padStart(8, '0'), ':', p.join(''), a.join(''));
  30. }
  31. }
  32. */
  33. function parse(buf) {
  34. assert.strictEqual(buf[0], 0x47, 'bad header');
  35. assert.strictEqual(buf[1], 0x50, 'bad header');
  36. assert.strictEqual(buf[2], 0, 'unknown version'); // version
  37. const flags = buf[3];
  38. const flag_x = (flags >> 5) & 1;
  39. // const flag_empty_geo = (flags >> 4) & 1; // 1 = empty, 0 non-empty
  40. const flag_byteOrder = (flags >> 0) & 1; // 1 = little endian, 0 = big
  41. const flag_envelope = (flags >> 1) & 7;
  42. assert.strictEqual(flag_x, 0, 'x must be 0');
  43. const envelopeSizes = [
  44. 0, // 0: non
  45. 4, // 1: minx, maxx, miny, maxy
  46. 6, // 2: minx, maxx, miny, maxy, minz, maxz
  47. 6, // 3: minx, maxx, miny, maxy, minm, maxm
  48. 8, // 4: minx, maxx, miny, maxy, minz, maxz, minm, maxm
  49. ];
  50. const envelopeSize = envelopeSizes[flag_envelope];
  51. assert.notStrictEqual(envelopeSize, undefined);
  52. const headerSize = 8;
  53. let cursor = headerSize;
  54. const dataView = new DataView(buf.buffer);
  55. /*
  56. const readBE = {
  57. getDouble() { const v = buf.readDoubleBE(cursor); cursor += 8 ; return v; },
  58. getFloat() { const v = buf.readFloatBE(cursor); cursor += 4 ; return v; },
  59. getInt8() { const v = buf.readInt8(cursor); cursor += 1 ; return v; },
  60. getUint8() { const v = buf.readUInt8(cursor); cursor += 1 ; return v; },
  61. getInt16() { const v = buf.readInt16BE(cursor); cursor += 2 ; return v; },
  62. getUint16() { const v = buf.readUInt16BE(cursor); cursor += 2 ; return v; },
  63. getInt32() { const v = buf.readInt32BE(cursor); cursor += 4 ; return v; },
  64. getUint32() { const v = buf.readUInt32BE(cursor); cursor += 4 ; return v; },
  65. };
  66. const readLE = {
  67. getDouble() { const v = buf.readDoubleLE(cursor); cursor += 8 ; return v; },
  68. getFloat() { const v = buf.readFloatLE(cursor); cursor += 4 ; return v; },
  69. getInt8() { const v = buf.readInt8(cursor); cursor += 1 ; return v; },
  70. getUint8() { const v = buf.readUInt8(cursor); cursor += 1 ; return v; },
  71. getInt16() { const v = buf.readInt16LE(cursor); cursor += 2 ; return v; },
  72. getUint16() { const v = buf.readUInt16LE(cursor); cursor += 2 ; return v; },
  73. getInt32() { const v = buf.readInt32LE(cursor); cursor += 4 ; return v; },
  74. getUint32() { const v = buf.readUInt32LE(cursor); cursor += 4 ; return v; },
  75. };
  76. */
  77. let littleEndian;
  78. const endianStack = [];
  79. function pushByteOrder(byteOrder) {
  80. endianStack.push(littleEndian);
  81. littleEndian = byteOrder;
  82. }
  83. function popByteOrder() {
  84. littleEndian = endianStack.pop();
  85. }
  86. const getDouble = () => { const v = dataView.getFloat64(cursor, littleEndian); cursor += 8 ; return v; };
  87. // const getFloat = () => { const v = dataView.getFloat32(cursor, littleEndian); cursor += 4 ; return v; };
  88. const getInt8 = () => { const v = dataView.getInt8(cursor); cursor += 1 ; return v; };
  89. // const getUint8 = () => { const v = dataView.getUint8(cursor, littleEndian); cursor += 1 ; return v; };
  90. // const getInt16 = () => { const v = dataView.getInt16(cursor, littleEndian); cursor += 2 ; return v; };
  91. // const getUint16 = () => { const v = dataView.getUint16(cursor, littleEndian); cursor += 2 ; return v; };
  92. // const getInt32 = () => { const v = dataView.getInt32(cursor, littleEndian); cursor += 4 ; return v; };
  93. const getUint32 = () => { const v = dataView.getUint32(cursor, littleEndian); cursor += 4 ; return v; };
  94. pushByteOrder(flag_byteOrder);
  95. const envelope = [];
  96. for (let i = 0; i < envelopeSize; ++i) {
  97. envelope.push(getDouble());
  98. }
  99. const primitives = [];
  100. function getPoints(num) {
  101. const points = [];
  102. for (let i = 0; i < num; ++i) {
  103. points.push(getDouble(), getDouble());
  104. }
  105. return points;
  106. }
  107. function getRings(num) {
  108. const rings = [];
  109. for (let i = 0; i < num; ++i) {
  110. rings.push(getPoints(getUint32()));
  111. }
  112. return rings;
  113. }
  114. function pointHandler() {
  115. return {
  116. type: 'point',
  117. point: getPoints(1),
  118. };
  119. }
  120. function lineStringHandler() {
  121. return {
  122. type: 'lineString',
  123. points: getPoints(getUint32()),
  124. };
  125. }
  126. function polygonHandler() {
  127. return {
  128. type: 'polygon',
  129. rings: getRings(getUint32()),
  130. };
  131. }
  132. function multiPointHandler() {
  133. // WTF?
  134. const points = [];
  135. const num = getUint32();
  136. for (let i = 0; i < num; ++i) {
  137. pushByteOrder(getInt8());
  138. const type = getUint32();
  139. assert.strictEqual(type, 1); // must be point
  140. points.push(getDouble(), getDouble());
  141. popByteOrder();
  142. }
  143. return {
  144. type: 'multiPoint',
  145. points,
  146. };
  147. }
  148. function multiLineStringHandler() {
  149. // WTF?
  150. const lineStrings = [];
  151. const num = getUint32();
  152. for (let i = 0; i < num; ++i) {
  153. pushByteOrder(getInt8());
  154. const type = getUint32();
  155. assert.strictEqual(type, 2); // must be lineString
  156. lineStrings.push(getPoints(getUint32()));
  157. popByteOrder();
  158. }
  159. return {
  160. type: 'multiLineString',
  161. lineStrings,
  162. };
  163. }
  164. function multiPolygonHandler() {
  165. // WTF?
  166. const polygons = [];
  167. const num = getUint32();
  168. for (let i = 0; i < num; ++i) {
  169. pushByteOrder(getInt8());
  170. const type = getUint32();
  171. assert.strictEqual(type, 3); // must be polygon
  172. polygons.push(getRings(getUint32()));
  173. popByteOrder();
  174. }
  175. return {
  176. type: 'multiPolygon',
  177. polygons,
  178. };
  179. }
  180. const typeHandlers = [
  181. undefined, // 0
  182. pointHandler, // 1
  183. lineStringHandler, // 2
  184. polygonHandler, // 3
  185. multiPointHandler, // 4
  186. multiLineStringHandler, // 5,
  187. multiPolygonHandler, // 6,
  188. ];
  189. const end = buf.length;
  190. while (cursor < end) {
  191. pushByteOrder(getInt8());
  192. const type = getUint32();
  193. const handler = typeHandlers[type];
  194. assert.notStrictEqual(handler, undefined, 'unknown type');
  195. primitives.push(handler());
  196. popByteOrder();
  197. }
  198. return {
  199. envelope,
  200. primitives,
  201. };
  202. }
  203. window.ogcParser = {parse};