ogc-parser.js 6.8 KB

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