OL3Parser.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* eslint-disable no-undef */
  2. /**
  3. * @module org/locationtech/jts/io/OL3Parser
  4. */
  5. import Coordinate from '../geom/Coordinate'
  6. import GeometryFactory from '../geom/GeometryFactory'
  7. function p2c(p) {
  8. return [p.x, p.y]
  9. }
  10. export default class OL3Parser {
  11. /**
  12. * OpenLayers Geometry parser and writer
  13. * @param {GeometryFactory} geometryFactory
  14. * @param {ol} olReference
  15. */
  16. constructor(geometryFactory, olReference) {
  17. this.geometryFactory = geometryFactory || new GeometryFactory()
  18. this.ol = olReference || (typeof ol !== 'undefined' && ol)
  19. }
  20. /**
  21. * Inject OpenLayers geom classes
  22. */
  23. inject(Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection) {
  24. this.ol = {
  25. geom: {
  26. Point, LineString, LinearRing, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection
  27. }
  28. }
  29. }
  30. /**
  31. * @param geometry {ol.geom.Geometry}
  32. * @return {Geometry}
  33. * @memberof module:org/locationtech/jts/io/OL3Parser#
  34. */
  35. read(geometry) {
  36. const ol = this.ol
  37. if (geometry instanceof ol.geom.Point)
  38. return this.convertFromPoint(geometry)
  39. else if (geometry instanceof ol.geom.LineString)
  40. return this.convertFromLineString(geometry)
  41. else if (geometry instanceof ol.geom.LinearRing)
  42. return this.convertFromLinearRing(geometry)
  43. else if (geometry instanceof ol.geom.Polygon)
  44. return this.convertFromPolygon(geometry)
  45. else if (geometry instanceof ol.geom.MultiPoint)
  46. return this.convertFromMultiPoint(geometry)
  47. else if (geometry instanceof ol.geom.MultiLineString)
  48. return this.convertFromMultiLineString(geometry)
  49. else if (geometry instanceof ol.geom.MultiPolygon)
  50. return this.convertFromMultiPolygon(geometry)
  51. else if (geometry instanceof ol.geom.GeometryCollection) return this.convertFromCollection(geometry)
  52. }
  53. convertFromPoint(point) {
  54. const coordinates = point.getCoordinates()
  55. return this.geometryFactory.createPoint(new Coordinate(coordinates[0], coordinates[1]))
  56. }
  57. convertFromLineString(lineString) {
  58. return this.geometryFactory.createLineString(lineString.getCoordinates().map(function(coordinates) {
  59. return new Coordinate(coordinates[0], coordinates[1])
  60. }))
  61. }
  62. convertFromLinearRing(linearRing) {
  63. return this.geometryFactory.createLinearRing(linearRing.getCoordinates().map(function(coordinates) {
  64. return new Coordinate(coordinates[0], coordinates[1])
  65. }))
  66. }
  67. convertFromPolygon(polygon) {
  68. const linearRings = polygon.getLinearRings()
  69. let shell = null
  70. const holes = []
  71. for (let i = 0; i < linearRings.length; i++) {
  72. const linearRing = this.convertFromLinearRing(linearRings[i])
  73. if (i === 0)
  74. shell = linearRing
  75. else holes.push(linearRing)
  76. }
  77. return this.geometryFactory.createPolygon(shell, holes)
  78. }
  79. convertFromMultiPoint(multiPoint) {
  80. const points = multiPoint.getPoints().map(function(point) {
  81. return this.convertFromPoint(point)
  82. }, this)
  83. return this.geometryFactory.createMultiPoint(points)
  84. }
  85. convertFromMultiLineString(multiLineString) {
  86. const lineStrings = multiLineString.getLineStrings().map(function(lineString) {
  87. return this.convertFromLineString(lineString)
  88. }, this)
  89. return this.geometryFactory.createMultiLineString(lineStrings)
  90. }
  91. convertFromMultiPolygon(multiPolygon) {
  92. const polygons = multiPolygon.getPolygons().map(function(polygon) {
  93. return this.convertFromPolygon(polygon)
  94. }, this)
  95. return this.geometryFactory.createMultiPolygon(polygons)
  96. }
  97. convertFromCollection(collection) {
  98. const geometries = collection.getGeometries().map(function(geometry) {
  99. return this.read(geometry)
  100. }, this)
  101. return this.geometryFactory.createGeometryCollection(geometries)
  102. }
  103. /**
  104. * @param geometry
  105. * {Geometry}
  106. * @return {ol.geom.Geometry}
  107. * @memberof module:org/locationtech/jts/io/OL3Parser#
  108. */
  109. write(geometry) {
  110. if (geometry.getGeometryType() === 'Point')
  111. return this.convertToPoint(geometry.getCoordinate())
  112. else if (geometry.getGeometryType() === 'LineString')
  113. return this.convertToLineString(geometry)
  114. else if (geometry.getGeometryType() === 'LinearRing')
  115. return this.convertToLinearRing(geometry)
  116. else if (geometry.getGeometryType() === 'Polygon')
  117. return this.convertToPolygon(geometry)
  118. else if (geometry.getGeometryType() === 'MultiPoint')
  119. return this.convertToMultiPoint(geometry)
  120. else if (geometry.getGeometryType() === 'MultiLineString')
  121. return this.convertToMultiLineString(geometry)
  122. else if (geometry.getGeometryType() === 'MultiPolygon')
  123. return this.convertToMultiPolygon(geometry)
  124. else if (geometry.getGeometryType() === 'GeometryCollection') return this.convertToCollection(geometry)
  125. }
  126. convertToPoint(coordinate) {
  127. return new this.ol.geom.Point([coordinate.x, coordinate.y])
  128. }
  129. convertToLineString(lineString) {
  130. const points = lineString._points._coordinates.map(p2c)
  131. return new this.ol.geom.LineString(points)
  132. }
  133. convertToLinearRing(linearRing) {
  134. const points = linearRing._points._coordinates.map(p2c)
  135. return new this.ol.geom.LinearRing(points)
  136. }
  137. convertToPolygon(polygon) {
  138. const rings = [polygon._shell._points._coordinates.map(p2c)]
  139. for (let i = 0; i < polygon._holes.length; i++) rings.push(polygon._holes[i]._points._coordinates.map(p2c))
  140. return new this.ol.geom.Polygon(rings)
  141. }
  142. convertToMultiPoint(multiPoint) {
  143. return new this.ol.geom.MultiPoint(multiPoint.getCoordinates().map(p2c))
  144. }
  145. convertToMultiLineString(multiLineString) {
  146. const lineStrings = []
  147. for (let i = 0; i < multiLineString._geometries.length; i++) lineStrings.push(this.convertToLineString(multiLineString._geometries[i]).getCoordinates())
  148. return new this.ol.geom.MultiLineString(lineStrings)
  149. }
  150. convertToMultiPolygon(multiPolygon) {
  151. const polygons = []
  152. for (let i = 0; i < multiPolygon._geometries.length; i++) polygons.push(this.convertToPolygon(multiPolygon._geometries[i]).getCoordinates())
  153. return new this.ol.geom.MultiPolygon(polygons)
  154. }
  155. convertToCollection(geometryCollection) {
  156. const geometries = []
  157. for (let i = 0; i < geometryCollection._geometries.length; i++) {
  158. const geometry = geometryCollection._geometries[i]
  159. geometries.push(this.write(geometry))
  160. }
  161. return new this.ol.geom.GeometryCollection(geometries)
  162. }
  163. }