javascriptnode.jsgdalogr2ogr

Converting geoJson to shapfile as batch using GDAL ogr2ogr in Node JS


I want to convert all the geojsons in a file to shapefiles in a single batch using ogr2ogr library in NodeJS. I know it can be done directly from the terminal using the gdal cli, but I want do know how to do it in Node. Thanks alot!

I have this code to convert a single file:

// Using CommonJS modules
const ogr2ogr = require('ogr2ogr').default;
const fs = require('fs') ;

// Promise API
(async() => {   

  // Convert path to GeoJSON.
  let {data} = await ogr2ogr('/Users/MihaiB/Desktop/test_josm/test22.geojson' );
  console.log(data);  
 
  let {stream} = await ogr2ogr( data, {format: 'ESRI Shapefile'}, {destination:'/Users/MihaiB/Desktop/shapefile2.shx'});
    console.log(stream)       
  
})()

Solution

  • After all, the solution seemed to be pretty easy (even if it took way to long to figure it out) I had to use dirTree package to get the paths for all the files in a given folder

    const ogr2ogr = require('ogr2ogr').default;
    const PATH = require('path');
    const dirTree = require('directory-tree');
    
      // Promise API
    ( async() => {
      
      // List Files in a Directory using dirTree package
      const tree = dirTree('/Users/MihaiB/Desktop/test_josm/', {extensions:/\.geojson$/}, 
      
      async (item, PATH, stats) => {
      console.log(PATH);
            
      // Convert path to GeoJSON
      let {data} = await ogr2ogr(PATH);
      console.log(data);
    
      // Convert GeoJSON to Shapefile
      let {stream} = await ogr2ogr( data, {format: 'ESRI Shapefile', destination:'/Users/MihaiB/Desktop/batch2/'});
      console.log(stream)
    
     }); 
    })
    ()