| | |
| | | } |
| | | |
| | | /** |
| | | * 拆分网格为细分网格 |
| | | * 拆分网格为细分网格,所有网格应该是相同边长的正方形 |
| | | * 根据相似矩形的原理,可以分别按比例得到每个细分网格的经纬度 |
| | | * @param gridCellList 原始网格数组 |
| | | * @param scale 拆分的系数,例如 2,表示将原有网格按边长的 1/2 拆分成 2 * 2 的4个网格 |
| | | * @param groupId 细分后的网格所属的网格组id |
| | | * @return |
| | | */ |
| | | fun splitGrid(gridCellList: List<GridCell>, scale: Int): List<GridCell> { |
| | | fun splitGrid(gridCellList: List<GridCell?>, scale: Int, groupId:Int): List<GridCell?> { |
| | | if (scale <= 0) throw IllegalArgumentException("网格拆分的数量不能小于1") |
| | | // 拆分系数为1,则表示不拆分 |
| | | if (scale == 1) return gridCellList |
| | |
| | | |
| | | // 根据函数[calGridVertex]生成的网格4个顶点坐标,以上北下南左西右东方向为标准 |
| | | // 计算首个网格中心坐标点分别和4个顶点的经纬度差值 |
| | | val p = gridCellList[0] |
| | | val p = gridCellList.find { it != null }!! |
| | | // (通过近似平面坐标系的方式)根据单个原始网格的4个顶点坐标,分别确定以下三组坐标点之间的经纬度单位偏移量 |
| | | val p1 = p.point1Lon to p.point1Lat |
| | | val p2 = p.point2Lon to p.point2Lat |
| | |
| | | val dxC = (p.longitude - p1.first) / scale.toBigDecimal() |
| | | val dyC = (p.latitude - p1.second) / scale.toBigDecimal() |
| | | |
| | | // 网格行循环 |
| | | for (row in 0 until scale) { |
| | | val newGridCell1 = GridCell() |
| | | // 网格索引 |
| | | var cellIndex = 0 |
| | | |
| | | // 确定每一行首个细分网格的中心坐标和4个顶点坐标 |
| | | // 左上角顶点根据所在行数在原始网格顶点基础上增加偏移量 |
| | | newGridCell1.point1Lon = p1.first + dx1 * row.toBigDecimal() |
| | | newGridCell1.point1Lat = p1.second + dy1 * row.toBigDecimal() |
| | | // 左下角顶点根据所在行数在原始网格顶点基础上增加偏移量(比左上角顶点多一个偏移) |
| | | newGridCell1.point3Lon = p1.first + dx1 * (row + 1).toBigDecimal() |
| | | newGridCell1.point3Lat = p1.second + dy1 * (row + 1).toBigDecimal() |
| | | // 右上角顶点在细分网格左上角的基础上增加相应的偏移量 |
| | | newGridCell1.point2Lon = newGridCell1.point1Lon + dx2 |
| | | newGridCell1.point2Lat = newGridCell1.point1Lat + dy2 |
| | | // 右下角顶点在细分网格左下角的基础上增加相应的偏移量 |
| | | newGridCell1.point4Lon = newGridCell1.point3Lon + dx3 |
| | | newGridCell1.point4Lat = newGridCell1.point3Lat + dy3 |
| | | // 中心点在细分网格左上角的基础上增加固定偏移量 |
| | | newGridCell1.longitude = newGridCell1.point1Lon + dxC |
| | | newGridCell1.latitude = newGridCell1.point1Lat + dyC |
| | | // 对网格组内的所有网格进行网格细分 |
| | | gridCellList.forEach { g -> |
| | | if (g == null) return@forEach |
| | | |
| | | // 加入结果集合 |
| | | newGridCellList.add(newGridCell1) |
| | | // 网格行循环 |
| | | for (row in 0 until scale) { |
| | | val newGridCell1 = GridCell() |
| | | |
| | | // 网格列循环(从第2列开始) |
| | | for (col in 1 until scale) { |
| | | val newGridCell = GridCell() |
| | | newGridCell.point1Lon = newGridCell1.point1Lon + dx2 * col.toBigDecimal() |
| | | newGridCell.point1Lat = newGridCell1.point1Lat + dy2 * col.toBigDecimal() |
| | | newGridCell.point2Lon = newGridCell1.point2Lon + dx2 * col.toBigDecimal() |
| | | newGridCell.point2Lat = newGridCell1.point2Lat + dy2 * col.toBigDecimal() |
| | | newGridCell.point3Lon = newGridCell1.point3Lon + dx3 * col.toBigDecimal() |
| | | newGridCell.point3Lat = newGridCell1.point3Lat + dy3 * col.toBigDecimal() |
| | | newGridCell.point4Lon = newGridCell1.point4Lon + dx3 * col.toBigDecimal() |
| | | newGridCell.point4Lat = newGridCell1.point4Lat + dy3 * col.toBigDecimal() |
| | | newGridCell.longitude = newGridCell.point1Lon + dxC |
| | | newGridCell.latitude = newGridCell.point1Lat + dyC |
| | | // 确定每一行首个细分网格的中心坐标和4个顶点坐标 |
| | | // 左上角顶点根据所在行数在原始网格顶点基础上增加偏移量 |
| | | newGridCell1.point1Lon = g.point1Lon + dx1 * row.toBigDecimal() |
| | | newGridCell1.point1Lat = g.point1Lat + dy1 * row.toBigDecimal() |
| | | // 左下角顶点根据所在行数在原始网格顶点基础上增加偏移量(比左上角顶点多一个偏移) |
| | | newGridCell1.point3Lon = g.point1Lon + dx1 * (row + 1).toBigDecimal() |
| | | newGridCell1.point3Lat = g.point1Lat + dy1 * (row + 1).toBigDecimal() |
| | | // 右上角顶点在细分网格左上角的基础上增加相应的偏移量 |
| | | newGridCell1.point2Lon = newGridCell1.point1Lon + dx2 |
| | | newGridCell1.point2Lat = newGridCell1.point1Lat + dy2 |
| | | // 右下角顶点在细分网格左下角的基础上增加相应的偏移量 |
| | | newGridCell1.point4Lon = newGridCell1.point3Lon + dx3 |
| | | newGridCell1.point4Lat = newGridCell1.point3Lat + dy3 |
| | | // 中心点在细分网格左上角的基础上增加固定偏移量 |
| | | newGridCell1.longitude = newGridCell1.point1Lon + dxC |
| | | newGridCell1.latitude = newGridCell1.point1Lat + dyC |
| | | |
| | | newGridCellList.add(newGridCell) |
| | | newGridCell1.groupId = groupId |
| | | newGridCell1.cellIndex = ++cellIndex |
| | | newGridCell1.fatherCellIndex = g.cellIndex |
| | | |
| | | // 加入结果集合 |
| | | newGridCellList.add(newGridCell1) |
| | | |
| | | // 网格列循环(从第2列开始) |
| | | for (col in 1 until scale) { |
| | | val newGridCell = GridCell() |
| | | newGridCell.point1Lon = newGridCell1.point1Lon + dx2 * col.toBigDecimal() |
| | | newGridCell.point1Lat = newGridCell1.point1Lat + dy2 * col.toBigDecimal() |
| | | newGridCell.point2Lon = newGridCell1.point2Lon + dx2 * col.toBigDecimal() |
| | | newGridCell.point2Lat = newGridCell1.point2Lat + dy2 * col.toBigDecimal() |
| | | newGridCell.point3Lon = newGridCell1.point3Lon + dx3 * col.toBigDecimal() |
| | | newGridCell.point3Lat = newGridCell1.point3Lat + dy3 * col.toBigDecimal() |
| | | newGridCell.point4Lon = newGridCell1.point4Lon + dx3 * col.toBigDecimal() |
| | | newGridCell.point4Lat = newGridCell1.point4Lat + dy3 * col.toBigDecimal() |
| | | newGridCell.longitude = newGridCell.point1Lon + dxC |
| | | newGridCell.latitude = newGridCell.point1Lat + dyC |
| | | |
| | | newGridCell.groupId = groupId |
| | | newGridCell.cellIndex = ++cellIndex |
| | | newGridCell.fatherCellIndex = g.cellIndex |
| | | |
| | | newGridCellList.add(newGridCell) |
| | | } |
| | | } |
| | | } |
| | | |
| | | return newGridCellList |
| | | } |
| | | |