Projekt

Obecné

Profil

Stáhnout (2.63 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { isMapPointDisplayable, MapPoint, MapPointType, PathVariant } from '../trackingToolUtils'
2
import * as yup from 'yup'
3
import generateUuid from '../../../utils/id/uuidGenerator'
4

    
5
export const exportAsGeoJsonString = (path: PathVariant) => JSON.stringify({
6
    type: 'FeatureCollection',
7
    features: path.filter(item => item.addToPath && isMapPointDisplayable(item)).map((item) => {
8
        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
    id: yup.string().optional(),
33
    name: yup.string().optional(),
34
    allNames: yup.array().of(yup.string()).optional(),
35
    description: yup.string().optional(),
36
    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

    
57
        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
            id: generateUuid(),
65
            idx: feature.properties.idx,
66
            addToPath: true,
67
            catalogItem: {
68
                id: catalogItem.id,
69
                name: catalogItem.name,
70
                description: catalogItem.description,
71
                latitude: catalogItem.latitude,
72
                longitude: catalogItem.longitude,
73
            },
74
            type: MapPointType.GeoJson,
75
        } as MapPoint
76
    })
77
    return path
78
}
79

    
(4-4/5)