-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathprocessFiles.jsx
197 lines (190 loc) · 7.83 KB
/
processFiles.jsx
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2018, 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 JSZip from 'jszip';
import { every, get, some } from 'lodash';
import { compose, createEventHandler, mapPropsStream } from 'recompose';
import Rx from 'rxjs';
import { isAnnotation, importJSONToAnnotations } from '../../../../plugins/Annotations/utils/AnnotationsUtils';
import ConfigUtils from '../../../../utils/ConfigUtils';
import {
MIME_LOOKUPS,
checkShapePrj,
gpxToGeoJSON,
kmlToGeoJSON,
readJson,
readKml,
readKmz,
readWMC,
readZip,
recognizeExt,
shpToGeoJSON,
readGeoJson
} from '../../../../utils/FileUtils';
import { geoJSONToLayer } from '../../../../utils/LayersUtils';
const tryUnzip = (file) => {
return readZip(file).then((buffer) => {
var zip = new JSZip();
return zip.loadAsync(buffer);
});
};
/**
* 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 (type === 'application/x-zip-compressed'
|| type === 'application/zip'
|| type === 'application/vnd.google-earth.kml+xml'
|| type === 'application/vnd.google-earth.kmz'
|| type === 'application/gpx+xml'
|| type === 'application/json'
|| type === 'application/vnd.wmc'
|| type === 'application/geo+json') {
resolve(file);
} else {
// Drag and drop of compressed folders doesn't correctly send the zip mime type (windows, also conflicts with installations of WinRar)
// so the application must try to unzip the file to find out the effective file type.
tryUnzip(file).then(() => resolve(file)).catch(() => reject(new Error("FILE_NOT_SUPPORTED")));
}
});
};
/**
* Create a function that return a Promise for reading file. The Promise resolves with an array of (json)
* @param {function} onWarnings callback in case of warnings to report
*/
const readFile = (onWarnings) => (file) => {
const ext = recognizeExt(file.name);
const type = file.type || MIME_LOOKUPS[ext];
const projectionDefs = ConfigUtils.getConfigProp('projectionDefs') || [];
const supportedProjections = (projectionDefs.length && projectionDefs.map(({code}) => code) || []).concat(["EPSG:4326", "EPSG:3857", "EPSG:900913"]);
if (type === 'application/vnd.google-earth.kml+xml') {
return readKml(file).then((xml) => {
return kmlToGeoJSON(xml);
});
}
if (type === 'application/gpx+xml') {
return readKml(file).then((xml) => {
return gpxToGeoJSON(xml, file.name);
});
}
if (type === 'application/vnd.google-earth.kmz') {
return readKmz(file).then((xml) => {
return kmlToGeoJSON(xml);
});
}
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 === 'application/json') {
return readJson(file).then(f => {
const projection = get(f, 'map.projection');
if (projection) {
if (supportedProjections.includes(projection)) {
return [{...f, "fileName": file.name}];
}
throw new Error("PROJECTION_NOT_SUPPORTED");
}
return [{...f, "fileName": file.name}];
});
}
if (type === 'application/geo+json') {
return readGeoJson(file).then(f => {
const projection = get(f, 'geoJSON.map.projection');
if (projection) {
if (supportedProjections.includes(projection)) {
return [{...f.geoJSON, "fileName": file.name}];
}
throw new Error("PROJECTION_NOT_SUPPORTED");
}
return [{...f.geoJSON, "fileName": file.name}];
});
}
if (type === 'application/vnd.wmc') {
return readWMC(file).then((config) => {
return [{...config, "fileName": file.name}];
});
}
return null;
};
const isGeoJSON = json => json && json.features && json.features.length !== 0;
const isMap = json => json && json.version && json.map;
/**
* 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
.reduce((result, jsonObjects) => ({ // divide files by type
layers: (result.layers || [])
.concat(
jsonObjects.filter(json => isGeoJSON(json))
.map(json => (isAnnotation(json) ?
// annotation GeoJSON to layers
{ name: "Annotations", features: importJSONToAnnotations(json), filename: json.filename} :
// other GeoJSON to layers
{...geoJSONToLayer(json), filename: json.filename}))
),
maps: (result.maps || [])
.concat(
jsonObjects.filter(json => isMap(json))
)
}), {})
.map(filesMap => ({
loading: false,
files: filesMap
}))
.catch(error => Rx.Observable.of({error, loading: false}))
.startWith({ loading: true})
)
.startWith({}),
(p1, p2) => ({
...p1,
...p2,
onDrop
})
).combineLatest(
warnings$
.scan((warnings = [], warning) => ([...warnings, warning]), [])
.startWith(undefined),
(p1, warnings) => ({
...p1,
warnings
})
);
}
)
);