-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce conversion
osmToClimbingRoutes()
- Loading branch information
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
src/components/FeaturePanel/Climbing/contexts/__tests__/osmToClimbingRoutes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
42 changes: 42 additions & 0 deletions
42
src/components/FeaturePanel/Climbing/contexts/osmToClimbingRoutes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
})); | ||
}; |