Public
Edited
Apr 26, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
paramsMethod1 = ({
g: JSON.stringify(feature),
stat: "mean", // this averages values for all grid cells that intersect the feature
})
Insert cell
dailyDataMethod1 = {
const [response, error] = await handleXHR(fetchDataMethod1(`${url}/events/`, paramsMethod1));
if (error) {
throw new Error(error.message);
}
return transformEventsResponse(response);
}
Insert cell
function fetchDataMethod1(url, params = {}) {
const formData = new FormData();
Object.keys(params).forEach((key) => {
formData.append(key, params[key]);
});
// Returns a promise that will resolve if fetch is successfull
return fetch(url, {
method: "POST",
headers: {
Accept: "application/json",
},
body: formData,
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
paramsMethod2 = ({
stat: "mean", // this averages values for all grid cells that intersect the feature
pagesize: 100, // api response for rasters endpoint is paginated, 100 years should cover all years in the series
})
Insert cell
dailyDataMethod2 = {
const input = document.querySelector('input[name="upload"]');
if (input.files.length === 0) {
throw new Error("Please select a file");
}
const [response, error] = await handleXHR(fetchDataMethod2(`${url}/rasters/`, paramsMethod2, input.form));
if (error) {
throw new Error(error.message);
}
return transformRasterResponse(response);
}
Insert cell
function fetchDataMethod2(url, params, form) {
const formData = new FormData(form);
Object.keys(params).forEach((key) => {
formData.append(key, params[key]);
});

// Returns a promise that will resolve if fetch is successfull
return fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
},
body: formData,
});
}
Insert cell
Insert cell
function getError(json) {
let errorStr = "";
let messageStr = "";
// Error returned from API can be in various configs
if (json.g && json.g[0]) {
[errorStr] = json.g;
} else if (json.upload && json.upload.length > 0) {
[errorStr] = json.upload;
} else if (json.features) {
[errorStr] = json.features;
} else if (json.detail) {
errorStr = json.detail;
}

// Generate user friendly error message
if (errorStr.indexOf("Max area of 5.7 exceeded") > -1) {
messageStr = `Area of uploaded feature is too large. Maximum
area supported is 20,000 sq. miles.`;
} else if (errorStr.indexOf("Upload a valid data source") > -1) {
messageStr = `File is corrupted or not a valid data source`;
} else {
messageStr = errorStr;
}
return messageStr;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more