Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Front Optimization for Keep indoor data #709

Merged
merged 6 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Yi hong",
"version": "1.0.0",
"author": "Yi Hong <zouzou0208@gmail.com>",
"packageManager": "pnpm@8.9.0+sha256.8f5264ad1d100da11a6add6bb8a94c6f1e913f9e9261b2a551fabefad2ec0fec",
"dependencies": {
"@mapbox/mapbox-gl-language": "^1.0.0",
"@mapbox/polyline": "^1.1.1",
Expand All @@ -29,7 +30,7 @@
"private": true,
"type": "module",
"scripts": {
"data:clean": "rm run_page/data.db {GPX,TCX,FIT}_OUT/* activities/* src/static/activities.json",
"data:clean": "rm -f run_page/data.db {GPX,TCX,FIT}_OUT/* activities/* src/static/activities.json",
"data:download:garmin": "python3 run_page/garmin_sync.py",
"data:analysis": "python3 run_page/gen_svg.py --from-db --type github --output assets/github.svg",
"build": "vite build",
Expand Down
2 changes: 1 addition & 1 deletion run_page/keep_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def parse_raw_data_to_nametuple(
if run_data["heartRate"]:
avg_heart_rate = run_data["heartRate"].get("averageHeartRate", None)
heart_rate_data = run_data["heartRate"].get("heartRates", None)
if heart_rate_data is not None:
if heart_rate_data:
decoded_hr_data = decode_runmap_data(heart_rate_data)
# fix #66
if avg_heart_rate and avg_heart_rate < 0:
Expand Down
48 changes: 40 additions & 8 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ const titleForShow = (run: Activity): string => {
if (run.name) {
name = run.name;
}
return `${name} ${date} ${distance} KM ${
!run.summary_polyline ? '(No map data for this run)' : ''
}`;
return `${name} ${date} ${distance} KM ${!run.summary_polyline ? '(No map data for this run)' : ''
}`;
};

const formatPace = (d: number): string => {
Expand Down Expand Up @@ -93,20 +92,39 @@ const extractLocations = (str: string): string[] => {
return locations;
};

const extractCoordinate = (str: string): [number, number] | null => {
const pattern = /'latitude': ([-]?\d+\.\d+).*?'longitude': ([-]?\d+\.\d+)/;
const match = str.match(pattern);

if (match) {
const latitude = parseFloat(match[1]);
const longitude = parseFloat(match[2]);
return [longitude, latitude];
}

return null;
};

const cities = chinaCities.map((c) => c.name);
const locationCache = new Map<number, ReturnType<typeof locationForRun>>();
// what about oversea?
const locationForRun = (
run: Activity
): {
country: string;
province: string;
city: string;
coordinate: [number, number] | null;
} => {
if (locationCache.has(run.run_id)) {
return locationCache.get(run.run_id)!;
}
let location = run.location_country;
let [city, province, country] = ['', '', ''];
let coordinate = null;
if (location) {
// Only for Chinese now
// should fiter 臺灣
// should filter 臺灣
const cityMatch = extractLocations(location);
const provinceMatch = location.match(/[\u4e00-\u9fa5]{2,}(省|自治区)/);

Expand All @@ -119,6 +137,8 @@ const locationForRun = (
}
if (provinceMatch) {
[province] = provinceMatch;
// try to extract city coord from location_country info
coordinate = extractCoordinate(location);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add some comments here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep的导出数据项location_country 中包含city的坐标
假如 存在province,尝试提取这个坐标保存。

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯,可以加在代码里么?因为这个是针对 keep 的所以加个注释好点~

}
const l = location.split(',');
// or to handle keep location format
Expand All @@ -136,7 +156,9 @@ const locationForRun = (
province = city;
}

return { country, province, city };
const r = { country, province, city, coordinate };
locationCache.set(run.run_id, r);
return r;
};

const intComma = (x = '') => {
Expand All @@ -158,6 +180,13 @@ const pathForRun = (run: Activity): Coordinate[] => {
? [arr[1], arr[0]]
: gcoord.transform([arr[1], arr[0]], gcoord.GCJ02, gcoord.WGS84);
});
// try to use location city coordinate instead , if runpath is incomplete
if (c.length === 2 && String(c[0]) === String(c[1])) {
const { coordinate } = locationForRun(run);
if (coordinate?.[0] && coordinate?.[1]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

解析runpath,如果仅有两个相同的坐标点(没有路径,只是一个坐标点),尝试替换为 location中记录的 city坐标。
ps: keep中室内跑步数据提取到的runpath 是北京或国家坐标点(可能是无坐标时的默认值)。 location 记录的是 更具体的城市坐标(如果有)。

如果更严谨的话,需要分析替换前后坐标,哪个更具体使用哪个。

return [coordinate, coordinate];
}
}
return c;
} catch (err) {
return [];
Expand All @@ -183,9 +212,9 @@ const geoJsonForRuns = (runs: Activity[]): FeatureCollection<LineString> => ({
});

const geoJsonForMap = (): FeatureCollection<RPGeometry> => ({
type: 'FeatureCollection',
features: worldGeoJson.features.concat(chinaGeojson.features),
})
type: 'FeatureCollection',
features: worldGeoJson.features.concat(chinaGeojson.features),
})

const titleForRun = (run: Activity): string => {
const runDistance = run.distance / 1000;
Expand Down Expand Up @@ -232,6 +261,9 @@ const getBoundsForGeoData = (
if (points.length === 0) {
return { longitude: 20, latitude: 20, zoom: 3 };
}
if (points.length === 2 && String(points[0]) === String(points[1])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调整路径只有一个点时的缩放级别,大概为市区级。

return { longitude: points[0][0], latitude: points[0][1], zoom: 9 };
}
// Calculate corner values of bounds
const pointsLong = points.map((point) => point[0]) as number[];
const pointsLat = points.map((point) => point[1]) as number[];
Expand Down