Skip to content

Commit

Permalink
HARMONY-1876: Add shapefile upload on free-text staged harmony query
Browse files Browse the repository at this point in the history
  • Loading branch information
indiejames committed Jan 3, 2025
1 parent 555b0a8 commit 6eb1ab2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
51 changes: 43 additions & 8 deletions services/harmony/app/frontends/free-text-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import { Response, NextFunction } from 'express';
import { BedrockRuntimeClient, InvokeModelCommand } from '@aws-sdk/client-bedrock-runtime';
import fetch from 'node-fetch';
import FormData from 'form-data';
import axios from 'axios';
import { Readable } from 'stream';
import { buffer, Units } from '@turf/turf';
import { v4 as uuid } from 'uuid';
import { CmrQuery, queryCollectionUsingMultipartForm } from '../util/cmr';
Expand Down Expand Up @@ -155,23 +158,55 @@ interface HarmonyJobStatus {
/**
* TODO
*/
async function submitHarmonyRequest(collection, variable, queryParams, token): Promise<HarmonyJobStatus> {
async function submitHarmonyRequest(collection, variable, queryParams, geoJson: string, token): Promise<HarmonyJobStatus> {
queryParams.forceAsync = true;
queryParams.maxResults = 1;
const encodedVariable = encodeURIComponent(variable);
const baseUrl = `http://localhost:3000/${collection}/ogc-api-coverages/1.0.0/collections/${encodedVariable}/coverage/rangeset`;
const querystr = querystring.stringify(queryParams);
const headers = {
..._makeTokenHeader(token),
'Content-Type': 'multipart/form-data'
};

const formData = new FormData();
// const readable = new Readable();
// readable.push(geoJson);
// readable.push(null);
const readableStream = Readable.from(JSON.stringify(geoJson));

// formData.append('shapefile', readableStream );
// formData.append('shapefile', new Blob([geoJson]));
formData.append('shapefile', JSON.stringify(geoJson), {
filename: 'data.geojson',
contentType: 'application/geo+json',
});

console.log(`Making request to ${baseUrl}?${querystr}`);
const response = await fetch(`${baseUrl}?${querystr}`,
{
method: 'GET',
headers,
});
const data = await response.json();

const response = await axios.post(`${baseUrl}?${querystr}`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
...headers
}
});

// const response = await fetch(`${baseUrl}?${querystr}`,
// {
// method: 'POST',
// headers,
// body: formData,
// });
// const response = await axios.post(`${baseUrl}?${querystr}`, formData, {
// headers
// });
// const response = await axios.post(`${baseUrl}?${querystr}`, {
// shapefile: readableStream,
// },
// { ...headers}
// );
// console.log(`RESPONSE ${JSON.stringify(response, null, 2)}`);
const data = await response.data;
console.log(JSON.stringify(data));
// return await response.json();
return data;
Expand Down Expand Up @@ -347,7 +382,7 @@ export async function freeTextQueryPost(
queryParams.format = modelOutput.outputFormat;
}

const harmonyJob = await submitHarmonyRequest(collectionId, variableId, queryParams, req.accessToken);
const harmonyJob = await submitHarmonyRequest(collectionId, variableId, queryParams, geoJson, req.accessToken);
logPerf(now, 'submit harmony request');
const harmonyParams: GeneratedHarmonyRequestParameters = {
propertyOfInterest: modelOutput.propertyOfInterest,
Expand Down
1 change: 1 addition & 0 deletions services/harmony/app/middleware/cmr-granule-locator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ async function cmrGranuleLocatorTurbo(

if (operation.geojson) {
cmrQuery.geojson = operation.geojson;
cmrQuery['simplify-shapefile'] = 'true';
}

// Only perform CMR granule query when needed by the first step
Expand Down

0 comments on commit 6eb1ab2

Please sign in to comment.