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