{"version":3,"file":"GridPoint.mjs","sources":["../../../../../../packages/sdk/utils/Dilution/GridPoint.ts"],"sourcesContent":["import L from 'leaflet'\nimport type { Map } from 'leaflet'\n\ninterface GridPoint {\n  lat: number\n  lng: number\n}\n\nexport class GridSystem {\n  private map: Map\n  private gridWidth = 0.1\n  private gridHeight = 0.1\n\n  constructor(map: Map, gridWidth = 0.5, gridHeight = 0.5) {\n    this.map = map\n    this.gridWidth = gridWidth\n    this.gridHeight = gridHeight\n    this.map.on('zoomend', this.handleZoomChange.bind(this))\n  }\n  private handleZoomChange() {\n    // 根据当前缩放级别动态计算网格宽度和高度\n    this.gridWidth = this.calculateGridWidthBasedOnZoom()\n    this.gridHeight = this.calculateGridHeightBasedOnZoom()\n    console.log(this.gridWidth, this.gridHeight)\n    // // 重新构建网格存储结构\n    // this.grid = this.initializeGrid()\n  }\n  private calculateGridWidthBasedOnZoom(): number {\n    const mapContainer = this.map.getContainer()\n    const containerWidth = mapContainer.clientWidth\n\n    const desiredPixelGridsPerRow = 60 // 希望每行显示的像素网格数（可根据实际需求调整）\n    const pixelWidthPerGrid = containerWidth / desiredPixelGridsPerRow\n\n    // 使用地图的latLngToContainerPoint方法将像素宽度转换为地理距离\n    const swCorner = this.map.containerPointToLatLng(L.point(0, 0))\n    const neCorner = this.map.containerPointToLatLng(L.point(containerWidth, 0))\n    const latLngWidth = neCorner.distanceTo(swCorner)\n\n    return (pixelWidthPerGrid * latLngWidth) / containerWidth / 10000\n  }\n\n  private calculateGridHeightBasedOnZoom(): number {\n    return this.calculateGridWidthBasedOnZoom() // 假设网格宽高比为1:1\n  }\n\n  /**\n   * 计算给定点所属网格的索引。\n   * @param point - 坐标点\n   */\n  getGridIndex(point: L.LatLng): {\n    gridLngIndex: number\n    gridLatIndex: number\n  } {\n    const { lat, lng } = point\n    const gridLngIndex = Math.floor(lng / this.gridWidth)\n    const gridLatIndex = Math.floor(lat / this.gridHeight)\n    return { gridLngIndex, gridLatIndex }\n  }\n\n  /**\n   * 构建网格存储结构\n   * @param bounds - 数据分布的地理边界\n   */\n  initializeGrid(): GridPoint[][] {\n    const { lat: west, lng: south } = this.map.getBounds().getSouthWest()\n    const { lat: east, lng: north } = this.map.getBounds().getNorthEast()\n    const numLngCells = Math.ceil((east - west) / this.gridWidth)\n    const numLatCells = Math.ceil((north - south) / this.gridHeight)\n\n    return Array.from({ length: numLatCells }, () =>\n      // eslint-disable-next-line unicorn/no-new-array\n      new Array(numLngCells).fill(null as GridPoint | null)\n    )\n  }\n\n  /**\n   * 将坐标点集合进行网格化筛选，存储至指定网格\n   * @param points - 原始坐标点集合\n   */\n  filterAndStorePoints(points: L.LatLng[]): L.LatLng[] {\n    this.handleZoomChange()\n    const grid: GridPoint[][] = this.initializeGrid()\n    for (const point of points) {\n      const { gridLngIndex, gridLatIndex } = this.getGridIndex(point)\n\n      if (!grid[gridLatIndex][gridLngIndex]) {\n        grid[gridLatIndex][gridLngIndex] = { lat: point.lat, lng: point.lng }\n      } else {\n        // 非空网格，根据选择策略决定是否替换现有点\n        // 这里仅作为示例，未实现具体策略\n        // ...\n      }\n    }\n    return this.getFilteredPoints(grid)\n  }\n\n  getFilteredPoints(grid: GridPoint[][]): L.LatLng[] {\n    return (\n      grid\n        // @ts-ignore: TS2550 TS7006\n        .flatMap((row) =>\n          // @ts-ignore: TS7006\n          row.map((cell) => {\n            if (cell) {\n              return L.latLng(cell.lat, cell.lng)\n            }\n            return null\n          })\n        )\n        .filter(Boolean) as L.LatLng[]\n    )\n  }\n}\n\n// 使用示例\n// const map = L.map('map').setView([51.505, -0.09], 13);\n// const gridSystem = new GridSystem(map);\n// const originalPoints: L.LatLng[] = [...]; // 原始坐标点集合\n// const grid = gridSystem.filterAndStorePoints(originalPoints);\n"],"names":["GridSystem","constructor","n","t","i","this","gridWidth","gridHeight","map","on","handleZoomChange","bind","calculateGridWidthBasedOnZoom","calculateGridHeightBasedOnZoom","getContainer","clientWidth","e","r","containerPointToLatLng","o","point","distanceTo","getGridIndex","lat","lng","gridLngIndex","Math","floor","gridLatIndex","initializeGrid","getBounds","getSouthWest","getNorthEast","ceil","a","Array","from","length","fill","filterAndStorePoints","getFilteredPoints","flatMap","latLng","filter","Boolean"],"mappings":"uBAA8B,MAAMA,EAAW,WAAAC,CAAYC,EAAEC,EAAE,GAAGC,EAAE,IAAIC,KAAKC,UAAU,GAAGD,KAAKE,WAAW,GAAGF,KAAKG,IAAIN,EAAEG,KAAKC,UAAUH,EAAEE,KAAKE,WAAWH,EAAEC,KAAKG,IAAIC,GAAG,UAAUJ,KAAKK,iBAAiBC,KAAKN,MAAM,CAAC,gBAAAK,GAAmBL,KAAKC,UAAUD,KAAKO,gCAAgCP,KAAKE,WAAWF,KAAKQ,gCAA4E,CAAC,6BAAAD,GAAgC,MAAMT,EAAEE,KAAKG,IAAIM,eAAeC,YAAYC,EAAEb,EAAE,GAAGc,EAAEZ,KAAKG,IAAIU,uBAAuBC,EAAEC,MAAM,EAAE,IAAkE,OAAOJ,EAAnEX,KAAKG,IAAIU,uBAAuBC,EAAEC,MAAMjB,EAAE,IAAIkB,WAAWJ,GAAcd,EAAE,GAAG,CAAC,8BAAAU,GAAiC,OAAOR,KAAKO,+BAA+B,CAAC,YAAAU,CAAapB,GAAG,MAAMqB,IAAIpB,EAAEqB,IAAIpB,GAAGF,EAAiE,MAAM,CAACuB,aAApEC,KAAKC,MAAMvB,EAAEC,KAAKC,WAAiEsB,aAApDF,KAAKC,MAAMxB,EAAEE,KAAKE,YAAiD,CAAC,cAAAsB,GAAiB,MAAMN,IAAIrB,EAAEsB,IAAIrB,GAAGE,KAAKG,IAAIsB,YAAYC,gBAAgBR,IAAInB,EAAEoB,IAAIR,GAAGX,KAAKG,IAAIsB,YAAYE,eAAef,EAAES,KAAKO,MAAM7B,EAAEF,GAAGG,KAAKC,WAAW4B,EAAER,KAAKO,MAAMjB,EAAEb,GAAGE,KAAKE,YAAY,OAAO4B,MAAMC,KAAK,CAACC,OAAOH,IAAG,IAAI,IAAIC,MAAM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