|
| 1 | +import { createWriteStream } from 'node:fs' |
| 2 | +import { point, feature, featureCollection, Feature, Point, Polygon } from '@turf/helpers' |
| 3 | +import os from 'node:os' |
| 4 | +import { MUUID } from 'uuid-mongodb' |
| 5 | + |
| 6 | +import { connectDB, gracefulExit, getAreaModel } from '../../../index.js' |
| 7 | +import { logger } from '../../../../logger.js' |
| 8 | + |
| 9 | +/** |
| 10 | + * Export leaf areas as Geojson. Leaf areas are crags/boulders that have climbs. |
| 11 | + */ |
| 12 | +async function exportLeafCrags (): Promise<void> { |
| 13 | + const model = getAreaModel() |
| 14 | + |
| 15 | + const stream = createWriteStream('crags.geojson', { encoding: 'utf-8' }) |
| 16 | + |
| 17 | + const features: Array<Feature<Point, { |
| 18 | + name: string |
| 19 | + id: string |
| 20 | + }>> = [] |
| 21 | + |
| 22 | + for await (const doc of model.find({ 'metadata.leaf': true }).lean()) { |
| 23 | + const { metadata, area_name: areaName, pathTokens, ancestors } = doc |
| 24 | + |
| 25 | + const ancestorArray = ancestors.split(',') |
| 26 | + const pointFeature = point(doc.metadata.lnglat.coordinates, { |
| 27 | + id: metadata.area_id.toUUID().toString(), |
| 28 | + name: areaName, |
| 29 | + type: 'crag', |
| 30 | + parent: { |
| 31 | + id: ancestorArray[ancestorArray.length - 2], |
| 32 | + name: pathTokens[doc.pathTokens.length - 2] |
| 33 | + } |
| 34 | + }) |
| 35 | + features.push(pointFeature) |
| 36 | + } |
| 37 | + stream.write(JSON.stringify(featureCollection(features)) + os.EOL) |
| 38 | + stream.close() |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Export crag groups as Geojson. Crag groups are immediate parent of leaf areas (crags/boulders). |
| 43 | + */ |
| 44 | +async function exportCragGroups (): Promise<void> { |
| 45 | + const model = getAreaModel() |
| 46 | + const stream = createWriteStream('crag-groups.geojson', { encoding: 'utf-8' }) |
| 47 | + |
| 48 | + interface CragGroup { |
| 49 | + uuid: MUUID |
| 50 | + name: string |
| 51 | + polygon: Polygon |
| 52 | + childAreaList: Array<{ |
| 53 | + name: string |
| 54 | + uuid: MUUID |
| 55 | + leftRightIndex: number |
| 56 | + }> |
| 57 | + } |
| 58 | + |
| 59 | + const rs: CragGroup[] = await model.aggregate([ |
| 60 | + { $match: { 'metadata.leaf': true } }, |
| 61 | + { |
| 62 | + $lookup: { |
| 63 | + from: 'areas', |
| 64 | + localField: '_id', |
| 65 | + foreignField: 'children', |
| 66 | + as: 'parentCrags' |
| 67 | + } |
| 68 | + }, |
| 69 | + { |
| 70 | + $match: { |
| 71 | + $and: [ |
| 72 | + { parentCrags: { $type: 'array', $ne: [] } } |
| 73 | + ] |
| 74 | + } |
| 75 | + }, |
| 76 | + { |
| 77 | + $unwind: '$parentCrags' |
| 78 | + }, |
| 79 | + { |
| 80 | + $addFields: { |
| 81 | + parentCrags: { |
| 82 | + childId: '$metadata.area_id' |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + { |
| 87 | + $group: { |
| 88 | + _id: { |
| 89 | + uuid: '$parentCrags.metadata.area_id', |
| 90 | + name: '$parentCrags.area_name', |
| 91 | + polygon: '$parentCrags.metadata.polygon' |
| 92 | + }, |
| 93 | + childAreaList: { |
| 94 | + $push: { |
| 95 | + leftRightIndex: '$metadata.leftRightIndex', |
| 96 | + uuid: '$metadata.area_id', |
| 97 | + name: '$area_name' |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + { |
| 103 | + $project: { |
| 104 | + _id: 0, |
| 105 | + uuid: '$_id.uuid', |
| 106 | + name: '$_id.name', |
| 107 | + polygon: '$_id.polygon', |
| 108 | + childAreaList: 1 |
| 109 | + } |
| 110 | + } |
| 111 | + ]) |
| 112 | + |
| 113 | + const features: Array<Feature<Polygon, { |
| 114 | + name: string |
| 115 | + id: string |
| 116 | + }>> = [] |
| 117 | + |
| 118 | + for await (const doc of rs) { |
| 119 | + const polygonFeature = feature(doc.polygon, { |
| 120 | + type: 'crag-group', |
| 121 | + name: doc.name, |
| 122 | + id: doc.uuid.toUUID().toString(), |
| 123 | + children: doc.childAreaList.map(({ uuid, name, leftRightIndex }) => ( |
| 124 | + { id: uuid.toUUID().toString(), name, lr: leftRightIndex })) |
| 125 | + }) |
| 126 | + features.push(polygonFeature) |
| 127 | + } |
| 128 | + |
| 129 | + stream.write(JSON.stringify(featureCollection(features)) + os.EOL) |
| 130 | + stream.close() |
| 131 | +} |
| 132 | + |
| 133 | +async function onDBConnected (): Promise<void> { |
| 134 | + logger.info('Start exporting crag data as Geojson') |
| 135 | + await exportLeafCrags() |
| 136 | + await exportCragGroups() |
| 137 | + await gracefulExit() |
| 138 | +} |
| 139 | + |
| 140 | +void connectDB(onDBConnected) |
0 commit comments