英文:
How do I change my grid so that it counts left to right, top to bottom?
问题
我试图创建一个地图坐标的正方形网格,其中 gridOrder
计数从左上角开始,从左到右,从上到下递增。
当这些坐标显示在地图上时,gridOrder
计数应该如下所示(期望的正确版本):
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
但当前看起来是这样的(当前错误版本):
3 | 6 | 9
---+---+---
2 | 5 | 8
---+---+---
1 | 4 | 7
如何调整以下代码以按照我期望的模式从左到右、从上到下分配 gridOrder
计数?
我知道问题出在我在 map()
中分配 gridOrder
的方式上。但我已经尝试了许多不同的变化,但无法使其正常工作。
import bbox from "@turf/bbox";
import { Units, point } from "@turf/helpers";
import pointGrid from "@turf/point-grid";
import buffer from "@turf/buffer";
const createGridCoordinates = (
centerCoordinates: google.maps.LatLngLiteral,
numPointsPerSide: number,
gridDimension: number,
unitOfMeasure: Units
) => {
// Calculate the cell size
const cellSize = gridDimension / (numPointsPerSide - 1);
// Calculate half the grid size to find the distance from center to edges of bounding box
const halfGridSize = gridDimension / 2;
// Make the bounding box around a center point and a circle buffer radius
// Radius is extended by 10% of a cellSize (cellSize / 10) to include
// east and north edges when gridDimension is an exact multiple of the grid spacing
const boundingBox = bbox(
buffer(
point([centerCoordinates.lng, centerCoordinates.lat]),
halfGridSize + cellSize / 10,
{
units: unitOfMeasure,
}
)
);
// Make the grid. This is an array of grid coordinates.
const grid = pointGrid(boundingBox, cellSize, {
units: unitOfMeasure,
});
// Extract the coordinates from the grid and add gridOrder
const coordinatesGrid = grid.features.map((feature, index) => {
const [longitude, latitude] = feature.geometry.coordinates;
const coordinatesGridItem = {
coordinates: { lat: latitude, lng: longitude },
gridOrder: index + 1,
};
return coordinatesGridItem;
});
return coordinatesGrid;
};
英文:
I am trying to create a square grid of map coordinates with a gridOrder
count that starts counting in the top left corner, left to right, top to bottom.
When the coordinates are shown on a map the gridOrder
count should look like this (desired correct version):
1 | 2 | 3
---+---+---
4 | 5 | 6
---+---+---
7 | 8 | 9
But currently it looks like this (current wrong version):
3 | 6 | 9
---+---+---
2 | 5 | 8
---+---+---
1 | 4 | 7
How can I adjust the code below to assign a gridOrder
count in my desired pattern of left to right, top to bottom?
I know the problem is with how I assign gridOrder
during the map()
. But I have tried many different variations and I can't get it to work.
import bbox from "@turf/bbox";
import { Units, point } from "@turf/helpers";
import pointGrid from "@turf/point-grid";
import buffer from "@turf/buffer";
const createGridCoordinates = (
centerCoordinates: google.maps.LatLngLiteral,
numPointsPerSide: number,
gridDimension: number,
unitOfMeasure: Units
) => {
// Calculate the cell size
const cellSize = gridDimension / (numPointsPerSide - 1);
// Calculate half the grid size to find the distance from center to edges of bounding box
const halfGridSize = gridDimension / 2;
// Make the bounding box around a center point and a circle buffer radius
// Radius is extended by 10% of a cellSize (cellSize / 10) to include
// east and north edges when gridDimension is an exact multiple of the grid spacing
const boundingBox = bbox(
buffer(
point([centerCoordinates.lng, centerCoordinates.lat]),
halfGridSize + cellSize / 10,
{
units: unitOfMeasure,
}
)
);
// Make the grid. This is an array of grid coordinates.
const grid = pointGrid(boundingBox, cellSize, {
units: unitOfMeasure,
});
// Extract the coordinates from the grid and add gridOrder
const coordinatesGrid = grid.features.map((feature, index) => {
const [longitude, latitude] = feature.geometry.coordinates;
const coordinatesGridItem = {
coordinates: { lat: latitude, lng: longitude },
gridOrder: index + 1,
};
return coordinatesGridItem;
});
return coordinatesGrid;
};
答案1
得分: 0
我已解决了。
不再尝试以奇怪的方式更改 gridOrder
的值,我只是在分配 gridOrder
之前对网格坐标进行了排序。
以下是完整的工作代码:
const createGridCoordinates = (
centerCoordinates: google.maps.LatLngLiteral,
numPointsPerSide: number,
gridDimension: number,
unitOfMeasure: Units
) => {
// 计算单元格大小
const cellSize = gridDimension / (numPointsPerSide - 1);
// 计算半个网格大小以找到中心到边界框边缘的距离
const halfGridSize = gridDimension / 2;
// 创建围绕中心点和圆形缓冲半径的边界框
// 半径扩展了单元格大小的 10%(cellSize / 10)以包括
// 当 gridDimension 是网格间距的精确倍数时,东部和北部的边缘
const boundingBox = bbox(
buffer(
point([centerCoordinates.lng, centerCoordinates.lat]),
halfGridSize + cellSize / 10,
{
units: unitOfMeasure,
}
)
);
// 创建网格
const grid = pointGrid(boundingBox, cellSize, {
units: unitOfMeasure,
});
// 创建网格坐标的普通数组
const coordinates = grid.features.map((feature) => {
const [longitude, latitude] = feature.geometry.coordinates;
const coordinatesGridItem = {
coordinates: { lat: latitude, lng: longitude },
};
return coordinatesGridItem;
});
// 按纬度(y)降序排列,然后按经度(x)升序排列
coordinates.sort((a, b) => {
const coord1 = a.coordinates;
const coord2 = b.coordinates;
if (coord1.lat > coord2.lat) {
return -1;
}
if (coord1.lat < coord2.lat) {
return 1;
}
if (coord1.lng < coord2.lng) {
return -1;
}
if (coord1.lng > coord2.lng) {
return 1;
}
return 0;
});
// 将 gridOrder 添加到新数组中
const coordinatesGrid = coordinates.map((coordinate, index) => {
const coordinatesGridItem = {
...coordinate,
gridOrder: index + 1,
};
return coordinatesGridItem;
});
return coordinatesGrid;
};
希望对你有所帮助。
英文:
I figured it out.
Instead of trying to change the value of gridOrder
in strange ways, I just sorted the grid coordinates before assigning gridOrder
.
Here is the full working code:
const createGridCoordinates = (
centerCoordinates: google.maps.LatLngLiteral,
numPointsPerSide: number,
gridDimension: number,
unitOfMeasure: Units
) => {
// Calculate the cell size
const cellSize = gridDimension / (numPointsPerSide - 1);
// Calculate half the grid size to find the distance from center to edges of bounding box
const halfGridSize = gridDimension / 2;
// Make the bounding box around a center point and a circle buffer radius
// Radius is extended by 10% of a cellSize (cellSize / 10) to include
// east and north edges when gridDimension is an exact multiple of the grid spacing
const boundingBox = bbox(
buffer(
point([centerCoordinates.lng, centerCoordinates.lat]),
halfGridSize + cellSize / 10,
{
units: unitOfMeasure,
}
)
);
// Make the grid
const grid = pointGrid(boundingBox, cellSize, {
units: unitOfMeasure,
});
// Make a normal array of the grid coordinates
const coordinates = grid.features.map((feature) => {
const [longitude, latitude] = feature.geometry.coordinates;
const coordinatesGridItem = {
coordinates: { lat: latitude, lng: longitude },
};
return coordinatesGridItem;
});
// Sort by latitude (y) in descending order, then by longitude (x) in ascending order
coordinates.sort((a, b) => {
const coord1 = a.coordinates;
const coord2 = b.coordinates;
if (coord1.lat > coord2.lat) {
return -1;
}
if (coord1.lat < coord2.lat) {
return 1;
}
if (coord1.lng < coord2.lng) {
return -1;
}
if (coord1.lng > coord2.lng) {
return 1;
}
return 0;
});
// Add gridOrder to the new array
const coordinatesGrid = coordinates.map((coordinate, index) => {
const coordinatesGridItem = {
...coordinate,
gridOrder: index + 1,
};
return coordinatesGridItem;
});
return coordinatesGrid;
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论