-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
222 lines (193 loc) · 5.3 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
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
import urlJoin from 'url-join';
import { fromUrl } from 'geotiff/src/main';
import types from '../../types';
import { startLoading, stopLoading } from '../main';
const {
SCENE_ADD, SCENE_REMOVE, SCENE_CHANGE_BANDS,
SCENE_PIPELINE_ADD_STEP, SCENE_PIPELINE_REMOVE_STEP, SCENE_PIPELINE_INDEX_STEP,
SCENE_PIPELINE_EDIT_STEP, SET_ERROR,
} = types;
const urlToAttribution = {
'https://s3-us-west-2.amazonaws.com/planet-disaster-data/hurricane-harvey/SkySat_Freeport_s03_20170831T162740Z3.tif': 'cc-by-sa, downloaded from https://www.planet.com/disaster/hurricane-harvey-2017-08-28/',
};
export function addScene(url, bands, redBand, greenBand, blueBand, isSingle, hasOvr, isRGB, attribution, pipeline = []) {
return {
type: SCENE_ADD,
sceneId: url,
bands,
redBand,
greenBand,
blueBand,
isSingle,
hasOvr,
isRGB,
attribution: attribution || urlToAttribution[url],
pipeline,
};
}
export function sceneChangeBands(url, newBands) {
return {
type: SCENE_CHANGE_BANDS,
sceneId: url,
newBands,
};
}
export function removeScene(url) {
return {
type: SCENE_REMOVE,
sceneId: url,
};
}
export function setError(message = null) {
return {
type: SET_ERROR,
message,
};
}
const landsat8Pipeline = [
{
operation: 'sigmoidal-contrast',
contrast: 50,
bias: 0.16,
}, {
operation: 'gamma',
bands: 'red',
value: 1.03,
}, {
operation: 'gamma',
bands: 'blue',
value: 0.925,
},
];
export function addSceneFromIndex(url, attribution, pipeline) {
return async (dispatch, getState) => {
const { scenes } = getState();
// remove old scenes
for (const scene of scenes) {
dispatch(removeScene(scene.id));
}
// clear previous error
dispatch(setError());
dispatch(startLoading());
try {
// find out type of data the URL is pointing to
const headerResponse = await fetch(url, { method: 'HEAD' });
if (!headerResponse.ok) {
throw new Error(`Failed to fetch ${url}`);
}
const contentType = headerResponse.headers.get('content-type');
if (contentType === 'text/html') {
const relUrl = url.endsWith('/') ? url : url.substring(0, url.lastIndexOf('/'));
const response = await fetch(url, {});
const content = await response.text();
const doc = (new DOMParser()).parseFromString(content, 'text/html');
const files = Array.from(doc.querySelectorAll('a[href]'))
.map(a => a.getAttribute('href'))
.map(file => urlJoin(relUrl, file));
let usedPipeline = pipeline;
let red = 0;
let green = 0;
let blue = 0;
let bands;
if (files.find(file => /LC0?8.*B[0-9]+.TIF$/.test(file))) {
// landsat case
red = 4;
green = 3;
blue = 2;
bands = new Map(
files
.filter(file => /LC0?8.*B[0-9]+.TIF$/.test(file))
.map(file => [parseInt(/.*B([0-9]+).TIF/.exec(file)[1], 10), file]),
);
usedPipeline = usedPipeline || landsat8Pipeline;
} else {
bands = new Map(
files
.filter(file => /.TIFF?$/gi.test(file))
.map((file, i) => [i, file]),
);
}
const hasOvr = typeof files.find(file => /.TIFF?.OVR$/i.test(file)) !== 'undefined';
dispatch(
addScene(url, bands, red, green, blue, false, hasOvr, false, attribution, usedPipeline)
);
} else if (contentType === 'image/tiff') {
const tiff = await fromUrl(url);
const image = await tiff.getImage();
const samples = image.getSamplesPerPixel();
const bands = new Map();
for (let i = 0; i < samples; ++i) {
bands.set(i, url);
}
let [red, green, blue] = [];
if (samples === 3 || typeof image.fileDirectory.PhotometricInterpretation !== 'undefined') {
red = 0;
green = 1;
blue = 2;
} else {
red = 0;
green = 0;
blue = 0;
}
const isRGB = (
typeof image.fileDirectory.PhotometricInterpretation !== 'undefined'
&& image.getSampleByteSize(0) === 1
);
dispatch(addScene(url, bands, red, green, blue, true, false, isRGB, attribution, pipeline));
}
} catch (error) {
// TODO: set error somewhere to present to user
dispatch(setError(error.toString()));
} finally {
dispatch(stopLoading());
}
};
}
export function addStep(url, operation) {
let values;
switch (operation) {
case 'sigmoidal-contrast':
values = {
contrast: 50,
bias: 0.15,
};
break;
case 'gamma':
values = { value: 1.0 };
break;
default:
values = {};
break;
}
return {
type: SCENE_PIPELINE_ADD_STEP,
sceneId: url,
payload: {
operation,
...values,
},
};
}
export function editStep(url, index, payload) {
return {
type: SCENE_PIPELINE_EDIT_STEP,
sceneId: url,
index,
payload,
};
}
export function indexStep(url, index, newIndex) {
return {
type: SCENE_PIPELINE_INDEX_STEP,
sceneId: url,
index,
newIndex,
};
}
export function removeStep(url, index) {
return {
type: SCENE_PIPELINE_REMOVE_STEP,
sceneId: url,
index,
};
}