@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
60 lines • 2.17 kB
JavaScript
import geobuf from "geobuf";
import Pbf from "pbf";
import isHostedOnLambda from "./isHostedOnLambda.js";
// Seasketch client
/**
* Given geoprocessing function request, fetches the GeoJSON, which can also be sketch JSON
* @param request
* @returns the JSON with geometry type optionally specified by request
*/
export const fetchGeoJSON = async (request) => {
if (request.geometryGeobuf) {
const sketchU8 = new Uint8Array(Buffer.from(request.geometryGeobuf, "base64"));
return geobuf.decode(new Pbf(sketchU8));
}
if (request.geometry) {
return request.geometry;
}
else if (request.geometryUri) {
if (request.geometryUri.startsWith("data:")) {
// data-uri
const data = Buffer.from(request.geometryUri.split(",")[1], "base64");
if (/application\/json/.test(request.geometryUri)) {
// json
return JSON.parse(data.toString());
}
else if (/application\/x-protobuf/.test(request.geometryUri)) {
// protobuf
// TODO: implement geobuf support
throw new Error("application/x-protobuf not yet supported");
}
else {
throw new Error("Unknown mime type in data-uri");
}
}
else {
// fetch geometry from endpoint
if (isHostedOnLambda) {
console.time(`Fetch sketch from ${request.geometryUri}`);
}
const response = await fetch(request.geometryUri,
// only send Authorization header if token is provided
request.token
? {
headers: {
Authorization: request.token,
},
}
: {});
const sketch = await response.json();
if (isHostedOnLambda) {
console.timeEnd(`Fetch sketch from ${request.geometryUri}`);
}
return sketch;
}
}
else {
throw new Error("No geometry or geometryUri present on request");
}
};
//# sourceMappingURL=seasketch.js.map