Skip to content

Commit

Permalink
introduce conversion osmToClimbingRoutes()
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Jan 25, 2024
1 parent 3d44ed9 commit 58c2b9a
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { osmToClimbingRoutes } from '../osmToClimbingRoutes';
import { Feature } from '../../../../../services/types';

const path1 = [
{ x: 0.8, y: 0.712, units: 'percentage' },
{ x: 0.783, y: 0.547, units: 'percentage' },
{ x: 0.675, y: 0.387, units: 'percentage' },
{ x: 0.583, y: 0.368, units: 'percentage' },
{ x: 0.546, y: 0.282, units: 'percentage' },
{ x: 0.571, y: 0.27, units: 'percentage' },
{ x: 0.573, y: 0.245, units: 'percentage' },
];
const getPathString = (path) => path.map(({ x, y }) => `${x},${y}`).join('|');

const feature: Feature = {
type: 'Feature',
center: [-73.0630908, 43.9068891],
osmMeta: {
type: 'relation',
id: 14334600,
timestamp: '2023-12-22T17:12:51Z',
version: 4,
changeset: 145414117,
user: '4b3eff',
uid: 14349548,
},
tags: {
climbing: 'crag',
'climbing:grade:yds_class:max': '5.9+',
'climbing:grade:yds_class:min': '5.7',
'climbing:rock': 'quartzite',
'climbing:routes': '7',
'climbing:url:thecrag': 'https://www.thecrag.com/395435010',
name: 'The North Overhangs',
site: 'sport',
sport: 'climbing',
type: 'site',
},
properties: {
class: 'climbing',
subclass: 'climbing',
},
memberFeatures: [
{
type: 'Feature',
center: [-73.0630918, 43.9068925],
osmMeta: {
type: 'way',
id: 1076927500,
timestamp: '2022-07-18T17:03:11Z',
version: 2,
changeset: 123772465,
user: 'Adam Franco',
uid: 27832,
role: '',
},
tags: {
climbing: 'route',
'climbing:grade:yds_class': '5.7',
'climbing:length': "100'",
'climbing:pitches': '1',
'climbing:toprope': 'yes',
'climbing:trad': 'yes',
name: 'Otter',
sport: 'climbing',
},
properties: {
class: 'climbing',
subclass: 'climbing',
},
},
{
type: 'Feature',
center: [-73.0630651, 43.9067905],
osmMeta: {
type: 'way',
id: 1076927501,
timestamp: '2022-07-08T19:29:58Z',
version: 1,
changeset: 123379162,
user: 'Adam Franco',
uid: 27832,
role: '',
},
tags: {
climbing: 'route',
'climbing:grade:yds_class': '5.7',
'climbing:length': "100'",
'climbing:pitches': '1',
'climbing:toprope': 'yes',
'climbing:trad': 'yes',
'climbing:image': `/images/jickovice1.jpg#${getPathString(path1)}`,
name: 'Red Squirrel',
sport: 'climbing',
},
properties: {
class: 'climbing',
subclass: 'climbing',
},
},
],
} as unknown as Feature; // TODO correct types in shelve

const climbingRoutes = [
{
id: 'way/1076927500',
length: "100'",
name: 'Otter',
paths: {},
},
{
id: 'way/1076927501',
length: "100'",
name: 'Red Squirrel',
paths: {
'/images/jickovice1.jpg': [
{ x: 0.8, y: 0.712, units: 'percentage' },
{ x: 0.783, y: 0.547, units: 'percentage' },
{ x: 0.675, y: 0.387, units: 'percentage' },
{ x: 0.583, y: 0.368, units: 'percentage' },
{ x: 0.546, y: 0.282, units: 'percentage' },
{ x: 0.571, y: 0.27, units: 'percentage' },
{ x: 0.573, y: 0.245, units: 'percentage' },
],
},
},
];

test('conversion', () => {
expect(osmToClimbingRoutes(feature)).toEqual(climbingRoutes);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Feature, FeatureTags } from '../../../../services/types';
import { ClimbingRoute, PathPoints } from '../types';
import { getUrlOsmId } from '../../../../services/helpers';

const parsePathString = (pathString?: string): PathPoints =>
pathString
?.split('|')
.map((coords) => coords.split(',', 2))
.map(([x, y]) => ({
x: parseFloat(x),
y: parseFloat(y),
units: 'percentage' as const,
}));
// TODO filter( where x and y are really numbers)

const parseImageTag = (value: string) => {
const [image, pathString] = value?.split('#', 2) ?? [];
return { image, points: parsePathString(pathString) };
};

function getPathsByImage(tags: FeatureTags) {
const { image, points } = parseImageTag(tags['climbing:image']);
// TODO parse all tags starting with `climbing:image*`

if (image) {
return { [image]: points };
}
return {};
}

export const osmToClimbingRoutes = (feature: Feature): Array<ClimbingRoute> => {
const routes = feature.memberFeatures.filter(({ tags }) =>
['route', 'route_bottom'].includes(tags.climbing),
);
return routes.map((route) => ({
id: getUrlOsmId(route.osmMeta),
length: route.tags['climbing:length'],
name: route.tags.name,
description: route.tags.description,
paths: getPathsByImage(route.tags),
}));
};

0 comments on commit 58c2b9a

Please sign in to comment.