Javascript - 在两组数字之间添加缺失值



我脑子里有一个概念,但不完全确定实现它的最佳方式。

我正在研究(在 JavaScript 中(在两点之间填充缺失的坐标数据值(纬度/纬度对(的方法。

我在下面有两个坐标对:

Point A  lat="32.7188350" lon="-16.7611710"
Point B lat="32.711461" lon="-16.911347"

我首先需要将每对的纬度和经度拉出到一个单独的值中。

所以。。。

latA="32.718835"
lonA="-16.761171"
latB="32.711461"
lonB="-16.911347"

我想以该精度在 latA 和 latB 之间插入额外的(缺失(值。 在此示例中,我们将创建 7373 个额外的数字/纬度点

32.718835 - 32.711461 = 0.007374

然后,我想为每个纬度点插入相同数量的相应纵向点 (7373(。
最终结果将是 7373 个额外的坐标对 - 从而填补空白。

关于如何实施这一点的任何建议都非常感谢。

注意:我假设你想要一个 A 和 B 之间线中的点列表

无论这两个点如何相互关联,以下代码都将运行,即如果 latA 大于或小于 latB,并且 lonA 在任意组合中大于或小于 lonB

它可能有点复杂,"整数"的使用可能有点过头了,但这应该让你知道如何进行

var latA=32.718835,
lonA=-16.761171,
latB=32.711461,
lonB=-16.911347,
lat = Math.floor(latA * 1e6), // use integers
lon = Math.floor(lonA * 1e6),
count = Math.floor(Math.abs(latA-latB)*1e6)+1, // count is always positive, and we add 1 so the following is correct
lonDiff = (lonB-lonA) / count * 1e6, // this can be either positive or negative, we don't care, it's multiplied by 1e6 so we use integers
latSign = Math.sign(latB - latA), // so we go in the right direction
arr = [];

for (let i = 0; i <= count; i++) { // use <= so we have both A and B in the array
arr.push({
lat: (lat / 1e6).toFixed(6),
lon: (lon / 1e6).toFixed(6)
});
lat += latSign;
lon += lonDiff;

}
console.log(arr[0], arr.slice(-1)); // just to show that first/last points are correct

但是,使用此代码,点数始终由纬度差异决定 - 在这种情况下,对于每个 1/1000000 度的纬度,经度大约有 20/1000000 度的变化 - 不确定是否要根据纬度与纬度的最大或最小差异来插值点

下面的代码是通用的 - 因为它是,它将选择更大的差异进行循环 - 代码中的注释显示了要更改的两个位置,以使其行为与上面的代码完全相同

var latA=32.718835,
lonA=-16.761171,
latB=32.711461,
lonB=-16.911347,
lat = Math.floor(latA * 1e6), // use integers
lon = Math.floor(lonA * 1e6),
countLat = Math.floor(Math.abs(latA-latB)*1e6)+1,
countLon = Math.floor(Math.abs(lonA-lonB)*1e6)+1,
count = Math.max(countLat, countLon), // change to Math.min to use least number of points
lonDiff = (lonB-lonA) / countLat * 1e6,
latDiff = (latB-latA) / countLon * 1e6,
latSign = Math.sign(latB - latA),
lonSign = Math.sign(lonB - lonA),
arr = [];

if (countLat < countLon) { // change to > to use least number of points
lonDiff = lonSign;
} else {
latDiff = latSign;
}

for (let i = 0; i <= count; i++) {
arr.push({
lat: (lat / 1e6).toFixed(6),
lon: (lon / 1e6).toFixed(6)
});
lat += latDiff;
lon += lonDiff;

}
console.log(arr.length, arr[0], arr.slice(-1)[0]);

我已经解决了类似的任务:

let points = [[32.7188350, -16.7611710],[32.711461, -16.911347]];
let result = [];
for (let i = 0; i < points.length-1; i++) {
	result.push([
Math.round(Math.abs(points[i][0] - points[i+1][0])*1000000)/1000000,
Math.round(Math.abs(points[i][1] - points[i+1][1])*1000000)/1000000
]);
}
alert(result);

据我了解,这个问题适合线性函数,要填充 A 和 B 之间的点,我们必须应用由这些点 A 和 B 定义的函数。这里有一个例子:

var pointB=["-16.911347","32.711461"];//x,y
var pointA=["-16.7611710","32.7188350"];//x,y
var slope = (pointB[1]-pointA[1])/(pointB[0]-pointA[0]);
var b = pointB[1] - slope*pointB[0];
var steps = Math.ceil((pointA[1] - pointB[1])*1000000);
var points = []//<-- here the points
for(let i= 1; i<=steps; i++){
let y = +pointB[1]+(i/1000000);
points.push([(y-b)/slope,y])
}
console.log(points[points.length-1])//<- just the last value

这将在 A 和 B 之间绘制一条直线。我们选择y来查找x的值。

最新更新