| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- import * as maptalks from 'maptalks'
- // import colorString from 'color-string'
- import { PathUwbDev, PathCamera } from './symbolSvg'
- import Color from 'color'
- /**
- * @method DrawEx
- * @param mapEngine
- * @param coordinate
- * @param symbol
- * @param drawMode
- * @constructor
- */
- function DrawEx(mapEngine, coordinate, symbol, drawMode) {
- switch (drawMode.toLowerCase()) {
- case 'anchor':
- drawAnchor(mapEngine, coordinate, symbol)
- break
- case 'camera':
- drawCamera(mapEngine, coordinate, symbol)
- break
- case 'textbox':
- drawText(mapEngine, coordinate, symbol)
- break
- case 'label':
- drawLabel(mapEngine, coordinate, symbol)
- break
- case 'dylabel':
- drawDylabel(mapEngine, coordinate, symbol)
- break
- }
- }
- function drawAnchor(mapEngine, coordinate, symbol, addr, buildingId, floorId) {
- if (!symbol) symbol = {}
- if (!addr) addr = 0
- if (!buildingId) buildingId = 0
- if (!floorId) floorId = 0
- const geo = new maptalks.Marker(
- coordinate,
- {
- 'symbol': {
- 'markerType': 'path',
- 'markerPath': PathUwbDev,
- 'markerPathWidth': 1024,
- 'markerPathHeight': 1024,
- 'markerFill': '#0000ff', // will override tiger path's style properties
- // 'markerLineColor' : 12,
- 'markerWidth': 32,
- 'markerHeight': 32,
- 'markerDy': 16,
- 'markerDx': 0,
- 'textName': '{jobId}\nA{addr}',
- 'textDy': -35
- },
- 'properties': {
- 'type': 'anchor',
- 'defaultFill': '#0000ff',
- 'selectFill': '#FF0000',
- 'altitude': symbol.altitude,
- 'addr': addr,
- 'buildingId': buildingId,
- 'floorId': floorId
- }
- }
- )
- Math.random()
- if (!geo.getId()) {
- const id = 'Anchor' + Math.floor(Math.random() * 10000)
- geo.setId(id)
- geo.isLock = false
- }
- mapEngine.selLaycon.addGeometry(geo)
- }
- function drawCamera(mapEngine, coordinate, symbol, addr) {
- if (!symbol) symbol = {}
- if (!addr) addr = 0
- const geo = new maptalks.Marker(
- coordinate,
- {
- 'symbol': {
- 'markerType': 'path',
- 'markerPath': PathCamera,
- 'markerPathWidth': 1024,
- 'markerPathHeight': 1024,
- 'markerFill': '#0000ff', // will override tiger path's style properties
- // 'markerLineColor' : 12,
- 'markerWidth': 32,
- 'markerHeight': 32,
- 'markerDy': 0,
- 'markerDx': 0,
- 'textName': '{jobId}\nA{addr}',
- 'textDy': 12
- },
- 'properties': {
- 'altitude': symbol.altitude,
- 'addr': addr,
- 'type': 'camera'
- }
- }
- )
- Math.random()
- if (!geo.getId()) {
- const id = 'Camera' + Math.floor(Math.random() * 10000)
- geo.setId(id)
- geo.isLock = false
- }
- mapEngine.selLaycon.addGeometry(geo)
- }
- function drawDylabel(mapEngine, coordinate, symbol) {
- if (!symbol) symbol = {}
- //const c = new Color(symbol.fillColor)
- const geo = new maptalks.Label('{val}' /* content */, coordinate, /* coordinate */
- {
- 'draggable': true,
- 'editable': false,
- 'boxStyle': {
- 'padding': [12, 8],
- 'verticalAlignment': 'top',
- 'horizontalAlignment': 'left',
- 'minWidth': symbol.boxWidth,
- 'minHeight': symbol.boxHeight,
- 'symbol': {
- 'markerType': 'square',
- 'markerFill': symbol.fillColor,
- 'markerFillOpacity': 0,
- 'markerLineColor': symbol.lineColor,
- 'markerLineWidth': 0
- }
- },
- 'textSymbol': {
- 'textFaceName': 'monospace',
- 'textFill': symbol.textFill,
- 'textHaloFill': symbol.textHaloFill,
- 'textHaloRadius': symbol.textHaloRadius,
- 'textSize': symbol.textSize,
- 'textWeight': 'bold',
- 'textVerticalAlignment': 'top'
- },
- 'properties': {
- 'val': 'XXXX.XX',
- 'altitude': symbol.altitude,
- 'action': '',
- 'type': 'dylabel'
- }
- })
- Math.random()
- if (!geo.getId()) {
- const id = 'Dylabel' + Math.floor(Math.random() * 10000)
- geo.setId(id)
- geo.isLock = false
- }
- mapEngine.selLaycon.addGeometry(geo)
- }
- function drawLabel(mapEngine, coordinate, symbol) {
- if (!symbol) symbol = {}
- const c = new Color(symbol.fillColor)
- const geo = new maptalks.Label('编辑文本' /* content */, coordinate, /* coordinate */
- {
- 'draggable': true,
- 'boxStyle': {
- 'padding': [12, 8],
- 'verticalAlignment': 'top',
- 'horizontalAlignment': 'left',
- 'minWidth': symbol.boxWidth,
- 'minHeight': symbol.boxHeight,
- 'symbol': {
- 'markerType': 'square',
- 'markerFill': symbol.fillColor,
- 'markerFillOpacity': c.alpha(),
- 'markerLineColor': symbol.lineColor,
- 'markerLineWidth': symbol.lineWidth
- }
- },
- 'textSymbol': {
- 'textFaceName': 'monospace',
- 'textFill': symbol.textFill,
- 'textHaloFill': symbol.textHaloFill,
- 'textHaloRadius': symbol.textHaloRadius,
- 'textSize': symbol.textSize,
- 'textWeight': 'bold',
- 'textVerticalAlignment': 'top'
- },
- 'properties': {
- 'altitude': symbol.altitude,
- 'action': ''
- }
- })
- Math.random()
- if (!geo.getId()) {
- const id = 'Label' + Math.floor(Math.random() * 10000)
- geo.setId(id)
- geo.isLock = false
- }
- mapEngine.selLaycon.addGeometry(geo)
- }
- function drawText(mapEngine, coordinate, symbol) {
- if (!symbol) symbol = {}
- const c = new Color(symbol.fillColor)
- const geo = new maptalks.TextBox('文本框' /* content */, coordinate, /* coordinate */
- symbol.boxWidth /* width */, symbol.boxHeight /* height */,
- {
- 'draggable': true,
- 'textStyle': {
- 'wrap': true, // auto wrap text
- 'padding': [12, 8], // padding of textbox
- 'verticalAlignment': 'top',
- 'horizontalAlignment': 'left',
- 'symbol': {
- 'textFaceName': 'monospace',
- 'textFill': symbol.textFill,
- 'textHaloFill': symbol.textHaloFill,
- 'textHaloRadius': symbol.textHaloRadius,
- 'textSize': symbol.textSize,
- 'textWeight': 'bold'
- }
- },
- 'boxSymbol': {
- // box's symbol
- 'markerType': 'square',
- 'markerFill': symbol.fillColor,
- 'markerFillOpacity': c.alpha(),
- 'markerLineColor': symbol.lineColor,
- 'markerLineWidth': symbol.lineWidth
- },
- properties: {
- 'altitude': symbol.altitude,
- 'action': ''
- }
- })
- Math.random()
- if (!geo.getId()) {
- const id = 'Text' + Math.floor(Math.random() * 10000)
- geo.setId(id)
- geo.isLock = false
- }
- mapEngine.selLaycon.addGeometry(geo)
- }
- function pointInPolygon(polygon, pt) {
- let oddNodes = false
- let j = polygon.length - 1
- for (let i = 0; i < polygon.length; i++) {
- if ((polygon[i].y < pt.y && polygon[j].y >= pt.y ||
- polygon[j].y < pt.y && polygon[i].y >= pt.y) &&
- (polygon[i].x <= pt.x || polygon[j].x <= pt.x)) {
- if (polygon[i].x + (pt.y - polygon[i].y) / (polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) < pt.x) {
- oddNodes = !oddNodes
- }
- }
- j = i
- }
- return oddNodes
- }
- function nearPoints(polygon, pt) {
- let idx = -1
- let minDis = -1
- for (let i = 0; i < polygon.length; i++) {
- const p = polygon[i]
- const dis = (p.x - pt.x) * (p.x - pt.x) + (p.y - pt.y) * (p.y - pt.y)
- if (dis < minDis || minDis < 0) {
- minDis = dis
- idx = i
- }
- }
- const vec = []
- vec.push(polygon[(idx - 1 + polygon.length) % polygon.length])
- vec.push(polygon[(idx + polygon.length) % polygon.length])
- vec.push(polygon[(idx + 1 + polygon.length) % polygon.length])
- return vec
- }
- function getLineFunc(pt1, pt2) {
- const a = pt2.y - pt1.y
- const b = pt1.x - pt2.x
- const c = pt1.y * (pt2.x - pt1.x) - pt1.x * (pt2.y - pt1.y)
- return { a: a, b: b, c: c }
- }
- function getDisToLine(a, b, c, pt) {
- return Math.abs(a * pt.x + b * pt.y + c) / Math.sqrt(a * a + c * c)
- }
- function getCrossPtInOrth(a, b, c, pt) {
- const d = a * pt.y - b * pt.x
- const xd = -(b * d + a * c) / (b * b + a * a)
- let yd = 0
- if (Math.abs(b) < 0.000001) {
- yd = pt.y
- } else {
- yd = -(a * xd + c) / b
- }
- return { x: xd, y: yd }
- }
- function getCrossPtInOrthByPts(pt1, pt2, pts) {
- const { a, b, c } = getLineFunc(pt1, pt2)
- return getCrossPtInOrth(a, b, c, pts)
- }
- export {
- DrawEx,
- pointInPolygon,
- nearPoints,
- getLineFunc,
- getDisToLine,
- getCrossPtInOrth,
- getCrossPtInOrthByPts
- }
|