forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraster.js
124 lines (112 loc) · 3.46 KB
/
raster.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
import 'isomorphic-fetch';
export default class RasterService {
/**
* Creates an instance of RasterService.
* @param {string} dataset - ID of the dataset
* @param {string} tableName - Name of the table
*/
constructor(dataset, tableName, provider) {
if (!dataset || !tableName || !provider) throw new Error('RasterService needs the dataset ID and the table name');
this.dataset = dataset;
this.tableName = tableName;
this.provider = provider;
}
/**
* Return the names of the bands
* @returns {Promise<string[]>}
*/
getBandNames() {
let query;
if (this.provider === 'gee') {
query = `SELECT st_metadata(rast) from "${this.tableName}"`;
} else if (this.provider === 'cartodb') {
query = `SELECT (st_metadata(st_union(the_raster_webmercator))).* from ${this.tableName}`;
}
return fetch(
`${process.env.WRI_API_URL}/query/${this.dataset}?sql=${query}`,
{ headers: { 'Upgrade-Insecure-Requests': 1 } }
)
.then((response) => {
if (!response.ok) throw new Error('Unable to fetch the band names');
return response.json();
})
.then(({ data }) => {
if (this.provider === 'gee') {
return data[0].bands.map(b => b.id);
} else if (this.provider === 'cartodb') {
return Array.from({ length: data[0].numbands }, (_, i) => `${i + 1}`);
}
throw new Error('Unsupported provider');
});
}
/**
* Return the statistical information of a band
* @param {string} bandName Name of the band
* @returns {Promise<object>}
*/
getBandStatsInfo(bandName) {
return new Promise((resolve, reject) => {
// First we build the query
let query;
if (this.provider === 'gee') {
// If we already have cached the information about all the bands
// we don't fetch it again
if (this.geeBandStatInfo) {
return resolve(this.geeBandStatInfo[bandName]);
}
query = `SELECT ST_SUMMARYSTATS() from '${this.tableName}'`;
} else if (this.provider === 'cartodb') {
query = `select (ST_SummaryStatsAgg(the_raster_webmercator, ${bandName}, True)).* from ${this.tableName}`;
} else {
// We don't support this provider yet
reject();
}
// We now fetch the actual data
return fetch(
`https://api.resourcewatch.org/v1/query/${this.dataset}?sql=${query}`,
{ headers: { 'Upgrade-Insecure-Requests': 1 } }
)
.then((res) => {
if (!res.ok) reject();
return res.json();
})
.then((data) => {
if (this.provider === 'gee') {
// We cache the data because the information of all the
// bands comes at once
this.geeBandStatInfo = data.data[0];
resolve(this.geeBandStatInfo[bandName]);
} else if (this.provider === 'cartodb') {
resolve(data.data[0]);
}
})
.catch(reject);
});
}
/**
* Return the ChartInfo object for a raster chart
* @static
* @param {object} widgetEditor - Store object
* @returns {ChartInfo}
*/
static getChartInfo(widgetEditor) {
const { areaIntersection } = widgetEditor;
return {
chartType: 'bar',
limit: 500,
order: null,
filters: [],
areaIntersection,
x: {
type: null,
name: 'x',
alias: null
},
y: {
type: null,
name: 'y',
alias: null
}
};
}
}