ogc-parser.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 = () => {
  87. const v = dataView.getFloat64( cursor, littleEndian ); cursor += 8; return v;
  88. };
  89. // const getFloat = () => { const v = dataView.getFloat32(cursor, littleEndian); cursor += 4 ; return v; };
  90. const getInt8 = () => {
  91. const v = dataView.getInt8( cursor ); cursor += 1; return v;
  92. };
  93. // const getUint8 = () => { const v = dataView.getUint8(cursor, littleEndian); cursor += 1 ; return v; };
  94. // const getInt16 = () => { const v = dataView.getInt16(cursor, littleEndian); cursor += 2 ; return v; };
  95. // const getUint16 = () => { const v = dataView.getUint16(cursor, littleEndian); cursor += 2 ; return v; };
  96. // const getInt32 = () => { const v = dataView.getInt32(cursor, littleEndian); cursor += 4 ; return v; };
  97. const getUint32 = () => {
  98. const v = dataView.getUint32( cursor, littleEndian ); cursor += 4; return v;
  99. };
  100. pushByteOrder( flag_byteOrder );
  101. const envelope = [];
  102. for ( let i = 0; i < envelopeSize; ++ i ) {
  103. envelope.push( getDouble() );
  104. }
  105. const primitives = [];
  106. function getPoints( num ) {
  107. const points = [];
  108. for ( let i = 0; i < num; ++ i ) {
  109. points.push( getDouble(), getDouble() );
  110. }
  111. return points;
  112. }
  113. function getRings( num ) {
  114. const rings = [];
  115. for ( let i = 0; i < num; ++ i ) {
  116. rings.push( getPoints( getUint32() ) );
  117. }
  118. return rings;
  119. }
  120. function pointHandler() {
  121. return {
  122. type: 'point',
  123. point: getPoints( 1 ),
  124. };
  125. }
  126. function lineStringHandler() {
  127. return {
  128. type: 'lineString',
  129. points: getPoints( getUint32() ),
  130. };
  131. }
  132. function polygonHandler() {
  133. return {
  134. type: 'polygon',
  135. rings: getRings( getUint32() ),
  136. };
  137. }
  138. function multiPointHandler() {
  139. // WTF?
  140. const points = [];
  141. const num = getUint32();
  142. for ( let i = 0; i < num; ++ i ) {
  143. pushByteOrder( getInt8() );
  144. const type = getUint32();
  145. assert.strictEqual( type, 1 ); // must be point
  146. points.push( getDouble(), getDouble() );
  147. popByteOrder();
  148. }
  149. return {
  150. type: 'multiPoint',
  151. points,
  152. };
  153. }
  154. function multiLineStringHandler() {
  155. // WTF?
  156. const lineStrings = [];
  157. const num = getUint32();
  158. for ( let i = 0; i < num; ++ i ) {
  159. pushByteOrder( getInt8() );
  160. const type = getUint32();
  161. assert.strictEqual( type, 2 ); // must be lineString
  162. lineStrings.push( getPoints( getUint32() ) );
  163. popByteOrder();
  164. }
  165. return {
  166. type: 'multiLineString',
  167. lineStrings,
  168. };
  169. }
  170. function multiPolygonHandler() {
  171. // WTF?
  172. const polygons = [];
  173. const num = getUint32();
  174. for ( let i = 0; i < num; ++ i ) {
  175. pushByteOrder( getInt8() );
  176. const type = getUint32();
  177. assert.strictEqual( type, 3 ); // must be polygon
  178. polygons.push( getRings( getUint32() ) );
  179. popByteOrder();
  180. }
  181. return {
  182. type: 'multiPolygon',
  183. polygons,
  184. };
  185. }
  186. const typeHandlers = [
  187. undefined, // 0
  188. pointHandler, // 1
  189. lineStringHandler, // 2
  190. polygonHandler, // 3
  191. multiPointHandler, // 4
  192. multiLineStringHandler, // 5,
  193. multiPolygonHandler, // 6,
  194. ];
  195. const end = buf.length;
  196. while ( cursor < end ) {
  197. pushByteOrder( getInt8() );
  198. const type = getUint32();
  199. const handler = typeHandlers[ type ];
  200. assert.notStrictEqual( handler, undefined, 'unknown type' );
  201. primitives.push( handler() );
  202. popByteOrder();
  203. }
  204. return {
  205. envelope,
  206. primitives,
  207. };
  208. }
  209. window.ogcParser = { parse };