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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more