-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
194 lines (177 loc) · 6.65 KB
/
index.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
'use strict';
/* global Blob */
/* global URL */
import fetch from 'cross-fetch';
import Worker from './worker.js';
import parseData from './parseData.js';
import {unflatten} from './utils.js';
import {fromUrl, fromUrls} from 'geotiff/src/main.js';
import toCanvas from 'georaster-to-canvas';
function urlExists(url) {
try {
return fetch(url, {method: 'HEAD'})
.then(response => response.status === 200);
} catch (error) {
return Promise.resolve(false);
}
}
function getValues(geotiff, options) {
const {left, top, right, bottom, width, height} = options;
// note this.image and this.geotiff both have a readRasters method;
// they are not the same thing. use this.geotiff for experimental version
// that reads from best overview
return geotiff.readRasters({
window: [left, top, right, bottom],
width: width,
height: height,
resampleMethod: 'bilinear',
}).then(rasters => {
/*
The result appears to be an array with a width and height property set.
We only need the values, assuming the user remembers the width and height.
Ex: [[0,27723,...11025,12924], width: 10, height: 10]
*/
return rasters.map(raster => unflatten(raster, {height, width}));
});
};
class GeoRaster {
constructor(data, metadata, debug) {
if (debug) console.log('starting GeoRaster.constructor with', data, metadata);
this._web_worker_is_available = typeof window !== 'undefined' && window.Worker !== 'undefined';
this._blob_is_available = typeof Blob !== 'undefined';
this._url_is_available = typeof URL !== 'undefined';
// check if should convert to buffer
if (typeof data === 'object' && data.constructor && data.constructor.name === 'Buffer' && Buffer.isBuffer(data) === false) {
data = new Buffer(data);
}
if (typeof data === 'string') {
if (debug) console.log('data is a url');
this._data = data;
this._url = data;
this.rasterType = 'geotiff';
this.sourceType = 'url';
} else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(data)) {
// this is node
if (debug) console.log('data is a buffer');
this._data = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
this.rasterType = 'geotiff';
this.sourceType = 'Buffer';
} else if (data instanceof ArrayBuffer) {
// this is browser
this._data = data;
this.rasterType = 'geotiff';
this.sourceType = 'ArrayBuffer';
} else if (Array.isArray(data) && metadata) {
this._data = data;
this.rasterType = 'object';
this._metadata = metadata;
}
if (debug) console.log('this after construction:', this);
}
preinitialize(debug) {
if (debug) console.log('starting preinitialize');
if (this._url) {
// initialize these outside worker to avoid weird worker error
// I don't see how cache option is passed through with fromUrl,
// though constantinius says it should work: https://github.com/geotiffjs/geotiff.js/issues/61
const ovrURL = this._url + '.ovr';
return urlExists(ovrURL).then(ovrExists => {
if (ovrExists) {
return fromUrls(this._url, [ovrURL], {cache: true, forceXHR: false});
} else {
return fromUrl(this._url, {cache: true, forceXHR: false});
}
});
} else {
// no pre-initialization steps required if not using a Cloud Optimized GeoTIFF
return Promise.resolve();
}
}
initialize(debug) {
return this.preinitialize(debug).then(geotiff => {
return new Promise((resolve, reject) => {
if (debug) console.log('starting GeoRaster.initialize');
if (debug) console.log('this', this);
if (this.rasterType === 'object' || this.rasterType === 'geotiff' || this.rasterType === 'tiff') {
if (this._web_worker_is_available) {
const worker = new Worker();
worker.onmessage = (e) => {
if (debug) console.log('main thread received message:', e);
const data = e.data;
for (const key in data) {
this[key] = data[key];
}
if (this._url) {
this._geotiff = geotiff;
this.getValues = function(options) {
return getValues(this._geotiff, options);
};
}
this.toCanvas = function(options) {
return toCanvas(this, options);
};
resolve(this);
};
if (debug) console.log('about to postMessage');
if (this._data instanceof ArrayBuffer) {
worker.postMessage({
data: this._data,
rasterType: this.rasterType,
sourceType: this.sourceType,
metadata: this._metadata,
}, [this._data]);
} else {
worker.postMessage({
data: this._data,
rasterType: this.rasterType,
sourceType: this.sourceType,
metadata: this._metadata,
});
}
} else {
if (debug) console.log('web worker is not available');
parseData({
data: this._data,
rasterType: this.rasterType,
sourceType: this.sourceType,
metadata: this._metadata,
}, debug).then(result => {
if (debug) console.log('result:', result);
if (this._url) {
result._geotiff = geotiff;
result.getValues = function(options) {
return getValues(this._geotiff, options);
};
}
result.toCanvas = function(options) {
return toCanvas(this, options);
};
resolve(result);
}).catch(reject);
}
} else {
reject('couldn\'t find a way to parse');
}
});
});
}
}
const parseGeoraster = (input, metadata, debug) => {
if (debug) console.log('starting parseGeoraster with ', input, metadata);
if (input === undefined) {
const errorMessage = '[Georaster.parseGeoraster] Error. You passed in undefined to parseGeoraster. We can\'t make a raster out of nothing!';
throw Error(errorMessage);
}
return new GeoRaster(input, metadata, debug).initialize(debug);
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = parseGeoraster;
}
/*
The following code allows you to use GeoRaster without requiring
*/
if (typeof window !== 'undefined') {
window['parseGeoraster'] = parseGeoraster;
} else if (typeof self !== 'undefined') {
self['parseGeoraster'] = parseGeoraster; // jshint ignore:line
}