GeomUtil.java 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.shkpr.service.alambizplugin.commtools;
  2. import org.locationtech.jts.geom.GeometryFactory;
  3. import org.locationtech.jts.geom.Point;
  4. import org.locationtech.jts.geom.PrecisionModel;
  5. import java.math.BigDecimal;
  6. import java.math.RoundingMode;
  7. /**
  8. * 地理工具类
  9. *
  10. * @author 欧阳劲驰
  11. * @since JDK1.8
  12. */
  13. public class GeomUtil {
  14. /**
  15. * geom工厂
  16. */
  17. private static final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4490);
  18. /**
  19. * 地球半径
  20. */
  21. private static final int EARTH_RADIUS = 6378137;
  22. /**
  23. * 将米转换为度
  24. *
  25. * @param degrees 度
  26. * @return 对应的米
  27. */
  28. public static double convertDegreesToMeters(double degrees) {
  29. return degrees * (Math.PI / 180) * EARTH_RADIUS;
  30. }
  31. /**
  32. * 将米转换为度
  33. *
  34. * @param meters 米
  35. * @return 对应的度
  36. */
  37. public static double convertMetersToDegrees(double meters) {
  38. return meters / (Math.PI / 180 * EARTH_RADIUS);
  39. }
  40. /**
  41. * 经纬度的度转十进制
  42. * <p>如RMC(NMEA0183)协议报文:</p>
  43. * <blockquote><pre>$GNRMC,072905.00,A,3640.46260,N,11707.54950,E,000.0,000.0,050119,OK*24</pre></blockquote>
  44. * <p>则表示为:{@code [36.67438,117.12583]}</p>
  45. *
  46. * @param degrees 度
  47. * @param minutesIndex 分索引,如 {@code ddmm.mmmmm} 则为2
  48. * @return 十进制表示
  49. */
  50. public static double convertDegreesToDecimal(String degrees, int minutesIndex) {
  51. return Integer.parseInt(degrees.substring(0, minutesIndex)) +
  52. //分除60保留8位
  53. BigDecimal.valueOf(Float.parseFloat(degrees.substring(minutesIndex)))
  54. .divide(BigDecimal.valueOf(60), 8, RoundingMode.HALF_UP)
  55. .doubleValue();
  56. }
  57. /**
  58. * 判断点1是否在点2距离范围内
  59. *
  60. * @param point1 点1
  61. * @param point2 点2
  62. * @param distance 距离(米)
  63. * @return 判断状态
  64. */
  65. public static Boolean isWithinDistance(Point point1, Point point2, double distance) {
  66. return point1.isWithinDistance(point2, convertMetersToDegrees(distance));
  67. }
  68. }