Send local image to R function using opencpu

155 Views Asked by At

I am trying to use a local .png file as a function argument in this javascript function to pass it to OpenCPU:

   function processWithR(promObj) {

    return new Promise((resolve, reject) => {
        try {

            var url = "https://public.opencpu.org/ocpu/github/amun-software/Processingservice/R/NDVI_Result";
            request({
                method: "POST",
                uri: url,
                formData: {
                    x:fs.createReadStream('./temp/red/' + promObj.z + '-' + promObj.x + '-' + promObj.y + '.png'),
                    y:fs.createReadStream('./temp/blue/' + promObj.z + '-' + promObj.x + '-' + promObj.y + '.png')
                },
            }, function (err, response, data) {
                err = err || (response && (response.statusCode === 400 ||
                    response.statusCode === 502 ||
                    response.statusCode === 503) && response.statusCode);
                if (!err) {
                    console.log(body);
                }
            });
            console.log("2. Promise  Processing with R");

            fileSave(promObj);
            resolve(promObj)
        } catch (error) {
            reject(error)
        }
    })

}

This function gets me the statusCode Error 400.

The formData part of the request just reads in two local .png files. When I try this at the OpenCPU test page, it works by uploading the two files. I just can't figure out how to pass the files as arguments properly to the request.

UPDATE

I found out that the request itself was ok. I get the error from OpenCPU:

file.copy(x$tmp_name, basename(x$name)) is not TRUE

I tried to encode the form data in base64 but Then i get this error:

In call:
.rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",
...)

Now, the data are being sent to R, but because of the encoding, R can not create a RasterLayer from this. How could I solve this?

1

There are 1 best solutions below

0
On

I found out was my mistake was. Both files were named the same, thats why this error occured. Hope this will help someone else.