node-gdal icon indicating copy to clipboard operation
node-gdal copied to clipboard

gdal.open() on Buffer object

Open brandonreavis opened this issue 10 years ago • 2 comments

This might be taking things a bit too far, but there are cases where being able to do something like this would be nice:

var data = new Buffer('<OGRVRTDataSource>....</OGRVRTDataSource>');
var file = new gdal.VSIMemFile('/vsimem/sample.vrt', data);
var dataset = gdal.open('/vsimem/sample.vrt');

This is can be done with the VSI mem handler as shown here.

It would require keeping track of which buffer is opened with which dataset, so that the file buffer never gets freed until the dataset is closed.

brandonreavis avatar Nov 14 '14 19:11 brandonreavis

Yeah, this would be really nice for manipulation of geojson / other simple formats. Writing to a temp file for GDAL to handle it isn't ideal.

brianreavis avatar Jul 27 '15 17:07 brianreavis

FYI : you can pass the VRT spec as a string directly into gdal.open() :

let vrt = `<OGRVRTDataSource>
   <OGRVRTLayer name="${filenameWithoutExtension}">
     <SrcDataSource>${file.fullPath}</SrcDataSource>
     <GeometryType>wkbPoint</GeometryType>
     <LayerSRS>EPSG:27700</LayerSRS>
     <GeometryField encoding="PointFromColumns" x="${xCoordName}" y="${yCoordName}"/>
   </OGRVRTLayer>
 </OGRVRTDataSource>`;

   const source_dataset = gdal.open(buildVRT(file));

   console.log(source_dataset.layers);
   if (source_dataset.layers.count() > 1)
      throw new Error(`there are ${source_dataset.layers.count()} layers in the file`);
   const layer = source_dataset.layers.get(0);

   return layer.features; // cursor

jomel avatar Apr 15 '19 10:04 jomel