drawExFunc.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. import * as maptalks from 'maptalks'
  2. // import colorString from 'color-string'
  3. import { PathUwbDev, PathCamera } from './symbolSvg'
  4. import Color from 'color'
  5. /**
  6. * @method DrawEx
  7. * @param mapEngine
  8. * @param coordinate
  9. * @param symbol
  10. * @param drawMode
  11. * @constructor
  12. */
  13. function DrawEx(mapEngine, coordinate, symbol, drawMode) {
  14. switch (drawMode.toLowerCase()) {
  15. case 'anchor':
  16. drawAnchor(mapEngine, coordinate, symbol)
  17. break
  18. case 'camera':
  19. drawCamera(mapEngine, coordinate, symbol)
  20. break
  21. case 'textbox':
  22. drawText(mapEngine, coordinate, symbol)
  23. break
  24. case 'label':
  25. drawLabel(mapEngine, coordinate, symbol)
  26. break
  27. case 'dylabel':
  28. drawDylabel(mapEngine, coordinate, symbol)
  29. break
  30. }
  31. }
  32. function drawAnchor(mapEngine, coordinate, symbol, addr, buildingId, floorId) {
  33. if (!symbol) symbol = {}
  34. if (!addr) addr = 0
  35. if (!buildingId) buildingId = 0
  36. if (!floorId) floorId = 0
  37. const geo = new maptalks.Marker(
  38. coordinate,
  39. {
  40. 'symbol': {
  41. 'markerType': 'path',
  42. 'markerPath': PathUwbDev,
  43. 'markerPathWidth': 1024,
  44. 'markerPathHeight': 1024,
  45. 'markerFill': '#0000ff', // will override tiger path's style properties
  46. // 'markerLineColor' : 12,
  47. 'markerWidth': 32,
  48. 'markerHeight': 32,
  49. 'markerDy': 16,
  50. 'markerDx': 0,
  51. 'textName': '{jobId}\nA{addr}',
  52. 'textDy': -35
  53. },
  54. 'properties': {
  55. 'type': 'anchor',
  56. 'defaultFill': '#0000ff',
  57. 'selectFill': '#FF0000',
  58. 'altitude': symbol.altitude,
  59. 'addr': addr,
  60. 'buildingId': buildingId,
  61. 'floorId': floorId
  62. }
  63. }
  64. )
  65. Math.random()
  66. if (!geo.getId()) {
  67. const id = 'Anchor' + Math.floor(Math.random() * 10000)
  68. geo.setId(id)
  69. geo.isLock = false
  70. }
  71. mapEngine.selLaycon.addGeometry(geo)
  72. }
  73. function drawCamera(mapEngine, coordinate, symbol, addr) {
  74. if (!symbol) symbol = {}
  75. if (!addr) addr = 0
  76. const geo = new maptalks.Marker(
  77. coordinate,
  78. {
  79. 'symbol': {
  80. 'markerType': 'path',
  81. 'markerPath': PathCamera,
  82. 'markerPathWidth': 1024,
  83. 'markerPathHeight': 1024,
  84. 'markerFill': '#0000ff', // will override tiger path's style properties
  85. // 'markerLineColor' : 12,
  86. 'markerWidth': 32,
  87. 'markerHeight': 32,
  88. 'markerDy': 0,
  89. 'markerDx': 0,
  90. 'textName': '{jobId}\nA{addr}',
  91. 'textDy': 12
  92. },
  93. 'properties': {
  94. 'altitude': symbol.altitude,
  95. 'addr': addr,
  96. 'type': 'camera'
  97. }
  98. }
  99. )
  100. Math.random()
  101. if (!geo.getId()) {
  102. const id = 'Camera' + Math.floor(Math.random() * 10000)
  103. geo.setId(id)
  104. geo.isLock = false
  105. }
  106. mapEngine.selLaycon.addGeometry(geo)
  107. }
  108. function drawDylabel(mapEngine, coordinate, symbol) {
  109. if (!symbol) symbol = {}
  110. //const c = new Color(symbol.fillColor)
  111. const geo = new maptalks.Label('{val}' /* content */, coordinate, /* coordinate */
  112. {
  113. 'draggable': true,
  114. 'editable': false,
  115. 'boxStyle': {
  116. 'padding': [12, 8],
  117. 'verticalAlignment': 'top',
  118. 'horizontalAlignment': 'left',
  119. 'minWidth': symbol.boxWidth,
  120. 'minHeight': symbol.boxHeight,
  121. 'symbol': {
  122. 'markerType': 'square',
  123. 'markerFill': symbol.fillColor,
  124. 'markerFillOpacity': 0,
  125. 'markerLineColor': symbol.lineColor,
  126. 'markerLineWidth': 0
  127. }
  128. },
  129. 'textSymbol': {
  130. 'textFaceName': 'monospace',
  131. 'textFill': symbol.textFill,
  132. 'textHaloFill': symbol.textHaloFill,
  133. 'textHaloRadius': symbol.textHaloRadius,
  134. 'textSize': symbol.textSize,
  135. 'textWeight': 'bold',
  136. 'textVerticalAlignment': 'top'
  137. },
  138. 'properties': {
  139. 'val': 'XXXX.XX',
  140. 'altitude': symbol.altitude,
  141. 'action': '',
  142. 'type': 'dylabel'
  143. }
  144. })
  145. Math.random()
  146. if (!geo.getId()) {
  147. const id = 'Dylabel' + Math.floor(Math.random() * 10000)
  148. geo.setId(id)
  149. geo.isLock = false
  150. }
  151. mapEngine.selLaycon.addGeometry(geo)
  152. }
  153. function drawLabel(mapEngine, coordinate, symbol) {
  154. if (!symbol) symbol = {}
  155. const c = new Color(symbol.fillColor)
  156. const geo = new maptalks.Label('编辑文本' /* content */, coordinate, /* coordinate */
  157. {
  158. 'draggable': true,
  159. 'boxStyle': {
  160. 'padding': [12, 8],
  161. 'verticalAlignment': 'top',
  162. 'horizontalAlignment': 'left',
  163. 'minWidth': symbol.boxWidth,
  164. 'minHeight': symbol.boxHeight,
  165. 'symbol': {
  166. 'markerType': 'square',
  167. 'markerFill': symbol.fillColor,
  168. 'markerFillOpacity': c.alpha(),
  169. 'markerLineColor': symbol.lineColor,
  170. 'markerLineWidth': symbol.lineWidth
  171. }
  172. },
  173. 'textSymbol': {
  174. 'textFaceName': 'monospace',
  175. 'textFill': symbol.textFill,
  176. 'textHaloFill': symbol.textHaloFill,
  177. 'textHaloRadius': symbol.textHaloRadius,
  178. 'textSize': symbol.textSize,
  179. 'textWeight': 'bold',
  180. 'textVerticalAlignment': 'top'
  181. },
  182. 'properties': {
  183. 'altitude': symbol.altitude,
  184. 'action': ''
  185. }
  186. })
  187. Math.random()
  188. if (!geo.getId()) {
  189. const id = 'Label' + Math.floor(Math.random() * 10000)
  190. geo.setId(id)
  191. geo.isLock = false
  192. }
  193. mapEngine.selLaycon.addGeometry(geo)
  194. }
  195. function drawText(mapEngine, coordinate, symbol) {
  196. if (!symbol) symbol = {}
  197. const c = new Color(symbol.fillColor)
  198. const geo = new maptalks.TextBox('文本框' /* content */, coordinate, /* coordinate */
  199. symbol.boxWidth /* width */, symbol.boxHeight /* height */,
  200. {
  201. 'draggable': true,
  202. 'textStyle': {
  203. 'wrap': true, // auto wrap text
  204. 'padding': [12, 8], // padding of textbox
  205. 'verticalAlignment': 'top',
  206. 'horizontalAlignment': 'left',
  207. 'symbol': {
  208. 'textFaceName': 'monospace',
  209. 'textFill': symbol.textFill,
  210. 'textHaloFill': symbol.textHaloFill,
  211. 'textHaloRadius': symbol.textHaloRadius,
  212. 'textSize': symbol.textSize,
  213. 'textWeight': 'bold'
  214. }
  215. },
  216. 'boxSymbol': {
  217. // box's symbol
  218. 'markerType': 'square',
  219. 'markerFill': symbol.fillColor,
  220. 'markerFillOpacity': c.alpha(),
  221. 'markerLineColor': symbol.lineColor,
  222. 'markerLineWidth': symbol.lineWidth
  223. },
  224. properties: {
  225. 'altitude': symbol.altitude,
  226. 'action': ''
  227. }
  228. })
  229. Math.random()
  230. if (!geo.getId()) {
  231. const id = 'Text' + Math.floor(Math.random() * 10000)
  232. geo.setId(id)
  233. geo.isLock = false
  234. }
  235. mapEngine.selLaycon.addGeometry(geo)
  236. }
  237. function pointInPolygon(polygon, pt) {
  238. let oddNodes = false
  239. let j = polygon.length - 1
  240. for (let i = 0; i < polygon.length; i++) {
  241. if ((polygon[i].y < pt.y && polygon[j].y >= pt.y ||
  242. polygon[j].y < pt.y && polygon[i].y >= pt.y) &&
  243. (polygon[i].x <= pt.x || polygon[j].x <= pt.x)) {
  244. if (polygon[i].x + (pt.y - polygon[i].y) / (polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) < pt.x) {
  245. oddNodes = !oddNodes
  246. }
  247. }
  248. j = i
  249. }
  250. return oddNodes
  251. }
  252. function nearPoints(polygon, pt) {
  253. let idx = -1
  254. let minDis = -1
  255. for (let i = 0; i < polygon.length; i++) {
  256. const p = polygon[i]
  257. const dis = (p.x - pt.x) * (p.x - pt.x) + (p.y - pt.y) * (p.y - pt.y)
  258. if (dis < minDis || minDis < 0) {
  259. minDis = dis
  260. idx = i
  261. }
  262. }
  263. const vec = []
  264. vec.push(polygon[(idx - 1 + polygon.length) % polygon.length])
  265. vec.push(polygon[(idx + polygon.length) % polygon.length])
  266. vec.push(polygon[(idx + 1 + polygon.length) % polygon.length])
  267. return vec
  268. }
  269. function getLineFunc(pt1, pt2) {
  270. const a = pt2.y - pt1.y
  271. const b = pt1.x - pt2.x
  272. const c = pt1.y * (pt2.x - pt1.x) - pt1.x * (pt2.y - pt1.y)
  273. return { a: a, b: b, c: c }
  274. }
  275. function getDisToLine(a, b, c, pt) {
  276. return Math.abs(a * pt.x + b * pt.y + c) / Math.sqrt(a * a + c * c)
  277. }
  278. function getCrossPtInOrth(a, b, c, pt) {
  279. const d = a * pt.y - b * pt.x
  280. const xd = -(b * d + a * c) / (b * b + a * a)
  281. let yd = 0
  282. if (Math.abs(b) < 0.000001) {
  283. yd = pt.y
  284. } else {
  285. yd = -(a * xd + c) / b
  286. }
  287. return { x: xd, y: yd }
  288. }
  289. function getCrossPtInOrthByPts(pt1, pt2, pts) {
  290. const { a, b, c } = getLineFunc(pt1, pt2)
  291. return getCrossPtInOrth(a, b, c, pts)
  292. }
  293. export {
  294. DrawEx,
  295. pointInPolygon,
  296. nearPoints,
  297. getLineFunc,
  298. getDisToLine,
  299. getCrossPtInOrth,
  300. getCrossPtInOrthByPts
  301. }