All files / application ogr2ogr.js

100% Statements 29/29
91.66% Branches 11/12
100% Functions 4/4
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                    696x 696x 696x 696x 3x   696x   696x 696x   696x 696x 696x 696x 696x 696x 693x 678x       696x 696x 696x 696x   696x 696x 696x   696x 3x 3x   693x     792x                
/* eslint-disable no-underscore-dangle */
/* eslint-disable camelcase */
import { GDALFunctions } from '../../allCFunctions';
import { getGdalError } from '../helper/error';
import { getOptions, clearOptions } from '../helper/options';
import { OUTPUTPATH, getRealOutputPath } from '../helper/const';
import { drivers } from '../helper/drivers';
import { getFileListFromDataset } from '../helper/getFileList';
 
/**
    * ogr2ogr function can be used to convert simple features data between file formats.
    * It can also perform various operations during the process,
    * such as spatial or attribute selection, reducing the set of attributes,
    * setting the output coordinate system or even reprojecting the features during translation.
    *
    * {@link https://gdal.org/programs/ogr2ogr.html}
    *
    * @module a/ogr2ogr
    * @async
    * @param {TypeDefs.Dataset} dataset Dataset to be converted.
    * @param {Array} [options] Options ({@link https://gdal.org/programs/ogr2ogr.html#description})
    * @param {string} [outputName] Destination file name without extension.
    * @return {Promise<TypeDefs.FilePath>} "Promise" returns paths of created files.
    * @example
    * const Gdal = await initGdalJs();
    * const dataset = (await Gdal.open('data.mbtiles')).datasets[0];
    * const options = [
    *   '-f', 'GeoJSON',
    *   '-t_srs', 'EPSG:4326'
    * ];
    * const filePath = await Gdal.ogr2ogr(dataset, options);
    *
*/
export default function ogr2ogr(dataset, options = [], outputName = null) {
    return new Promise((resolve, reject) => {
        const optStr = getOptions(options);
        const config = optStr.config;
        Object.entries(config).forEach(([key, value]) => {
            GDALFunctions.CPLSetConfigOption(key, value);
        });
        const translateOptionsPtr = GDALFunctions.GDALVectorTranslateOptionsNew(optStr.ptr, null);
 
        const datasetList = GDALFunctions.Module._malloc(4);
        GDALFunctions.Module.setValue(datasetList, dataset.pointer, '*');
 
        const driverIndex = options.indexOf('-f') + 1;
        let ext = 'unknown';
        if (driverIndex !== 0) {
            const driverName = options[driverIndex];
            const driver = drivers.vector[driverName];
            if (driver) {
                if (driverName === 'MapInfo File' && options.indexOf('FORMAT=MIF') !== -1) ext = 'mif';
                else ext = driver.extension;
            }
        }
 
        const finalOutputName = outputName || dataset.path.split('.', 1)[0];
        const filePath = `${OUTPUTPATH}/${finalOutputName}.${ext}`;
        const datasetPtr = GDALFunctions.GDALVectorTranslate(filePath, null, 1, datasetList, translateOptionsPtr, null);
        const outputFiles = getFileListFromDataset(datasetPtr);
 
        GDALFunctions.GDALVectorTranslateOptionsFree(translateOptionsPtr);
        clearOptions(optStr);
        GDALFunctions.GDALClose(datasetPtr);
 
        if (GDALFunctions.CPLGetLastErrorNo() >= 3) {
            const error = getGdalError();
            reject(error);
        } else {
            resolve({
                local: filePath,
                real: `${getRealOutputPath()}/${finalOutputName}.${ext}`,
                all: outputFiles.map((file) => ({
                    local: file,
                    real: file.replace(`${OUTPUTPATH}/`, `${getRealOutputPath()}/`),
                })),
            });
        }
    });
}