BFL_Coordinate.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "BFL_Coordinate.h"
  3. #include "Math/UnrealMathUtility.h"
  4. FVector2D UBFL_Coordinate::GCJ02ToWGS84(double Longitude, double Latitude)
  5. {
  6. const FVector2D Coord(Longitude, Latitude);
  7. if (IsOutOfChina(Coord))
  8. {
  9. return Coord; // 中国境外直接返回原坐标
  10. }
  11. // 计算偏移量
  12. const double dLng = TransformLongitude(Longitude - 105.0, Latitude - 35.0);
  13. const double dLat = TransformLatitude(Longitude - 105.0, Latitude - 35.0);
  14. const double radLat = FMath::DegreesToRadians(Latitude);
  15. const double magic = FMath::Sin(radLat);
  16. const double sqrtMagic = FMath::Sqrt(1 - ee * magic * magic);
  17. // 计算精确偏移
  18. const double finalDLat = (dLat * 180.0) / ((a * (1 - ee)) / (sqrtMagic * (1 - ee * magic * magic)) * PI);
  19. const double finalDLng = (dLng * 180.0) / (a / sqrtMagic * FMath::Cos(radLat) * PI);
  20. // 计算墨卡托投影坐标
  21. const double mgLat = Latitude + finalDLat;
  22. const double mgLng = Longitude + finalDLng;
  23. // 返回WGS84坐标
  24. return FVector2D(
  25. Longitude * 2 - mgLng,
  26. Latitude * 2 - mgLat
  27. );
  28. }
  29. bool UBFL_Coordinate::IsOutOfChina(const FVector2D& Coord)
  30. {
  31. return !(Coord.X >= 72.004 && Coord.X <= 137.8347 &&
  32. Coord.Y >= 0.8293 && Coord.Y <= 55.8271);
  33. }
  34. double UBFL_Coordinate::TransformLatitude(double lng, double lat)
  35. {
  36. double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat +
  37. 0.1 * lng * lat + 0.2 * FMath::Sqrt(FMath::Abs(lng));
  38. ret += (20.0 * FMath::Sin(6.0 * lng * PI) +
  39. 20.0 * FMath::Sin(2.0 * lng * PI)) * 2.0 / 3.0;
  40. ret += (20.0 * FMath::Sin(lat * PI) +
  41. 40.0 * FMath::Sin(lat / 3.0 * PI)) * 2.0 / 3.0;
  42. ret += (160.0 * FMath::Sin(lat / 12.0 * PI) +
  43. 320.0 * FMath::Sin(lat * PI / 30.0)) * 2.0 / 3.0;
  44. return ret;
  45. }
  46. double UBFL_Coordinate::TransformLongitude(double lng, double lat)
  47. {
  48. double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng +
  49. 0.1 * lng * lat + 0.1 * FMath::Sqrt(FMath::Abs(lng));
  50. ret += (20.0 * FMath::Sin(6.0 * lng * PI) +
  51. 20.0 * FMath::Sin(lng * PI)) * 2.0 / 3.0;
  52. ret += (20.0 * FMath::Sin(lng * PI) +
  53. 40.0 * FMath::Sin(lng / 3.0 * PI)) * 2.0 / 3.0;
  54. ret += (150.0 * FMath::Sin(lng / 12.0 * PI) +
  55. 300.0 * FMath::Sin(lng / 30.0 * PI)) * 2.0 / 3.0;
  56. return ret;
  57. }
  58. FColor UBFL_Coordinate::HexToColor(FString HexString)
  59. {
  60. return FColor::FromHex(HexString);
  61. }
  62. FString UBFL_Coordinate::ColorToHex(FColor Color)
  63. {
  64. return Color.ToHex();
  65. }