UNPKG

gdal3.js

Version:

gdal3.js is a port of Gdal applications (**gdal_translate**, **ogr2ogr**, **gdal_rasterize**, **gdalwarp**, **gdaltransform**) to Webassembly. It allows you to convert raster and vector geospatial data to various formats and coordinate systems.

81 lines (74 loc) 3.42 kB
/* 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()}/`), })), }); } }); }