leaflet-add23DLDCicle.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. function getCirclePoint(x, y, radius) {
  2. var pointArr = []
  3. for (var i = 0; i < 361; i++) {
  4. if (i < 360) {
  5. pointArr.push({
  6. x: x + radius * Math.cos(i * 2 * Math.PI / 360),
  7. y: y + radius * Math.sin(i * 2 * Math.PI / 360)
  8. })
  9. } else {
  10. pointArr.push({
  11. x: x + radius * Math.cos(0 * 2 * Math.PI / 360),
  12. y: y + radius * Math.sin(0 * 2 * Math.PI / 360)
  13. })
  14. }
  15. }
  16. return pointArr; //x - y
  17. };
  18. function addCircleToPolygon(options, allCirclePoint, extrudes) {
  19. var altitudes = [];
  20. var guid = [];
  21. var guidArr = [];
  22. for (var n = 0; n < allCirclePoint.length; n++) {
  23. var curExtrude = extrudes[n];
  24. altitudes = [];
  25. for (var j = 0; j < allCirclePoint[n].length; j++) {
  26. altitudes.push(curExtrude);
  27. }
  28. if (n == 0) {
  29. if (options.display.show2D) {
  30. map2DViewer.polygon({
  31. action: 'add',
  32. groupId:options.groupId,
  33. geojson: {
  34. "type": "Feature",
  35. "properties": {
  36. title: options.properties.title,
  37. color: options.properties.color,
  38. weight: options.properties.weight,
  39. fillColor: options.properties.fillColor,
  40. opacity: options.properties.opacity,
  41. fillOpacity: options.properties.fillOpacity,
  42. popupContent: '',
  43. extrude: 0, //拉伸高度
  44. altitude: altitudes, //点海拔高度
  45. altitudeMode: 0 //海拔模式
  46. },
  47. "geometry": {
  48. "type": "Polygon",
  49. "coordinates": [
  50. allCirclePoint[n]
  51. ]
  52. }
  53. }
  54. })
  55. }
  56. if (options.altitudeMode === 1) {
  57. if (options.display.show3D) {
  58. map3DViewer.polyline({
  59. action: 'add',
  60. groupId:options.groupId,
  61. geojson: {
  62. "type": "Feature",
  63. "properties": {
  64. title: options.properties.title,
  65. color: options.properties.color,
  66. weight: options.properties.weight,
  67. fillColor: options.properties.fillColor,
  68. opacity: options.properties.opacity,
  69. popupContent: '',
  70. extrude: 0, //拉伸高度
  71. altitude: altitudes, //点海拔高度
  72. altitudeMode: 0 //海拔模式
  73. },
  74. "geometry": {
  75. "type": "Polyline",
  76. "coordinates": allCirclePoint[n]
  77. }
  78. }
  79. })
  80. }
  81. }
  82. } else {
  83. if (options.display.show3D) {
  84. map3DViewer.polyline({
  85. action: 'add',
  86. groupId:options.groupId,
  87. geojson: {
  88. "type": "Feature",
  89. "properties": {
  90. title: options.properties.title,
  91. color: options.properties.color,
  92. weight: options.properties.weight,
  93. fillColor: options.properties.fillColor,
  94. opacity: options.properties.opacity,
  95. popupContent: '',
  96. extrude: 0, //拉伸高度
  97. altitude: altitudes, //点海拔高度
  98. altitudeMode: 1 //海拔模式
  99. },
  100. "geometry": {
  101. "type": "Polyline",
  102. "coordinates": allCirclePoint[n]
  103. }
  104. }
  105. })
  106. }
  107. }
  108. }
  109. };
  110. L.addLD = function(options) {
  111. //圆心
  112. var allCirclePoint = [];
  113. var center_lat = options.center.lat;
  114. var center_lon = options.center.lng;
  115. //半径
  116. var radius = options.radius; //米
  117. //计算圆心到极点的距离
  118. if (center_lat < 0) {
  119. var centerToPole = new L.LatLng(center_lat, center_lon).distanceTo(new L.LatLng(-90, center_lon));
  120. } else {
  121. var centerToPole = new L.LatLng(center_lat, center_lon).distanceTo(new L.LatLng(90, center_lon));
  122. }
  123. //转换定义 核心
  124. var epsg_4326 = "+proj=longlat +datum=WGS84 +no_defs "
  125. var esri_102016 = "+proj=aeqd +lat_0=90 +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
  126. var self_defined = "+proj=aeqd +lat_0=" + center_lat + "+lon_0=" + center_lon + "+x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m no_defs"
  127. var circleTangle = options.circleTangle || 5;
  128. var extrudes = [];
  129. var minExtrudes = map23DUtil.getLDCircleExtrude(radius);
  130. for (var i = 0; i <= 90; i += circleTangle) {
  131. if (i == 90) {
  132. var curHeight = radius;
  133. } else {
  134. var ral = (i * Math.PI) / 180;
  135. var curHeight = radius * Math.sin(ral);
  136. }
  137. if (i != 0) {
  138. if (curHeight < minExtrudes) {
  139. curHeight = minExtrudes;
  140. }
  141. }
  142. var curBRedius = Math.sqrt(Math.pow(radius, 2) - Math.pow(curHeight, 2));
  143. //生成圆平面点;
  144. var pointArr = getCirclePoint(0, 0, curBRedius);
  145. //圆的轮廓点转换为经纬度,当做GeoJSON来使用。
  146. var circlePoints = [];
  147. extrudes.push(curHeight);
  148. for (var k = 0; k < pointArr.length; k++) {
  149. var item = pointArr[k];
  150. //核心 将平面坐标转经纬度坐标
  151. var xy = proj4(self_defined, epsg_4326, [item.x, item.y]);
  152. circlePoints.push([
  153. xy[0],
  154. xy[1]
  155. ])
  156. }
  157. if (i == 0) { //二维雷达显示坐标处理
  158. if (curBRedius >= centerToPole) {
  159. if (center_lat < 0) {
  160. circlePoints = map23DUtil.southHeightLatLD(map23DUtil.southHeightLatLD(map23DUtil.lnglatsToDateLineLnglats(circlePoints)));
  161. circlePoints.push([circlePoints[circlePoints.length - 1][0], -90])
  162. circlePoints[circlePoints.length - 2] = [circlePoints[circlePoints.length - 3][0], -90];
  163. } else {
  164. circlePoints = map23DUtil.northHeightLatLD(map23DUtil.northHeightLatLD(map23DUtil.lnglatsToDateLineLnglats(circlePoints)));
  165. circlePoints.push([circlePoints[circlePoints.length - 1][0], 90])
  166. circlePoints[circlePoints.length - 2] = [circlePoints[circlePoints.length - 3][0], 90];
  167. }
  168. } else {
  169. circlePoints = map23DUtil.lnglatsToDateLineLnglats(circlePoints);
  170. }
  171. } else { //三维雷达显示坐标处理
  172. if (curBRedius > centerToPole) {
  173. //判断中心点是在南半球还是北半球
  174. if (center_lat < 0) {
  175. circlePoints = map23DUtil.southHeightLatLD(map23DUtil.southHeightLatLD(map23DUtil.lnglatsToDateLineLnglats(circlePoints)))
  176. } else {
  177. circlePoints = map23DUtil.northHeightLatLD(map23DUtil.northHeightLatLD(map23DUtil.lnglatsToDateLineLnglats(circlePoints)))
  178. }
  179. }
  180. }
  181. allCirclePoint.push(circlePoints)
  182. if (i == 90) {
  183. addCircleToPolygon(options, allCirclePoint, extrudes);
  184. }
  185. }
  186. };