udt.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. 'use strict'
  2. /* const FIGURE = {
  3. INTERIOR_RING: 0x00,
  4. STROKE: 0x01,
  5. EXTERIOR_RING: 0x02
  6. };
  7. const FIGURE_V2 = {
  8. POINT: 0x00,
  9. LINE: 0x01,
  10. ARC: 0x02,
  11. COMPOSITE_CURVE: 0x03
  12. };
  13. const SHAPE = {
  14. POINT: 0x01,
  15. LINESTRING: 0x02,
  16. POLYGON: 0x03,
  17. MULTIPOINT: 0x04,
  18. MULTILINESTRING: 0x05,
  19. MULTIPOLYGON: 0x06,
  20. GEOMETRY_COLLECTION: 0x07
  21. };
  22. const SHAPE_V2 = {
  23. POINT: 0x01,
  24. LINESTRING: 0x02,
  25. POLYGON: 0x03,
  26. MULTIPOINT: 0x04,
  27. MULTILINESTRING: 0x05,
  28. MULTIPOLYGON: 0x06,
  29. GEOMETRY_COLLECTION: 0x07,
  30. CIRCULAR_STRING: 0x08,
  31. COMPOUND_CURVE: 0x09,
  32. CURVE_POLYGON: 0x0A,
  33. FULL_GLOBE: 0x0B
  34. };
  35. const SEGMENT = {
  36. LINE: 0x00,
  37. ARC: 0x01,
  38. FIRST_LINE: 0x02,
  39. FIRST_ARC: 0x03
  40. }; */
  41. class Point {
  42. constructor () {
  43. this.x = 0
  44. this.y = 0
  45. this.z = null
  46. this.m = null
  47. }
  48. }
  49. const parsePoints = (buffer, count) => {
  50. // s2.1.5 + s2.1.6
  51. const points = []
  52. if (count < 1) {
  53. return points
  54. }
  55. for (let i = 1; i <= count; i++) {
  56. const point = new Point()
  57. points.push(point)
  58. point.x = buffer.readDoubleLE(buffer.position)
  59. point.y = buffer.readDoubleLE(buffer.position + 8)
  60. buffer.position += 16
  61. }
  62. return points
  63. }
  64. const parseZ = (buffer, points) => {
  65. // s2.1.1 + s.2.1.2
  66. if (points < 1) {
  67. return
  68. }
  69. points.forEach(point => {
  70. point.z = buffer.readDoubleLE(buffer.position)
  71. buffer.position += 8
  72. })
  73. }
  74. const parseM = (buffer, points) => {
  75. // s2.1.1 + s.2.1.2
  76. if (points < 1) {
  77. return
  78. }
  79. points.forEach(point => {
  80. point.m = buffer.readDoubleLE(buffer.position)
  81. buffer.position += 8
  82. })
  83. }
  84. const parseFigures = (buffer, count, properties) => {
  85. // s2.1.3
  86. const figures = []
  87. if (count < 1) {
  88. return figures
  89. }
  90. if (properties.P) {
  91. figures.push({
  92. attribute: 0x01,
  93. pointOffset: 0
  94. })
  95. } else if (properties.L) {
  96. figures.push({
  97. attribute: 0x01,
  98. pointOffset: 0
  99. })
  100. } else {
  101. for (let i = 1; i <= count; i++) {
  102. figures.push({
  103. attribute: buffer.readUInt8(buffer.position),
  104. pointOffset: buffer.readInt32LE(buffer.position + 1)
  105. })
  106. buffer.position += 5
  107. }
  108. }
  109. return figures
  110. }
  111. const parseShapes = (buffer, count, properties) => {
  112. // s2.1.4
  113. const shapes = []
  114. if (count < 1) {
  115. return shapes
  116. }
  117. if (properties.P) {
  118. shapes.push({
  119. parentOffset: -1,
  120. figureOffset: 0,
  121. type: 0x01
  122. })
  123. } else if (properties.L) {
  124. shapes.push({
  125. parentOffset: -1,
  126. figureOffset: 0,
  127. type: 0x02
  128. })
  129. } else {
  130. for (let i = 1; i <= count; i++) {
  131. shapes.push({
  132. parentOffset: buffer.readInt32LE(buffer.position),
  133. figureOffset: buffer.readInt32LE(buffer.position + 4),
  134. type: buffer.readUInt8(buffer.position + 8)
  135. })
  136. buffer.position += 9
  137. }
  138. }
  139. return shapes
  140. }
  141. const parseSegments = (buffer, count) => {
  142. // s2.1.7
  143. const segments = []
  144. if (count < 1) {
  145. return segments
  146. }
  147. for (let i = 1; i <= count; i++) {
  148. segments.push({ type: buffer.readUInt8(buffer.position) })
  149. buffer.position++
  150. }
  151. return segments
  152. }
  153. const parseGeography = buffer => {
  154. // s2.1.1 + s.2.1.2
  155. const srid = buffer.readInt32LE(0)
  156. if (srid === -1) {
  157. return null
  158. }
  159. const value = {
  160. srid,
  161. version: buffer.readUInt8(4)
  162. }
  163. const flags = buffer.readUInt8(5)
  164. buffer.position = 6
  165. // console.log("srid", srid)
  166. // console.log("version", version)
  167. const properties = {
  168. Z: (flags & (1 << 0)) > 0,
  169. M: (flags & (1 << 1)) > 0,
  170. V: (flags & (1 << 2)) > 0,
  171. P: (flags & (1 << 3)) > 0,
  172. L: (flags & (1 << 4)) > 0
  173. }
  174. if (value.version === 2) {
  175. properties.H = (flags & (1 << 3)) > 0
  176. }
  177. // console.log("properties", properties);
  178. let numberOfPoints
  179. if (properties.P) {
  180. numberOfPoints = 1
  181. } else if (properties.L) {
  182. numberOfPoints = 2
  183. } else {
  184. numberOfPoints = buffer.readUInt32LE(buffer.position)
  185. buffer.position += 4
  186. }
  187. // console.log("numberOfPoints", numberOfPoints)
  188. value.points = parsePoints(buffer, numberOfPoints)
  189. if (properties.Z) {
  190. parseZ(buffer, value.points)
  191. }
  192. if (properties.M) {
  193. parseM(buffer, value.points)
  194. }
  195. // console.log("points", points)
  196. let numberOfFigures
  197. if (properties.P) {
  198. numberOfFigures = 1
  199. } else if (properties.L) {
  200. numberOfFigures = 1
  201. } else {
  202. numberOfFigures = buffer.readUInt32LE(buffer.position)
  203. buffer.position += 4
  204. }
  205. // console.log("numberOfFigures", numberOfFigures)
  206. value.figures = parseFigures(buffer, numberOfFigures, properties)
  207. // console.log("figures", figures)
  208. let numberOfShapes
  209. if (properties.P) {
  210. numberOfShapes = 1
  211. } else if (properties.L) {
  212. numberOfShapes = 1
  213. } else {
  214. numberOfShapes = buffer.readUInt32LE(buffer.position)
  215. buffer.position += 4
  216. }
  217. // console.log("numberOfShapes", numberOfShapes)
  218. value.shapes = parseShapes(buffer, numberOfShapes, properties)
  219. // console.log( "shapes", shapes)
  220. if (value.version === 2) {
  221. const numberOfSegments = buffer.readUInt32LE(buffer.position)
  222. buffer.position += 4
  223. // console.log("numberOfSegments", numberOfSegments)
  224. value.segments = parseSegments(buffer, numberOfSegments)
  225. // console.log("segments", segments)
  226. } else {
  227. value.segments = []
  228. }
  229. return value
  230. }
  231. module.exports.PARSERS = {
  232. geography (buffer) {
  233. return parseGeography(buffer)
  234. },
  235. geometry (buffer) {
  236. return parseGeography(buffer)
  237. }
  238. }