WKTParser.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. import Coordinate from '../geom/Coordinate'
  2. import GeometryFactory from '../geom/GeometryFactory'
  3. /**
  4. * The coordinate layout for geometries, indicating whether a 3rd or 4th z ('Z')
  5. * or measure ('M') coordinate is available. Supported values are `'XY'`,
  6. * `'XYZ'`, `'XYM'`, `'XYZM'`.
  7. * @enum {string}
  8. */
  9. const GeometryLayout = {
  10. XY: 'XY',
  11. XYZ: 'XYZ',
  12. XYM: 'XYM',
  13. XYZM: 'XYZM',
  14. }
  15. /**
  16. * The geometry type. One of `'Point'`, `'LineString'`, `'LinearRing'`,
  17. * `'Polygon'`, `'MultiPoint'`, `'MultiLineString'`, `'MultiPolygon'`,
  18. * `'GeometryCollection'`, `'Circle'`.
  19. * @enum {string}
  20. */
  21. const GeometryType = {
  22. POINT: 'Point',
  23. LINE_STRING: 'LineString',
  24. LINEAR_RING: 'LinearRing',
  25. POLYGON: 'Polygon',
  26. MULTI_POINT: 'MultiPoint',
  27. MULTI_LINE_STRING: 'MultiLineString',
  28. MULTI_POLYGON: 'MultiPolygon',
  29. GEOMETRY_COLLECTION: 'GeometryCollection',
  30. CIRCLE: 'Circle',
  31. }
  32. /**
  33. * @typedef {Object} Options
  34. * @property {boolean} [splitCollection=false] Whether to split GeometryCollections into
  35. * multiple features on reading.
  36. */
  37. /**
  38. * @typedef {Object} Token
  39. * @property {number} type
  40. * @property {number|string} [value]
  41. * @property {number} position
  42. */
  43. /**
  44. * @const
  45. * @type {string}
  46. */
  47. const EMPTY = 'EMPTY'
  48. /**
  49. * @const
  50. * @type {string}
  51. */
  52. const Z = 'Z'
  53. /**
  54. * @const
  55. * @type {string}
  56. */
  57. const M = 'M'
  58. /**
  59. * @const
  60. * @type {string}
  61. */
  62. const ZM = 'ZM'
  63. /**
  64. * @const
  65. * @enum {number}
  66. */
  67. const TokenType = {
  68. TEXT: 1,
  69. LEFT_PAREN: 2,
  70. RIGHT_PAREN: 3,
  71. NUMBER: 4,
  72. COMMA: 5,
  73. EOF: 6,
  74. }
  75. /**
  76. * @const
  77. * @type {Object<string, string>}
  78. */
  79. const WKTGeometryType = {}
  80. for (const type in GeometryType)
  81. WKTGeometryType[type] = GeometryType[type].toUpperCase()
  82. /**
  83. * Class to tokenize a WKT string.
  84. */
  85. class Lexer {
  86. /**
  87. * @param {string} wkt WKT string.
  88. */
  89. constructor(wkt) {
  90. /**
  91. * @type {string}
  92. */
  93. this.wkt = wkt
  94. /**
  95. * @type {number}
  96. * @private
  97. */
  98. this.index_ = -1
  99. }
  100. /**
  101. * @param {string} c Character.
  102. * @return {boolean} Whether the character is alphabetic.
  103. * @private
  104. */
  105. isAlpha_(c) {
  106. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
  107. }
  108. /**
  109. * @param {string} c Character.
  110. * @param {boolean=} opt_decimal Whether the string number
  111. * contains a dot, i.e. is a decimal number.
  112. * @return {boolean} Whether the character is numeric.
  113. * @private
  114. */
  115. isNumeric_(c, opt_decimal) {
  116. const decimal = opt_decimal !== undefined ? opt_decimal : false
  117. return (c >= '0' && c <= '9') || (c == '.' && !decimal)
  118. }
  119. /**
  120. * @param {string} c Character.
  121. * @return {boolean} Whether the character is whitespace.
  122. * @private
  123. */
  124. isWhiteSpace_(c) {
  125. return c == ' ' || c == '\t' || c == '\r' || c == '\n'
  126. }
  127. /**
  128. * @return {string} Next string character.
  129. * @private
  130. */
  131. nextChar_() {
  132. return this.wkt.charAt(++this.index_)
  133. }
  134. /**
  135. * Fetch and return the next token.
  136. * @return {!Token} Next string token.
  137. */
  138. nextToken() {
  139. const c = this.nextChar_()
  140. const position = this.index_
  141. /** @type {number|string} */
  142. let value = c
  143. let type
  144. if (c == '(') {
  145. type = TokenType.LEFT_PAREN
  146. } else if (c == ',') {
  147. type = TokenType.COMMA
  148. } else if (c == ')') {
  149. type = TokenType.RIGHT_PAREN
  150. } else if (this.isNumeric_(c) || c == '-') {
  151. type = TokenType.NUMBER
  152. value = this.readNumber_()
  153. } else if (this.isAlpha_(c)) {
  154. type = TokenType.TEXT
  155. value = this.readText_()
  156. } else if (this.isWhiteSpace_(c)) {
  157. return this.nextToken()
  158. } else if (c === '') {
  159. type = TokenType.EOF
  160. } else {
  161. throw new Error('Unexpected character: ' + c)
  162. }
  163. return { position: position, value: value, type: type }
  164. }
  165. /**
  166. * @return {number} Numeric token value.
  167. * @private
  168. */
  169. readNumber_() {
  170. let c
  171. const index = this.index_
  172. let decimal = false
  173. let scientificNotation = false
  174. do {
  175. if (c == '.')
  176. decimal = true
  177. else if (c == 'e' || c == 'E')
  178. scientificNotation = true
  179. c = this.nextChar_()
  180. } while (
  181. this.isNumeric_(c, decimal) ||
  182. // if we haven't detected a scientific number before, 'e' or 'E'
  183. // hint that we should continue to read
  184. (!scientificNotation && (c == 'e' || c == 'E')) ||
  185. // once we know that we have a scientific number, both '-' and '+'
  186. // are allowed
  187. (scientificNotation && (c == '-' || c == '+'))
  188. )
  189. return parseFloat(this.wkt.substring(index, this.index_--))
  190. }
  191. /**
  192. * @return {string} String token value.
  193. * @private
  194. */
  195. readText_() {
  196. let c
  197. const index = this.index_
  198. do
  199. c = this.nextChar_()
  200. while (this.isAlpha_(c))
  201. return this.wkt.substring(index, this.index_--).toUpperCase()
  202. }
  203. }
  204. /**
  205. * Class to parse the tokens from the WKT string.
  206. */
  207. class Parser {
  208. /**
  209. * @param {Lexer} lexer The lexer.
  210. */
  211. constructor(lexer, factory) {
  212. /**
  213. * @type {Lexer}
  214. * @private
  215. */
  216. this.lexer_ = lexer
  217. /**
  218. * @type {Token}
  219. * @private
  220. */
  221. this.token_
  222. /**
  223. * @type {import("../geom/GeometryLayout.js").default}
  224. * @private
  225. */
  226. this.layout_ = GeometryLayout.XY
  227. this.factory = factory
  228. }
  229. /**
  230. * Fetch the next token form the lexer and replace the active token.
  231. * @private
  232. */
  233. consume_() {
  234. this.token_ = this.lexer_.nextToken()
  235. }
  236. /**
  237. * Tests if the given type matches the type of the current token.
  238. * @param {TokenType} type Token type.
  239. * @return {boolean} Whether the token matches the given type.
  240. */
  241. isTokenType(type) {
  242. const isMatch = this.token_.type == type
  243. return isMatch
  244. }
  245. /**
  246. * If the given type matches the current token, consume it.
  247. * @param {TokenType} type Token type.
  248. * @return {boolean} Whether the token matches the given type.
  249. */
  250. match(type) {
  251. const isMatch = this.isTokenType(type)
  252. if (isMatch)
  253. this.consume_()
  254. return isMatch
  255. }
  256. /**
  257. * Try to parse the tokens provided by the lexer.
  258. * @return {import("../geom/Geometry.js").default} The geometry.
  259. */
  260. parse() {
  261. this.consume_()
  262. const geometry = this.parseGeometry_()
  263. return geometry
  264. }
  265. /**
  266. * Try to parse the dimensional info.
  267. * @return {import("../geom/GeometryLayout.js").default} The layout.
  268. * @private
  269. */
  270. parseGeometryLayout_() {
  271. let layout = GeometryLayout.XY
  272. const dimToken = this.token_
  273. if (this.isTokenType(TokenType.TEXT)) {
  274. const dimInfo = dimToken.value
  275. if (dimInfo === Z)
  276. layout = GeometryLayout.XYZ
  277. else if (dimInfo === M)
  278. layout = GeometryLayout.XYM
  279. else if (dimInfo === ZM)
  280. layout = GeometryLayout.XYZM
  281. if (layout !== GeometryLayout.XY)
  282. this.consume_()
  283. }
  284. return layout
  285. }
  286. /**
  287. * @return {!Array<import("../geom/Geometry.js").default>} A collection of geometries.
  288. * @private
  289. */
  290. parseGeometryCollectionText_() {
  291. if (this.match(TokenType.LEFT_PAREN)) {
  292. const geometries = []
  293. do
  294. geometries.push(this.parseGeometry_())
  295. while (this.match(TokenType.COMMA))
  296. if (this.match(TokenType.RIGHT_PAREN))
  297. return geometries
  298. } else if (this.isEmptyGeometry_()) {
  299. return []
  300. }
  301. throw new Error(this.formatErrorMessage_())
  302. }
  303. /**
  304. * @return {Array<number>} All values in a point.
  305. * @private
  306. */
  307. parsePointText_() {
  308. if (this.match(TokenType.LEFT_PAREN)) {
  309. const coordinates = this.parsePoint_()
  310. if (this.match(TokenType.RIGHT_PAREN))
  311. return coordinates
  312. } else if (this.isEmptyGeometry_()) {
  313. return null
  314. }
  315. throw new Error(this.formatErrorMessage_())
  316. }
  317. /**
  318. * @return {!Array<!Array<number>>} All points in a linestring.
  319. * @private
  320. */
  321. parseLineStringText_() {
  322. if (this.match(TokenType.LEFT_PAREN)) {
  323. const coordinates = this.parsePointList_()
  324. if (this.match(TokenType.RIGHT_PAREN))
  325. return coordinates
  326. } else if (this.isEmptyGeometry_()) {
  327. return []
  328. }
  329. throw new Error(this.formatErrorMessage_())
  330. }
  331. /**
  332. * @return {!Array<!Array<!Array<number>>>} All points in a polygon.
  333. * @private
  334. */
  335. parsePolygonText_() {
  336. if (this.match(TokenType.LEFT_PAREN)) {
  337. const coordinates = this.parseLineStringTextList_()
  338. if (this.match(TokenType.RIGHT_PAREN))
  339. return coordinates
  340. } else if (this.isEmptyGeometry_()) {
  341. return []
  342. }
  343. throw new Error(this.formatErrorMessage_())
  344. }
  345. /**
  346. * @return {!Array<!Array<number>>} All points in a multipoint.
  347. * @private
  348. */
  349. parseMultiPointText_() {
  350. if (this.match(TokenType.LEFT_PAREN)) {
  351. let coordinates
  352. if (this.token_.type == TokenType.LEFT_PAREN)
  353. coordinates = this.parsePointTextList_()
  354. else
  355. coordinates = this.parsePointList_()
  356. if (this.match(TokenType.RIGHT_PAREN))
  357. return coordinates
  358. } else if (this.isEmptyGeometry_()) {
  359. return []
  360. }
  361. throw new Error(this.formatErrorMessage_())
  362. }
  363. /**
  364. * @return {!Array<!Array<!Array<number>>>} All linestring points
  365. * in a multilinestring.
  366. * @private
  367. */
  368. parseMultiLineStringText_() {
  369. if (this.match(TokenType.LEFT_PAREN)) {
  370. const coordinates = this.parseLineStringTextList_()
  371. if (this.match(TokenType.RIGHT_PAREN))
  372. return coordinates
  373. } else if (this.isEmptyGeometry_()) {
  374. return []
  375. }
  376. throw new Error(this.formatErrorMessage_())
  377. }
  378. /**
  379. * @return {!Array<!Array<!Array<!Array<number>>>>} All polygon points in a multipolygon.
  380. * @private
  381. */
  382. parseMultiPolygonText_() {
  383. if (this.match(TokenType.LEFT_PAREN)) {
  384. const coordinates = this.parsePolygonTextList_()
  385. if (this.match(TokenType.RIGHT_PAREN))
  386. return coordinates
  387. } else if (this.isEmptyGeometry_()) {
  388. return []
  389. }
  390. throw new Error(this.formatErrorMessage_())
  391. }
  392. /**
  393. * @return {!Array<number>} A point.
  394. * @private
  395. */
  396. parsePoint_() {
  397. const coordinates = []
  398. const dimensions = this.layout_.length
  399. for (let i = 0; i < dimensions; ++i) {
  400. const token = this.token_
  401. if (this.match(TokenType.NUMBER))
  402. coordinates.push(/** @type {number} */(token.value))
  403. else
  404. break
  405. }
  406. if (coordinates.length == dimensions)
  407. return coordinates
  408. throw new Error(this.formatErrorMessage_())
  409. }
  410. /**
  411. * @return {!Array<!Array<number>>} An array of points.
  412. * @private
  413. */
  414. parsePointList_() {
  415. const coordinates = [this.parsePoint_()]
  416. while (this.match(TokenType.COMMA))
  417. coordinates.push(this.parsePoint_())
  418. return coordinates
  419. }
  420. /**
  421. * @return {!Array<!Array<number>>} An array of points.
  422. * @private
  423. */
  424. parsePointTextList_() {
  425. const coordinates = [this.parsePointText_()]
  426. while (this.match(TokenType.COMMA))
  427. coordinates.push(this.parsePointText_())
  428. return coordinates
  429. }
  430. /**
  431. * @return {!Array<!Array<!Array<number>>>} An array of points.
  432. * @private
  433. */
  434. parseLineStringTextList_() {
  435. const coordinates = [this.parseLineStringText_()]
  436. while (this.match(TokenType.COMMA))
  437. coordinates.push(this.parseLineStringText_())
  438. return coordinates
  439. }
  440. /**
  441. * @return {!Array<!Array<!Array<!Array<number>>>>} An array of points.
  442. * @private
  443. */
  444. parsePolygonTextList_() {
  445. const coordinates = [this.parsePolygonText_()]
  446. while (this.match(TokenType.COMMA))
  447. coordinates.push(this.parsePolygonText_())
  448. return coordinates
  449. }
  450. /**
  451. * @return {boolean} Whether the token implies an empty geometry.
  452. * @private
  453. */
  454. isEmptyGeometry_() {
  455. const isEmpty =
  456. this.isTokenType(TokenType.TEXT) && this.token_.value == EMPTY
  457. if (isEmpty)
  458. this.consume_()
  459. return isEmpty
  460. }
  461. /**
  462. * Create an error message for an unexpected token error.
  463. * @return {string} Error message.
  464. * @private
  465. */
  466. formatErrorMessage_() {
  467. return (
  468. 'Unexpected `' +
  469. this.token_.value +
  470. '` at position ' +
  471. this.token_.position +
  472. ' in `' +
  473. this.lexer_.wkt +
  474. '`'
  475. )
  476. }
  477. /**
  478. * @return {!import("../geom/Geometry.js").default} The geometry.
  479. * @private
  480. */
  481. parseGeometry_() {
  482. const factory = this.factory
  483. const o2c = ordinates => new Coordinate(...ordinates)
  484. const ca2p = coordinates => {
  485. const rings = coordinates.map(a => factory.createLinearRing(a.map(o2c)))
  486. if (rings.length > 1)
  487. return factory.createPolygon(rings[0], rings.slice(1))
  488. else
  489. return factory.createPolygon(rings[0])
  490. }
  491. const token = this.token_
  492. if (this.match(TokenType.TEXT)) {
  493. const geomType = token.value
  494. this.layout_ = this.parseGeometryLayout_()
  495. if (geomType == 'GEOMETRYCOLLECTION') {
  496. const geometries = this.parseGeometryCollectionText_()
  497. return factory.createGeometryCollection(geometries)
  498. } else {
  499. switch (geomType) {
  500. case 'POINT': {
  501. const ordinates = this.parsePointText_()
  502. if (!ordinates)
  503. return factory.createPoint()
  504. return factory.createPoint(new Coordinate(...ordinates))
  505. }
  506. case 'LINESTRING': {
  507. const coordinates = this.parseLineStringText_()
  508. const components = coordinates.map(o2c)
  509. return factory.createLineString(components)
  510. }
  511. case 'LINEARRING': {
  512. const coordinates = this.parseLineStringText_()
  513. const components = coordinates.map(o2c)
  514. return factory.createLinearRing(components)
  515. }
  516. case 'POLYGON': {
  517. const coordinates = this.parsePolygonText_()
  518. if (!coordinates || coordinates.length === 0)
  519. return factory.createPolygon()
  520. return ca2p(coordinates)
  521. }
  522. case 'MULTIPOINT': {
  523. const coordinates = this.parseMultiPointText_()
  524. if (!coordinates || coordinates.length === 0)
  525. return factory.createMultiPoint()
  526. const components = coordinates.map(o2c).map(c => factory.createPoint(c))
  527. return factory.createMultiPoint(components)
  528. }
  529. case 'MULTILINESTRING': {
  530. const coordinates = this.parseMultiLineStringText_()
  531. const components = coordinates.map(a => factory.createLineString(a.map(o2c)))
  532. return factory.createMultiLineString(components)
  533. }
  534. case 'MULTIPOLYGON': {
  535. const coordinates = this.parseMultiPolygonText_()
  536. if (!coordinates || coordinates.length === 0)
  537. return factory.createMultiPolygon()
  538. const polygons = coordinates.map(ca2p)
  539. return factory.createMultiPolygon(polygons)
  540. }
  541. default: {
  542. throw new Error('Invalid geometry type: ' + geomType)
  543. }
  544. }
  545. }
  546. }
  547. throw new Error(this.formatErrorMessage_())
  548. }
  549. }
  550. /**
  551. * @param {Point} geom Point geometry.
  552. * @return {string} Coordinates part of Point as WKT.
  553. */
  554. function encodePointGeometry(geom) {
  555. if (geom.isEmpty())
  556. return ''
  557. const c = geom.getCoordinate()
  558. const cs = [c.x, c.y]
  559. if (c.z !== undefined && !Number.isNaN(c.z))
  560. cs.push(c.z)
  561. if (c.m !== undefined && !Number.isNaN(c.m))
  562. cs.push(c.m)
  563. return cs.join(' ')
  564. }
  565. /**
  566. * @param {MultiPoint} geom MultiPoint geometry.
  567. * @return {string} Coordinates part of MultiPoint as WKT.
  568. */
  569. function encodeMultiPointGeometry(geom) {
  570. const array = []
  571. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i)
  572. array.push('(' + encodePointGeometry(geom.getGeometryN(i)) + ')')
  573. return array.join(', ')
  574. }
  575. /**
  576. * @param {GeometryCollection} geom GeometryCollection geometry.
  577. * @return {string} Coordinates part of GeometryCollection as WKT.
  578. */
  579. function encodeGeometryCollectionGeometry(geom) {
  580. const array = []
  581. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i)
  582. array.push(encode(geom.getGeometryN(i)))
  583. return array.join(', ')
  584. }
  585. /**
  586. * @param {LineString|import("../geom/LinearRing.js").default} geom LineString geometry.
  587. * @return {string} Coordinates part of LineString as WKT.
  588. */
  589. function encodeLineStringGeometry(geom) {
  590. const coordinates = geom.getCoordinates()
  591. .map(c => {
  592. const a = [c.x, c.y]
  593. if (c.z !== undefined && !Number.isNaN(c.z))
  594. a.push(c.z)
  595. if (c.m !== undefined && !Number.isNaN(c.m))
  596. a.push(c.m)
  597. return a
  598. })
  599. const array = []
  600. for (let i = 0, ii = coordinates.length; i < ii; ++i)
  601. array.push(coordinates[i].join(' '))
  602. return array.join(', ')
  603. }
  604. /**
  605. * @param {MultiLineString} geom MultiLineString geometry.
  606. * @return {string} Coordinates part of MultiLineString as WKT.
  607. */
  608. function encodeMultiLineStringGeometry(geom) {
  609. const array = []
  610. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i)
  611. array.push('(' + encodeLineStringGeometry(geom.getGeometryN(i)) + ')')
  612. return array.join(', ')
  613. }
  614. /**
  615. * @param {Polygon} geom Polygon geometry.
  616. * @return {string} Coordinates part of Polygon as WKT.
  617. */
  618. function encodePolygonGeometry(geom) {
  619. const array = []
  620. array.push('(' + encodeLineStringGeometry(geom.getExteriorRing()) + ')')
  621. for (let i = 0, ii = geom.getNumInteriorRing(); i < ii; ++i)
  622. array.push('(' + encodeLineStringGeometry(geom.getInteriorRingN(i)) + ')')
  623. return array.join(', ')
  624. }
  625. /**
  626. * @param {MultiPolygon} geom MultiPolygon geometry.
  627. * @return {string} Coordinates part of MultiPolygon as WKT.
  628. */
  629. function encodeMultiPolygonGeometry(geom) {
  630. const array = []
  631. for (let i = 0, ii = geom.getNumGeometries(); i < ii; ++i)
  632. array.push('(' + encodePolygonGeometry(geom.getGeometryN(i)) + ')')
  633. return array.join(', ')
  634. }
  635. /**
  636. * @param {Geometry} geom Geometry geometry.
  637. * @return {string} Potential dimensional information for WKT type.
  638. */
  639. function encodeGeometryLayout(geom) {
  640. let dimInfo = ''
  641. if (geom.isEmpty())
  642. return dimInfo
  643. const c = geom.getCoordinate()
  644. if (c.z !== undefined && !Number.isNaN(c.z))
  645. dimInfo += Z
  646. if (c.m !== undefined && !Number.isNaN(c.m))
  647. dimInfo += M
  648. return dimInfo
  649. }
  650. /**
  651. * @const
  652. * @type {Object<string, function(import("../geom/Geometry.js").default): string>}
  653. */
  654. const GeometryEncoder = {
  655. 'Point': encodePointGeometry,
  656. 'LineString': encodeLineStringGeometry,
  657. 'LinearRing': encodeLineStringGeometry,
  658. 'Polygon': encodePolygonGeometry,
  659. 'MultiPoint': encodeMultiPointGeometry,
  660. 'MultiLineString': encodeMultiLineStringGeometry,
  661. 'MultiPolygon': encodeMultiPolygonGeometry,
  662. 'GeometryCollection': encodeGeometryCollectionGeometry,
  663. }
  664. /**
  665. * Encode a geometry as WKT.
  666. * @param {!import("../geom/Geometry.js").default} geom The geometry to encode.
  667. * @return {string} WKT string for the geometry.
  668. */
  669. function encode(geom) {
  670. let type = geom.getGeometryType()
  671. const geometryEncoder = GeometryEncoder[type]
  672. type = type.toUpperCase()
  673. const dimInfo = encodeGeometryLayout(geom)
  674. if (dimInfo.length > 0)
  675. type += ' ' + dimInfo
  676. if (geom.isEmpty())
  677. return type + ' ' + EMPTY
  678. const enc = geometryEncoder(geom)
  679. return type + ' (' + enc + ')'
  680. }
  681. /**
  682. * Class for reading and writing Well-Known Text.
  683. *
  684. * NOTE: Adapted from OpenLayers.
  685. */
  686. export default class WKTParser {
  687. /** Create a new parser for WKT
  688. *
  689. * @param {GeometryFactory} geometryFactory
  690. * @return An instance of WKTParser.
  691. * @private
  692. */
  693. constructor(geometryFactory) {
  694. this.geometryFactory = geometryFactory || new GeometryFactory()
  695. this.precisionModel = this.geometryFactory.getPrecisionModel()
  696. }
  697. /**
  698. * Deserialize a WKT string and return a geometry. Supports WKT for POINT,
  699. * MULTIPOINT, LINESTRING, LINEARRING, MULTILINESTRING, POLYGON, MULTIPOLYGON,
  700. * and GEOMETRYCOLLECTION.
  701. *
  702. * @param {String} wkt A WKT string.
  703. * @return {Geometry} A geometry instance.
  704. * @private
  705. */
  706. read(wkt) {
  707. const lexer = new Lexer(wkt)
  708. const parser = new Parser(lexer, this.geometryFactory)
  709. const geometry = parser.parse()
  710. return geometry
  711. }
  712. /**
  713. * Serialize a geometry into a WKT string.
  714. *
  715. * @param {Geometry} geometry A feature or array of features.
  716. * @return {String} The WKT string representation of the input geometries.
  717. * @private
  718. */
  719. write(geometry) {
  720. return encode(geometry)
  721. }
  722. }