-
Notifications
You must be signed in to change notification settings - Fork 394
/
processFile.js
154 lines (149 loc) · 5.98 KB
/
processFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import every from 'lodash/every';
import get from 'lodash/get';
import includes from 'lodash/includes';
import some from 'lodash/some';
import proj4 from 'proj4';
import { compose, createEventHandler, mapPropsStream } from 'recompose';
import Rx from 'rxjs';
import { FILE_TYPE_ALLOWED } from '../constants';
import {getConfigProp} from "../../../utils/ConfigUtils";
import {parseURN} from "../../../utils/CoordinatesUtils";
import {
MIME_LOOKUPS,
checkShapePrj,
readJson,
readZip,
readDxf,
recognizeExt,
shpToGeoJSON
} from '../../../utils/FileUtils';
import {flattenImportedFeatures} from "../../../utils/LongitudinalProfileUtils";
/**
* Checks if the file is allowed. Returns a promise that does this check.
*/
const checkFileType = (file) => {
return new Promise((resolve, reject) => {
const ext = recognizeExt(file.name);
const type = file.type || MIME_LOOKUPS[ext];
if (includes(FILE_TYPE_ALLOWED, type)) {
resolve(file);
} else {
reject(new Error("FILE_NOT_SUPPORTED"));
}
});
};
/**
* Create a function that return a Promise for reading file. The Promise resolves with an array of (json)
*/
const readFile = (onWarnings) => (file) => {
const ext = recognizeExt(file.name);
const type = file.type || MIME_LOOKUPS[ext];
const projectionDefs = getConfigProp('projectionDefs') || [];
const supportedProjections = (projectionDefs.length && projectionDefs.map(({code}) => code) || []).concat(["EPSG:4326", "EPSG:3857", "EPSG:900913"]);
// [ ] change this to use filterCRSList
if (type === 'application/json') {
return readJson(file).then(f => {
const projection = get(f, 'map.projection') ?? parseURN(get(f, 'crs'));
if (projection) {
const supportedProjection = proj4.defs(projection);
if (supportedProjection) {
return [{...f, "fileName": file.name}];
}
throw new Error("PROJECTION_NOT_SUPPORTED");
}
return [{...f, "fileName": file.name}];
});
}
if (type === 'application/x-zip-compressed' ||
type === 'application/zip') {
return readZip(file).then((buffer) => {
return checkShapePrj(buffer)
.then((warnings) => {
if (warnings.length > 0) {
onWarnings({type: 'warning', filename: file.name, message: 'shapefile.error.missingPrj'});
}
const geoJsonArr = shpToGeoJSON(buffer).map(json => ({ ...json, filename: file.name }));
const areProjectionsPresent = some(geoJsonArr, geoJson => !!get(geoJson, 'map.projection'));
if (areProjectionsPresent) {
const filteredGeoJsonArr = geoJsonArr.filter(item => !!get(item, 'map.projection'));
const areProjectionsValid = every(filteredGeoJsonArr, geoJson => supportedProjections.includes(geoJson.map.projection));
if (areProjectionsValid) {
return geoJsonArr;
}
throw new Error("PROJECTION_NOT_SUPPORTED");
}
return geoJsonArr;
});
});
}
if (type === 'image/x-dxf' || type === 'image/vnd.dxf') {
return readDxf(file)
.then(({entities}) => {
const geoJSON = {
type: "Feature",
geometry: {
type: "LineString",
coordinates: entities[0].vertices.map(entity => {
return [entity.x, entity.y];
})
}
};
if (entities[0].type === "LWPOLYLINE") {
return [{...geoJSON, "fileName": file.name, showProjectionCombobox: true}];
}
throw new Error("GEOMETRY_NOT_SUPPORTED");
});
}
return null;
};
/**
* Enhancers a component to process files on drop event.
* Recognizes map files (JSON format) or vector data in various formats.
* They are converted in JSON as a "files" property.
*/
export default compose(
mapPropsStream(
props$ => {
const { handler: onDrop, stream: drop$ } = createEventHandler();
const { handler: onWarnings, stream: warnings$} = createEventHandler();
return props$.combineLatest(
drop$.switchMap(
files => Rx.Observable.from(files)
.flatMap(checkFileType) // check file types are allowed
.flatMap(readFile(onWarnings)) // read files to convert to json
.map(res => {
return ({
showProjectionCombobox: !!res[0].showProjectionCombobox,
loading: false,
flattenFeatures: flattenImportedFeatures(res),
crs: res[0]?.crs?.properties?.name ?? 'EPSG:3857'
});
})
.catch(error => Rx.Observable.of({error, loading: false}))
.startWith({ loading: true, showProjectionCombobox: false})
)
.startWith({}),
(p1, p2) => ({
...p1,
...p2,
onDrop
})
).combineLatest(
warnings$
.scan((warnings = [], warning) => ([...warnings, warning]), [])
.startWith(undefined),
(p1, warnings) => ({
...p1,
warnings
})
);
}
)
);