naturalatlas / node-gdal

Node.js bindings for GDAL (Geospatial Data Abstraction Library)
http://naturalatlas.github.io/node-gdal/classes/gdal.html
Apache License 2.0
567 stars 124 forks source link

gdal.open() on Buffer object #80

Open brandonreavis opened 9 years ago

brandonreavis commented 9 years ago

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.

brianreavis commented 9 years ago

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.

jomel commented 5 years ago

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