GeoToolsUtil.kt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.persagy.server.util
  2. import org.geotools.geometry.jts.JTSFactoryFinder
  3. import org.locationtech.jts.geom.LinearRing
  4. import cn.hutool.core.collection.CollUtil
  5. import com.fasterxml.jackson.databind.node.ObjectNode
  6. import com.persagy.server.datacenter.models.entities.assistant.PointPosition
  7. import org.locationtech.jts.geom.Coordinate
  8. import org.locationtech.jts.geom.Point
  9. import org.locationtech.jts.geom.Polygon
  10. import kotlin.collections.ArrayList
  11. /**
  12. * @ClassName GeoToolsUtil
  13. * @Description: 点面包含关系分析
  14. * @Author linhuili
  15. * @Date 2021/9/3 20:31
  16. * @Version V1.0
  17. */
  18. object GeoToolsUtil {
  19. /**
  20. * 创建点
  21. * @param pointLocal
  22. */
  23. private fun createPoint(pointLocal: ObjectNode): Point {
  24. var x = 0.0
  25. var y = 0.0
  26. val z = 0.0
  27. if (pointLocal["X"] != null) {
  28. x = pointLocal["X"].doubleValue()
  29. }
  30. if (pointLocal["Y"] != null) {
  31. y = pointLocal["Y"].doubleValue()
  32. }
  33. val geometryFactory = JTSFactoryFinder.getGeometryFactory()
  34. val coord = Coordinate(x, y, z)
  35. return geometryFactory.createPoint(coord)
  36. }
  37. /**
  38. * 创建面
  39. * @param outLines
  40. */
  41. private fun createPolygon(outLines: ArrayList<ArrayList<PointPosition>>?): Polygon {
  42. val pointPositions = outLines?.get(0)
  43. val geometryFactory = JTSFactoryFinder.getGeometryFactory()
  44. val objects: MutableList<Coordinate> = ArrayList()
  45. for (outLine in pointPositions!!) {
  46. var x = 0.0
  47. if(outLine.x != null){
  48. x = outLine.x!!
  49. }
  50. var y = 0.0
  51. if(outLine.y != null){
  52. y = outLine.y!!
  53. }
  54. var z = 0.0
  55. if(outLine.z!= null){
  56. z = outLine.z!!
  57. }
  58. val coordinate = Coordinate(x, y, z)
  59. objects.add(coordinate)
  60. }
  61. //注意数据的闭合
  62. val coordinates = objects.toTypedArray()
  63. val ring = geometryFactory.createLinearRing(coordinates)
  64. val holes: Array<LinearRing>? = null
  65. return geometryFactory.createPolygon(ring, holes)
  66. }
  67. /**
  68. * 点面包含关系判断
  69. * 用途:判断一个面是否包含一个点,即一个点是否在一个面内
  70. * @param pointLocal 坐标点
  71. * @param outLines 轮廓线
  72. * @return
  73. */
  74. fun isPointInPoly(pointLocal: ObjectNode?, outLines: ArrayList<ArrayList<PointPosition>>?): Boolean {
  75. if (null == pointLocal || CollUtil.isEmpty(outLines)) {
  76. return false
  77. }
  78. //创建点
  79. val point = createPoint(pointLocal)
  80. //创建面
  81. val polygon = createPolygon(outLines)
  82. //判断点面包含关系
  83. return polygon.contains(point)
  84. }
  85. }