GeometryFactory.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import CoordinateSequenceFactory from './CoordinateSequenceFactory'
  2. import LineString from './LineString'
  3. import hasInterface from '../../../../hasInterface'
  4. import Coordinate from './Coordinate'
  5. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  6. import Point from './Point'
  7. import Polygon from './Polygon'
  8. import MultiPoint from './MultiPoint'
  9. import LinearRing from './LinearRing'
  10. import CoordinateArraySequenceFactory from './impl/CoordinateArraySequenceFactory'
  11. import MultiPolygon from './MultiPolygon'
  12. import CoordinateSequences from './CoordinateSequences'
  13. import CoordinateSequence from './CoordinateSequence'
  14. import GeometryCollection from './GeometryCollection'
  15. import PrecisionModel from './PrecisionModel'
  16. import Serializable from '../../../../java/io/Serializable'
  17. import Assert from '../util/Assert'
  18. import MultiLineString from './MultiLineString'
  19. export default class GeometryFactory {
  20. constructor() {
  21. GeometryFactory.constructor_.apply(this, arguments)
  22. }
  23. static constructor_() {
  24. this._precisionModel = null
  25. this._coordinateSequenceFactory = null
  26. this._SRID = null
  27. if (arguments.length === 0) {
  28. GeometryFactory.constructor_.call(this, new PrecisionModel(), 0)
  29. } else if (arguments.length === 1) {
  30. if (hasInterface(arguments[0], CoordinateSequenceFactory)) {
  31. const coordinateSequenceFactory = arguments[0]
  32. GeometryFactory.constructor_.call(this, new PrecisionModel(), 0, coordinateSequenceFactory)
  33. } else if (arguments[0] instanceof PrecisionModel) {
  34. const precisionModel = arguments[0]
  35. GeometryFactory.constructor_.call(this, precisionModel, 0, GeometryFactory.getDefaultCoordinateSequenceFactory())
  36. }
  37. } else if (arguments.length === 2) {
  38. const precisionModel = arguments[0], SRID = arguments[1]
  39. GeometryFactory.constructor_.call(this, precisionModel, SRID, GeometryFactory.getDefaultCoordinateSequenceFactory())
  40. } else if (arguments.length === 3) {
  41. const precisionModel = arguments[0], SRID = arguments[1], coordinateSequenceFactory = arguments[2]
  42. this._precisionModel = precisionModel
  43. this._coordinateSequenceFactory = coordinateSequenceFactory
  44. this._SRID = SRID
  45. }
  46. }
  47. static toMultiPolygonArray(multiPolygons) {
  48. const multiPolygonArray = new Array(multiPolygons.size()).fill(null)
  49. return multiPolygons.toArray(multiPolygonArray)
  50. }
  51. static toGeometryArray(geometries) {
  52. if (geometries === null) return null
  53. const geometryArray = new Array(geometries.size()).fill(null)
  54. return geometries.toArray(geometryArray)
  55. }
  56. static getDefaultCoordinateSequenceFactory() {
  57. return CoordinateArraySequenceFactory.instance()
  58. }
  59. static toMultiLineStringArray(multiLineStrings) {
  60. const multiLineStringArray = new Array(multiLineStrings.size()).fill(null)
  61. return multiLineStrings.toArray(multiLineStringArray)
  62. }
  63. static toLineStringArray(lineStrings) {
  64. const lineStringArray = new Array(lineStrings.size()).fill(null)
  65. return lineStrings.toArray(lineStringArray)
  66. }
  67. static toMultiPointArray(multiPoints) {
  68. const multiPointArray = new Array(multiPoints.size()).fill(null)
  69. return multiPoints.toArray(multiPointArray)
  70. }
  71. static toLinearRingArray(linearRings) {
  72. const linearRingArray = new Array(linearRings.size()).fill(null)
  73. return linearRings.toArray(linearRingArray)
  74. }
  75. static toPointArray(points) {
  76. const pointArray = new Array(points.size()).fill(null)
  77. return points.toArray(pointArray)
  78. }
  79. static toPolygonArray(polygons) {
  80. const polygonArray = new Array(polygons.size()).fill(null)
  81. return polygons.toArray(polygonArray)
  82. }
  83. static createPointFromInternalCoord(coord, exemplar) {
  84. exemplar.getPrecisionModel().makePrecise(coord)
  85. return exemplar.getFactory().createPoint(coord)
  86. }
  87. createEmpty(dimension) {
  88. switch (dimension) {
  89. case -1:
  90. return this.createGeometryCollection()
  91. case 0:
  92. return this.createPoint()
  93. case 1:
  94. return this.createLineString()
  95. case 2:
  96. return this.createPolygon()
  97. default:
  98. throw new IllegalArgumentException('Invalid dimension: ' + dimension)
  99. }
  100. }
  101. toGeometry(envelope) {
  102. if (envelope.isNull())
  103. return this.createPoint()
  104. if (envelope.getMinX() === envelope.getMaxX() && envelope.getMinY() === envelope.getMaxY())
  105. return this.createPoint(new Coordinate(envelope.getMinX(), envelope.getMinY()))
  106. if (envelope.getMinX() === envelope.getMaxX() || envelope.getMinY() === envelope.getMaxY())
  107. return this.createLineString([new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY())])
  108. return this.createPolygon(this.createLinearRing([new Coordinate(envelope.getMinX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMaxY()), new Coordinate(envelope.getMaxX(), envelope.getMinY()), new Coordinate(envelope.getMinX(), envelope.getMinY())]), null)
  109. }
  110. createLineString() {
  111. if (arguments.length === 0)
  112. return this.createLineString(this.getCoordinateSequenceFactory().create([]))
  113. else if (arguments.length === 1)
  114. if (arguments[0] instanceof Array) {
  115. const coordinates = arguments[0]
  116. return this.createLineString(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null)
  117. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  118. const coordinates = arguments[0]
  119. return new LineString(coordinates, this)
  120. }
  121. }
  122. createMultiLineString() {
  123. if (arguments.length === 0) {
  124. return new MultiLineString(null, this)
  125. } else if (arguments.length === 1) {
  126. const lineStrings = arguments[0]
  127. return new MultiLineString(lineStrings, this)
  128. }
  129. }
  130. buildGeometry(geomList) {
  131. let geomType = null
  132. let isHeterogeneous = false
  133. let hasGeometryCollection = false
  134. for (let i = geomList.iterator(); i.hasNext(); ) {
  135. const geom = i.next()
  136. const partType = geom.getTypeCode()
  137. if (geomType === null)
  138. geomType = partType
  139. if (partType !== geomType)
  140. isHeterogeneous = true
  141. if (geom instanceof GeometryCollection) hasGeometryCollection = true
  142. }
  143. if (geomType === null)
  144. return this.createGeometryCollection()
  145. if (isHeterogeneous || hasGeometryCollection)
  146. return this.createGeometryCollection(GeometryFactory.toGeometryArray(geomList))
  147. const geom0 = geomList.iterator().next()
  148. const isCollection = geomList.size() > 1
  149. if (isCollection) {
  150. if (geom0 instanceof Polygon)
  151. return this.createMultiPolygon(GeometryFactory.toPolygonArray(geomList))
  152. else if (geom0 instanceof LineString)
  153. return this.createMultiLineString(GeometryFactory.toLineStringArray(geomList))
  154. else if (geom0 instanceof Point)
  155. return this.createMultiPoint(GeometryFactory.toPointArray(geomList))
  156. Assert.shouldNeverReachHere('Unhandled geometry type: ' + geom0.getGeometryType())
  157. }
  158. return geom0
  159. }
  160. createMultiPointFromCoords(coordinates) {
  161. return this.createMultiPoint(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null)
  162. }
  163. createPoint() {
  164. if (arguments.length === 0)
  165. return this.createPoint(this.getCoordinateSequenceFactory().create([]))
  166. else if (arguments.length === 1)
  167. if (arguments[0] instanceof Coordinate) {
  168. const coordinate = arguments[0]
  169. return this.createPoint(coordinate !== null ? this.getCoordinateSequenceFactory().create([coordinate]) : null)
  170. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  171. const coordinates = arguments[0]
  172. return new Point(coordinates, this)
  173. }
  174. }
  175. getCoordinateSequenceFactory() {
  176. return this._coordinateSequenceFactory
  177. }
  178. createPolygon() {
  179. if (arguments.length === 0) {
  180. return this.createPolygon(null, null)
  181. } else if (arguments.length === 1) {
  182. if (hasInterface(arguments[0], CoordinateSequence)) {
  183. const shell = arguments[0]
  184. return this.createPolygon(this.createLinearRing(shell))
  185. } else if (arguments[0] instanceof Array) {
  186. const shell = arguments[0]
  187. return this.createPolygon(this.createLinearRing(shell))
  188. } else if (arguments[0] instanceof LinearRing) {
  189. const shell = arguments[0]
  190. return this.createPolygon(shell, null)
  191. }
  192. } else if (arguments.length === 2) {
  193. const shell = arguments[0], holes = arguments[1]
  194. return new Polygon(shell, holes, this)
  195. }
  196. }
  197. getSRID() {
  198. return this._SRID
  199. }
  200. createGeometryCollection() {
  201. if (arguments.length === 0) {
  202. return new GeometryCollection(null, this)
  203. } else if (arguments.length === 1) {
  204. const geometries = arguments[0]
  205. return new GeometryCollection(geometries, this)
  206. }
  207. }
  208. getPrecisionModel() {
  209. return this._precisionModel
  210. }
  211. createLinearRing() {
  212. if (arguments.length === 0)
  213. return this.createLinearRing(this.getCoordinateSequenceFactory().create([]))
  214. else if (arguments.length === 1)
  215. if (arguments[0] instanceof Array) {
  216. const coordinates = arguments[0]
  217. return this.createLinearRing(coordinates !== null ? this.getCoordinateSequenceFactory().create(coordinates) : null)
  218. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  219. const coordinates = arguments[0]
  220. return new LinearRing(coordinates, this)
  221. }
  222. }
  223. createMultiPolygon() {
  224. if (arguments.length === 0) {
  225. return new MultiPolygon(null, this)
  226. } else if (arguments.length === 1) {
  227. const polygons = arguments[0]
  228. return new MultiPolygon(polygons, this)
  229. }
  230. }
  231. createMultiPoint() {
  232. if (arguments.length === 0)
  233. return new MultiPoint(null, this)
  234. else if (arguments.length === 1)
  235. if (arguments[0] instanceof Array) {
  236. const point = arguments[0]
  237. return new MultiPoint(point, this)
  238. } else if (hasInterface(arguments[0], CoordinateSequence)) {
  239. const coordinates = arguments[0]
  240. if (coordinates === null)
  241. return this.createMultiPoint(new Array(0).fill(null))
  242. const points = new Array(coordinates.size()).fill(null)
  243. for (let i = 0; i < coordinates.size(); i++) {
  244. const ptSeq = this.getCoordinateSequenceFactory().create(1, coordinates.getDimension(), coordinates.getMeasures())
  245. CoordinateSequences.copy(coordinates, i, ptSeq, 0, 1)
  246. points[i] = this.createPoint(ptSeq)
  247. }
  248. return this.createMultiPoint(points)
  249. }
  250. }
  251. get interfaces_() {
  252. return [Serializable]
  253. }
  254. }