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

时间:2018-09-11 10:39:11

标签: javascript arrays sorting coordinates geocoding

我脑子里有个概念,但不确定如何实现它的最佳方法。

我正在研究(用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个坐标对,从而填补了空白。

任何有关如何实施此建议的建议。

3 个答案:

答案 0 :(得分:1)

  

注意:我假设您想要A和B之间的直线中的点列表

无论两点之间如何关联,以下代码都将运行,即latA大于或小于latB,并且lonA大于或小于lonB的任何组合

这可能有点令人费解,使用“ integers”可能有点麻烦,但这应该可以让您了解如何进行

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]);

答案 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);

答案 2 :(得分:0)

据我了解,此问题适合线性函数,为了填充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的值。