我有这个坐标数组,最终目标是将彼此之间30分钟之内的坐标分组。
有关如何用Java脚本实现此目标的任何指示?,我正在使用的示例数据如下。
{
"gps_locations": [{
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "21.08.2018 at 22:45:04",
"unixtime": 1534891504
},
"geometry": {
"type": "Point",
"coordinates": [-90.3837, -0.66886666666667]
}
}, {
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "21.08.2018 at 16:05:04",
"unixtime": 1534867504
},
"geometry": {
"type": "Point",
"coordinates": [-78.494933333333, -0.21085]
}
}, {
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "18.01.2018 at 01:32:03",
"unixtime": 1516239123
},
"geometry": {
"type": "Point",
"coordinates": [-78.494993333333, -0.20414]
}
}, {
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "31.07.2010 at 09:02:48",
"unixtime": 1280566968
},
"geometry": {
"type": "Point",
"coordinates": [13.03573045, 47.466879033333]
}
}, {
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "31.07.2010 at 07:02:04",
"unixtime": 1280559724
},
"geometry": {
"type": "Point",
"coordinates": [13.03573045, 47.466879033333]
}
}, {
"type": "gps_location",
"properties": {
"layer": "Features",
"device": "0",
"timestamp": "31.07.2010 at 07:02:04",
"unixtime": 1280559724
},
"geometry": {
"type": "Point",
"coordinates": [13.03573045, 47.466879033333]
}
}]
}
答案 0 :(得分:0)
以下是一个可能适合您的版本:
const last = (xs) => xs[xs.length - 1]
const groupByDiff = (diff, period) => (locs) => locs.sort(diff).reduce(
(all, loc) => {
if (!all.length || diff(loc, last(last(all))) > period) {
all.push([])
}
last(all).push(loc)
return all;
},
[]
)
const toDate = (ts) => new Date(`${ts.slice(6, 10)}-${ts.slice(3, 5)}-${ts.slice(0, 2)}T${ts.slice(14)}`)
const timestampDifference = (a, b) => toDate(a.properties.timestamp) - toDate(b.properties.timestamp)
const input = {"gps_locations": [{"geometry": {"coordinates": [13.03573045, 47.466879033333], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "31.07.2010 at 07:02:04", "unixtime": 1280559724}, "type": "gps_location"}, {"geometry": {"coordinates": [13.03573045, 47.466879033333], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "31.07.2010 at 07:02:04", "unixtime": 1280559724}, "type": "gps_location"}, {"geometry": {"coordinates": [13.03573045, 47.466879033333], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "31.07.2010 at 09:02:48", "unixtime": 1280566968}, "type": "gps_location"}, {"geometry": {"coordinates": [-78.494993333333, -0.20414], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "18.01.2018 at 01:32:03", "unixtime": 1516239123}, "type": "gps_location"}, {"geometry": {"coordinates": [-78.494933333333, -0.21085], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "21.08.2018 at 16:05:04", "unixtime": 1534867504}, "type": "gps_location"}, {"geometry": {"coordinates": [-90.3837, -0.66886666666667], "type": "Point"}, "properties": {"device": "0", "layer": "Features", "timestamp": "21.08.2018 at 22:45:04", "unixtime": 1534891504}, "type": "gps_location"}]}
const groupedLocations = groupByDiff(timestampDifference, 1000 * 60 * 30)(input.gps_locations)
console.log(groupedLocations)
请注意API:函数groupByDiff
相当通用,接受一个函数,该函数可以找到两个元素之间的数值差和阈值周期(以毫秒为单位),并返回一个接受对象列表,排序和分组的函数他们根据那个时期。
last
是一个简单的辅助函数,可以避免重复xs[xs.length]
多次,包括嵌套一次。 toDate
使用您的一个时间戳并将其转换为日期。 timestampDifference
占据两个位置,获取它们的时间戳,将它们都转换为日期,并求出它们的差(以毫秒为单位)。
最后,groupByDiff
是主要功能。我们首先提供timestampDifference
和30分钟,然后为结果函数提供原始数据的gps_locations
属性,并返回位置列表的列表。
当然,const groupedLocations = groupByDiff(timestampDifference, 1000 * 60 * 30)
是一种函数,您可以在拥有此数据结构的任何时间重复使用。 groupByDiff
您可以在许多数据结构中重复使用。