1
|
import { isMapPointDisplayable, PathVariant } from '../pathUtils'
|
2
|
import * as yup from 'yup'
|
3
|
|
4
|
export const exportAsGeoJsonString = (path: PathVariant) => JSON.stringify({
|
5
|
type: 'FeatureCollection',
|
6
|
features: path.filter(item => item.active && isMapPointDisplayable(item)).map((item) => {
|
7
|
const catalogItem = item.catalogItem
|
8
|
return {
|
9
|
type: 'Feature',
|
10
|
properties: {
|
11
|
catalogItem: {
|
12
|
id: catalogItem.id,
|
13
|
name: catalogItem.name,
|
14
|
allNames: catalogItem.allNames,
|
15
|
description: catalogItem.description,
|
16
|
latitude: catalogItem.latitude,
|
17
|
longitude: catalogItem.longitude,
|
18
|
},
|
19
|
idx: item.idx,
|
20
|
displayable: isMapPointDisplayable(item),
|
21
|
},
|
22
|
geometry: {
|
23
|
type: 'Point',
|
24
|
coordinates: [catalogItem.longitude, catalogItem.latitude],
|
25
|
},
|
26
|
}
|
27
|
}),
|
28
|
})
|
29
|
|
30
|
const catalogItemValidationSchema = yup.object({
|
31
|
id: yup.string().required(),
|
32
|
name: yup.string().required(),
|
33
|
allNames: yup.array().of(yup.string()).required(),
|
34
|
description: yup.string(),
|
35
|
latitude: yup.number().required(),
|
36
|
longitude: yup.number().required(),
|
37
|
})
|
38
|
|
39
|
/**
|
40
|
* Parses a GeoJson string and returns a list of MapPoints
|
41
|
* @param geoJson loaded file
|
42
|
* @returns
|
43
|
*/
|
44
|
export const parseGeoJsonToPathVariant = (geoJson: string) => {
|
45
|
const parsed = JSON.parse(geoJson)
|
46
|
if (parsed.type !== 'FeatureCollection') {
|
47
|
throw new Error('Invalid GeoJson')
|
48
|
}
|
49
|
const features = parsed.features
|
50
|
if (!features) {
|
51
|
throw new Error('Invalid GeoJson provided')
|
52
|
}
|
53
|
const path: PathVariant = features.map((feature: any) => {
|
54
|
const catalogItemDto = feature.properties.catalogItem
|
55
|
|
56
|
if (!catalogItemDto) {
|
57
|
throw new Error('GeoJson file does not have a valid structure')
|
58
|
}
|
59
|
// validate catalog item
|
60
|
const catalogItem = catalogItemValidationSchema.validateSync(catalogItemDto)
|
61
|
|
62
|
return {
|
63
|
idx: feature.properties.idx,
|
64
|
active: true,
|
65
|
catalogItem: {
|
66
|
id: catalogItem.id,
|
67
|
name: catalogItem.name,
|
68
|
description: catalogItem.description,
|
69
|
latitude: catalogItem.latitude,
|
70
|
longitude: catalogItem.longitude,
|
71
|
},
|
72
|
}
|
73
|
})
|
74
|
return path
|
75
|
}
|
76
|
|