Skip to content

Commit

Permalink
新功能:新增异常GPS噪点识别降噪功能
Browse files Browse the repository at this point in the history
  • Loading branch information
李民 committed Oct 8, 2024
1 parent e5a1c9f commit b73d65a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# 核心功能介绍
- 自动识别停留点
- 密集停留点降噪
- 异常GPS噪点识别降噪
- 卡片式信息展示
- 轨迹补偿(需要配置高德密钥。国外需要配置Google Map密钥)
- 使用门槛低
Expand Down
39 changes: 37 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,49 @@ function formatDistance(meters) {
let kilometers = meters / oneKilometer; // 保留两位小数
return `${kilometers.toFixed(2)}${getI18nValue(config.locale,'kilometer')}`;
}
// noiseRecognitionDistance
function noiseRecognitionFilter(points) {
if (points.length <= 2) {
return points; // 如果点太少,无法计算平均值,直接返回
}

// 提取所有经纬度
const latitudes = points.map(point => point.lat);
const longitudes = points.map(point => point.lng);

// 计算最大值和最小值
const maxLat = Math.max(...latitudes);
const minLat = Math.min(...latitudes);
const maxLng = Math.max(...longitudes);
const minLng = Math.min(...longitudes);

// 计算平均值,去掉最大和最小
const avgLat = (latitudes.reduce((sum, lat) => sum + lat, 0) - maxLat - minLat) / (latitudes.length - 2);
const avgLng = (longitudes.reduce((sum, lng) => sum + lng, 0) - maxLng - minLng) / (longitudes.length - 2);

// 计算阈值,考虑正负
const latThreshold = Math.abs(avgLat * 0.5);
const lngThreshold = Math.abs(avgLng * 0.5);

// 过滤点,去掉超出范围的点
const filteredPoints = points.filter(point => {
const isLatValid = point.lat >= (avgLat - latThreshold) && point.lat <= (avgLat + latThreshold);
const isLngValid = point.lng >= (avgLng - lngThreshold) && point.lng <= (avgLng + lngThreshold);
return isLatValid && isLngValid;
});

return filteredPoints;
}

/**
* 寻找停留点
* @param {*} gpsPoints
* @returns
*/
async function optimize(gpsPoints) {
//祛除噪点
let newGpsPoints = noiseRecognitionFilter(gpsPoints)

if(config.autoOptimize){
//反复渲染轨迹时,为了确保自动优化每次都生效,这里需要重置自动优化次数
autoOptimizeCount = 0
Expand All @@ -96,7 +132,7 @@ async function optimize(gpsPoints) {
config.stationaryEndPoints = 10
}

return innerOptimize(gpsPoints)
return innerOptimize(newGpsPoints)
}

/**
Expand Down Expand Up @@ -1253,7 +1289,6 @@ function samplePoints(finalPoints) {
return sampledPoints;
}


const GpsPathTransfigure = {
conf:async (newConfig) => {
config = { ...config, ...newConfig };
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpspathtransfigure",
"version": "1.0.5",
"version": "1.0.6",
"description": "Designed specifically for rendering trajectories of GPS points reported by devices. Optimize GPS coordinate trajectory. Calculate stop points, stay time, exercise time, mileage and other information.",
"main": "index.js",
"type": "module",
Expand Down

0 comments on commit b73d65a

Please sign in to comment.