-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESRIGridParser.js
376 lines (321 loc) · 10.7 KB
/
ESRIGridParser.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const MathTools = require("./MathTools.js");
const proj4 = require("proj4");
const crs = L.CRS.EPSG3857;
/**
* A stateful ESRI Grid parser. Calculates min and max values for selected polygons in {@link L.ALS.SynthRectangleBaseLayer}. Can parse huge files by chunks.
*/
class ESRIGridParser {
/**
* Constructs a ESRI Grid parser.
*
* @param layer {L.ALS.SynthRectangleBaseLayer} A layer to apply parsed values to.
* @param projectionString {string} Proj4 projection string. If not given, WGS84 assumed.
*/
constructor(layer = undefined, projectionString = "") {
/**
* Layer to apply parsed values to
* @type {L.ALS.SynthRectangleBaseLayer}
*/
this.layer = layer;
if (projectionString === "")
projectionString = "WGS84";
/**
* Proj4 projection object. Projects coordinates from WGS84 to grids projection.
* @type {Object|undefined}
*/
this.projectionFromMerc = proj4("EPSG:3857", projectionString);
this.clearState();
}
/**
* Clears current state and prepares polygons again. Call it before reusing parser.
*/
clearState() {
/**
* Contains parameters
* @property {number|undefined} xllcenter Longitude of a center
* @property {number|undefined} yllcenter Latitude of a center
* @property {number|undefined} xllcorner Longitude of a bottom left corner
* @property {number|undefined} yllcorner Latitude of a bottom left corner
* @property {number} nrows Number of rows
* @property {number} ncols Number of columns
* @property {number} nodata_value A value which represents missing data for a pixel
* @property {number} cellsize Pixel size in degrees
* @type {Object}
*/
this.DEMParams = {};
/**
* Indicates whether parser is currently reading parameter's name
* @type {boolean}
*/
this.readingDEMParamName = true;
/**
* Indicates whether parser already read all parameters
* @type {boolean}
*/
this.allDEMParamsRead = false;
/**
* Indicates whether additional parameters has been calculated
* @type {boolean}
*/
this.DEMParamsCalculated = false;
/**
* Parameter name that currently is being read
* @type {string}
*/
this.param = "";
/**
* Whatever value that currently is being read
* @type {string}
*/
this.value = "";
/**
* Current Y coordinate. Changes when statistics is being calculated.
* @type {number}
*/
this.y = 0;
/**
* Current X coordinate. Changes when statistics is being calculated.
* @type {number}
*/
this.x = 0;
/**
* Statistics for each selected polygon where keys are polygons' names and values are objects with `min` and `max` keys.
* @type {Object}
*/
this.polygonsStats = {};
/**
* Contains polygons' coordinates mapped by polygons' names.
* @type {Object}
*/
this.polygonsCoordinates = {};
this.isPreviousLineBreak = false;
this.isPreviousSpace = false;
if (!this.layer)
return;
// Turn polygons into rectangles beforehand to optimize calculations
this.polygonsCoordinates = ESRIGridParser.getInitialData(this.layer);
}
/**
* Reads a chunk
* @param chunk {string} A chunk to read
*/
readChunk(chunk) {
for (let i = 0; i < chunk.length; i++) {
let symbol = chunk[i].toLowerCase(),
isSpace = (symbol === " "),
isLineBreak = (symbol === "\n" || symbol === "\r");
// Skip multiple spaces and line breaks
let nameMap = [
["isPreviousLineBreak", isLineBreak],
["isPreviousSpace", isSpace],
];
let doSkip = false;
for (let record of nameMap) {
let name = record[0];
if (record[1]) {
doSkip = this[name];
this[name] = true;
} else
this[name] = false;
}
if (doSkip)
continue;
// Read parameters
if (!this.allDEMParamsRead) {
// Read param name until we hit space
if (this.readingDEMParamName) {
if (isSpace) {
if (this.param === "")
continue;
this.readingDEMParamName = false;
}
// Stop reading when we hit minus or digit
else if (symbol === "-" || !isNaN(parseInt(symbol))) {
this.value = symbol;
this.allDEMParamsRead = true;
} else
this.param += symbol;
}
// Read param value
else {
// There might be multiple spaces before the value
if (isSpace)
continue;
// If we hit line break, the value has been read
if (isLineBreak) {
this.DEMParams[this.param.trim()] = parseFloat(this.value); // Trailing \r might have been left, need to trim it. Also, different programs are using different case, so we gotta normalize it.
this.param = "";
this.value = "";
this.readingDEMParamName = true;
} else
this.value += symbol;
}
continue;
}
// Calculate params
if (!this.DEMParamsCalculated) {
// Calculate top left corner coordinates
if (this.DEMParams.xllcenter) { // If center is given
this.DEMParams.topLeft = {
x: this.DEMParams.xllcenter - (this.DEMParams.ncols * this.DEMParams.cellsize) / 2,
y: this.DEMParams.yllcenter + (this.DEMParams.nrows * this.DEMParams.cellsize) / 2,
}
} else { // If bottom left is given
this.DEMParams.topLeft = {
x: this.DEMParams.xllcorner,
y: this.DEMParams.yllcorner + (this.DEMParams.nrows * this.DEMParams.cellsize),
}
}
this.x = this.DEMParams.topLeft.x;
this.y = this.DEMParams.topLeft.y;
if (!this.DEMParams.nodata_value)
this.DEMParams.nodata_value = -9999;
this.DEMParamsCalculated = true;
}
// Read values and calculate statistics
let valueNotEmpty = (this.value !== "");
if ((isSpace || isLineBreak) && valueNotEmpty) {
let pixelValue = parseFloat(this.value.trim()); // Some programs leaves spaces after last symbol. Also, there might be a trailing \r.
if (pixelValue === this.DEMParams.nodata_value) {
this._updateCoordinates(isLineBreak, isSpace);
continue;
}
let point = this.projectionFromMerc.inverse([this.x, this.y]);
for (let name in this.polygonsCoordinates) {
let poly = this.polygonsCoordinates[name];
if (!MathTools[poly.length > 2 ? "isPointInPolygon" : "isPointInRectangle"](point, poly))
continue;
if (!this.polygonsStats[name])
this.polygonsStats[name] = ESRIGridParser.createStatsObject();
let stats = this.polygonsStats[name];
ESRIGridParser.addToStats(pixelValue, stats);
/*new L.CircleMarker(
crs.unproject(L.point(...point)),
{color: `rgb(${pixelValue},${pixelValue},${pixelValue})`, fillOpacity: 1, stroke: false}
).addTo(map);*/
}
} else if (!isSpace && !isLineBreak)
this.value += symbol;
this._updateCoordinates(isLineBreak, isSpace);
}
}
/**
* Clears current value and updates coordinates
* @param isLineBreak {boolean} Indicates whether current symbol is a line break
* @param isSpace {boolean} Indicates whether current symbol is a space
* @private
*/
_updateCoordinates(isLineBreak, isSpace) {
if (!isLineBreak && !isSpace)
return;
this.value = "";
if (isLineBreak) {
this.x = this.DEMParams.topLeft.x;
this.y -= this.DEMParams.cellsize;
} else if (isSpace)
this.x += this.DEMParams.cellsize;
}
/**
* Copies statistics to the polygons and clears state. Call it when you finish reading file.
*
* This method is NOT thread-safe! Call it outside of your WebWorker and pass your layer as an argument.
*
* @param layer {L.ALS.SynthRectangleBaseLayer} If you're not using it in a WebWorker, don't pass anything. Otherwise, pass your layer.
*/
copyStats(layer = undefined) {
let l = this.layer || layer;
if (!l)
throw new Error("Can't copy statistics. If you're using ESRIGridParser in a WebWorker, call this method outside of WebWorker!");
ESRIGridParser.copyStats(l, this.polygonsStats);
this.clearState();
}
/**
* Parses given file
* @param file {File} File to parse
* @param parser {ESRIGridParser} An parser instance such as `new ESRIGridParser(this)`.
* @param fileReader {FileReader} A FileReader instance. Just pass `new FileReader()`.
* @param callback {function} This function will be called when reading will be finished.
*/
static parseFile(file, parser, fileReader = new FileReader(), callback = undefined) {
let offset = 0, size = 1048576;
fileReader.onloadend = (e) => {
parser.readChunk(e.target.result);
offset += size;
this._nextChunk(file, fileReader, size, offset, parser, callback);
};
this._nextChunk(file, fileReader, size, offset, parser, callback);
}
/**
* Constructs a new chunk of a file and passes it to the FileReader
* @param file {File} File to use
* @param fileReader {FileReader} FileReader instance
* @param size {number} Chunk size
* @param offset {number} Current chunk offset
* @param parser {ESRIGridParser} A parser instance
* @param callback {function(Object)} Callback that accepts statistics as an argument.
* @private
*/
static _nextChunk(file, fileReader, size, offset, parser, callback) {
if (offset >= file.size) {
if (callback)
callback(parser.polygonsStats);
if (parser.layer)
parser.copyStats();
return;
}
let blob = file.slice(offset, size + offset);
fileReader.readAsText(blob);
}
/**
* Generates initial parameters for the layer.
* @param layer {L.ALS.SynthPolygonBaseLayer} Layer to copy data from
*/
static getInitialData(layer) {
let polygonsCoordinates = {};
layer.forEachValidPolygon(polygon => {
polygon.tempDemName = L.ALS.Helpers.generateID();
let coords;
if (polygon instanceof L.Rectangle) {
let rect = polygon.getBounds();
coords = [rect.getNorthWest(), rect.getSouthEast()];
} else
coords = polygon.getLatLngs()[0];
let coordsCopy = [];
for (let coord of coords) {
let {x, y} = crs.project(coord);
coordsCopy.push([x, y]);
}
polygonsCoordinates[polygon.tempDemName] = coordsCopy;
});
return polygonsCoordinates;
}
/**
* Copies stats from any ESRIGridParser to a given layer
* @param layer {L.ALS.SynthRectangleBaseLayer} Layer to copy stats to
* @param stats {Object} Stats from any ESRIGridParser
*/
static copyStats(layer, stats) {
layer.forEachValidPolygon(polygon => {
let entry = stats[polygon.tempDemName];
if (!entry)
return;
entry.mean = entry.sum / entry.count;
polygon.widgetable.getWidgetById("minHeight").setValue(entry.min);
polygon.widgetable.getWidgetById("maxHeight").setValue(entry.max);
polygon.widgetable.getWidgetById("meanHeight").setValue(entry.mean);
});
layer.calculateParameters();
}
static createStatsObject() {
return {min: Infinity, max: -Infinity, mean: 0, sum: 0, count: 0}
}
static addToStats(value, stats) {
if (value < stats.min)
stats.min = value;
if (value > stats.max)
stats.max = value;
stats.sum += value;
stats.count++;
}
}
module.exports = ESRIGridParser;