{"version":3,"file":"WeatherSymbolHandler.mjs","sources":["../../../../../../../packages/sdk/plugins/weather/util/WeatherSymbolHandler.ts"],"sourcesContent":["import * as L from 'leaflet'\nimport weatherWind from '@map-sdk/sdk/plugins/weather/Weather/WeatherWind'\nimport { weatherArrow } from '@map-sdk/sdk/plugins/weather/Weather/WeatherArrow'\nimport type { MyMap } from '@map-sdk/sdk/ShipxyAPISDK'\nimport type { WeatherWindOptions } from '@map-sdk/sdk/plugins/weather/Weather/WeatherWind'\nimport type { WeatherArrowOptions } from '@map-sdk/sdk/plugins/weather/Weather/WeatherArrow'\n\nexport class WeatherSymbolHandler {\n  private _symbolLayer: L.LayerGroup\n  private _map: MyMap\n\n  constructor(map: MyMap, layer: L.LayerGroup) {\n    this._map = map\n    this._symbolLayer = layer\n  }\n\n  private processWeatherData(\n    datas: { x: number; y: number; value: string }[]\n  ): void {\n    for (const data of datas) {\n      if (data) {\n        const longitude = data.x / 1000\n        const latitude = data.y / 1000\n        const latLng = L.latLng(latitude, longitude)\n        if (this._map.getBounds().contains(latLng)) {\n          const [speedValue, directionValue] = data.value.split('/')\n          if (\n            !Number.isNaN(Number.parseInt(speedValue)) &&\n            !Number.isNaN(Number.parseInt(directionValue)) &&\n            Number.parseInt(speedValue) !== -32767 &&\n            Number.parseInt(directionValue) !== -32767\n          ) {\n            const options: WeatherWindOptions = {\n              direction: Number.parseInt(directionValue),\n              size: 22,\n              speed: Number.parseInt(speedValue),\n            }\n            this._symbolLayer.addLayer(weatherWind(latLng, options))\n          }\n        }\n      }\n    }\n  }\n\n  /**\n   * 风速和风向\n   * @param datas\n   */\n  private processWindData<T>(datas: T[]) {\n    const filteredData = this.filterDataByZoomLevel(datas, 6, false)\n    for (const point of filteredData) {\n      if (point) {\n        const lat = point.y / 1000\n        const lng = point.x / 1000\n        const location = L.latLng(lat, lng)\n        if (this._map.getBounds().contains(location)) {\n          const [speedStr, directionStr] = point.value.split('/')\n          if (\n            Number.parseInt(speedStr) !== -32768 &&\n            Number.parseInt(directionStr) !== -32768\n          ) {\n            const options = {\n              direction: directionStr,\n              size: 22,\n              speed: speedStr,\n            }\n            const windLayer = weatherWind(location, options)\n            this._symbolLayer.addLayer(windLayer)\n          }\n        }\n      }\n    }\n  }\n  /**\n   * 风速和风向，带有交互式箭头\n   * @param datas\n   */\n  private processInteractiveWindArrows<T>(datas: T[]) {\n    const filteredData = this.filterDataByZoomLevel(datas, 6, false)\n    for (const point of filteredData) {\n      if (point) {\n        const lat = point.y / 1000\n        const lng = point.x / 1000\n        const location = L.latLng(lat, lng)\n        if (this._map.getBounds().contains(location)) {\n          const [speedStr, directionStr] = point.value.split('/')\n          if (\n            Math.abs(Number.parseInt(speedStr)) !== 32768 &&\n            Math.abs(Number.parseInt(directionStr)) !== 32768 &&\n            Number.parseFloat(speedStr) > 0.012\n          ) {\n            const speed = Number.parseFloat(speedStr) * 100\n            const sizeIndex =\n              speed < 1\n                ? 0\n                : [12, 15, 18, 21, 30, 27, 30, 33][\n                    Math.min(Math.floor(speed / 14), 7)\n                  ]\n            const direction = Math.round(Number.parseFloat(directionStr) + 180)\n            const arrowOptions: WeatherArrowOptions = {\n              weight: 0.1,\n              color: '#329808',\n              direction,\n              size: sizeIndex,\n              item: {\n                speed: speedStr,\n                direction,\n                direction_old: directionStr,\n              },\n            }\n            const arrowLayer = weatherArrow(location, arrowOptions)\n            this._symbolLayer.addLayer(arrowLayer)\n            arrowLayer.on('click', (event) => {\n              console.debug(event)\n            })\n          }\n        }\n      }\n    }\n  }\n  /**\n   * 显示数值标记\n   * @param datas\n   */\n  private processNumberMarkers<T>(datas: T[]) {\n    const filteredData = this.filterDataByZoomLevel(datas, 7, true)\n    for (const point of filteredData) {\n      if (point) {\n        const lat = point.y / 1000\n        const lng = point.x / 1000\n        const location = L.latLng(lat, lng)\n        if (this._map.getBounds().contains(location)) {\n          const value = point.value\n          if (Number.parseInt(value) !== -32768) {\n            const marker = L.marker(location, {\n              icon: L.divIcon({\n                html: `${Math.floor(value)}`,\n                className: 'weather-label',\n                iconSize: [40, 20],\n              }),\n            })\n            this._symbolLayer.addLayer(marker)\n          }\n        }\n      }\n    }\n  }\n\n  public displayWeatherData<T>(weatherData: T[], dataType: number): void {\n    switch (dataType) {\n      case 13: // 风速和风向\n        this.processWindData(weatherData)\n        break\n      case 15: // 风速和风向，带有交互式箭头\n        this.processInteractiveWindArrows(weatherData)\n        break\n      case 16: // 显示数值标记\n        this.processNumberMarkers(weatherData)\n        break\n      default:\n        console.error('error')\n    }\n  }\n\n  public updateWeatherSymbols(processedData: any[]): void {\n    this.processWeatherData(processedData)\n  }\n\n  private filterDataByZoomLevel(\n    data: string | any[],\n    minZoom: number,\n    specialCondition: any\n  ) {\n    const zoom = this._map.getZoom()\n    let stepX = 0\n    let stepY = 0\n    switch (zoom) {\n      case 6:\n        stepX = 2\n        stepY = 2\n        break\n      case 5:\n        stepX = 4\n        stepY = 3\n        break\n      case 4:\n        if (specialCondition) {\n          stepX = 6\n          stepY = 5\n        } else {\n          stepX = 4\n          stepY = 3\n        }\n        break\n      case 3:\n        if (specialCondition) {\n          stepX = 8\n          stepY = 13\n        } else {\n          stepX = 6\n          stepY = 11\n        }\n        break\n      case 2:\n        if (specialCondition) {\n          stepX = 13\n          stepY = 20\n        } else {\n          stepX = 8\n          stepY = 13\n        }\n        break\n      case 1:\n        stepX = 10\n        stepY = 15\n    }\n\n    if (zoom < minZoom) {\n      const xData = data.slice(0, data.length / 2)\n      const yData = data.slice(data.length / 2)\n      const filteredXData: any[] = []\n      const filteredYData: any[] = []\n\n      for (let i = 0; i < 170; i += stepX) {\n        filteredXData.push(xData.slice(420 * i, 420 * (i + 1)))\n        filteredYData.push(yData.slice(420 * i, 420 * (i + 1)))\n      }\n\n      const result: any[] = []\n      filteredXData.forEach((chunkX) => {\n        chunkX.forEach((itemX: any, innerIndex: number) => {\n          if (innerIndex % stepY === 0) {\n            result.push(itemX)\n          }\n        })\n      })\n\n      filteredYData.forEach((chunkY) => {\n        chunkY.forEach((itemY: any, innerIndex: number) => {\n          if (innerIndex % stepY === 0) {\n            result.push(itemY)\n          }\n        })\n      })\n\n      return result\n    }\n\n    return data\n  }\n}\n\n// 使用示例\n// const map = L.map('mapid').setView([51.505, -0.09], 13);\n// const weatherSymbolHandler = new WeatherSymbolHandler(map);\n// const processedData = weatherDataProcessor.processData(6, false);\n// weatherSymbolHandler.updateWeatherSymbols(processedData);\n"],"names":["WeatherSymbolHandler","constructor","t","a","this","_map","_symbolLayer","processWeatherData","r","x","c","y","s","d","latLng","getBounds","contains","e","o","value","split","Number","isNaN","parseInt","i","direction","size","speed","addLayer","f","processWindData","filterDataByZoomLevel","l","processInteractiveWindArrows","Math","abs","parseFloat","p","min","floor","m","round","u","weight","color","item","direction_old","on","h","processNumberMarkers","marker","icon","divIcon","html","className","iconSize","displayWeatherData","updateWeatherSymbols","getZoom","slice","length","n","push","forEach"],"mappings":"+HAAkL,MAAMA,EAAqB,WAAAC,CAAYC,EAAEC,GAAGC,KAAKC,KAAKH,EAAEE,KAAKE,aAAaH,CAAC,CAAC,kBAAAI,CAAmBL,GAAG,IAAI,MAAMC,KAAKD,EAAE,GAAGC,EAAE,CAAC,MAAMK,EAAEL,EAAEM,EAAE,IAAIC,EAAEP,EAAEQ,EAAE,IAAIC,EAAEC,EAAEC,OAAOJ,EAAEF,GAAG,GAAGJ,KAAKC,KAAKU,YAAYC,SAASJ,GAAG,CAAC,MAAMK,EAAEC,GAAGf,EAAEgB,MAAMC,MAAM,KAAK,IAAIC,OAAOC,MAAMD,OAAOE,SAASN,MAAMI,OAAOC,MAAMD,OAAOE,SAASL,MAA2B,QAAtBG,OAAOE,SAASN,KAAmC,QAAtBI,OAAOE,SAASL,GAAY,CAAC,MAAMM,EAAE,CAACC,UAAUJ,OAAOE,SAASL,GAAGQ,KAAK,GAAGC,MAAMN,OAAOE,SAASN,IAAIb,KAAKE,aAAasB,SAASC,EAAEjB,EAAEY,GAAG,CAAC,CAAC,CAAC,CAAC,eAAAM,CAAgB5B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