打开文件的Gdal函数使用filename来读取数据:
GDALOpen (const char *pszFilename, GDALAccess eAccess)
但我的文件位于内存中。我使用下一个代码来创建指向文件的指针:
FILE *stream;
stream = fmemopen(buf, lengthOfArray, "r");
是否可以通过指针文件以某种方式创建GDALDataset?
任何技巧和提示?
答案 0 :(得分:2)
GDAL可以选择从缓冲区创建内存中文件。参见:
http://gdal.org/cpl__vsi_8h.html#a86b6b1c37bb19d954ee3c4a7e910120c
我没有使用C ++的经验,但在Python中它看起来像:
with open('myfile.tif', mode='rb') as f:
gdal.FileFromMemBuffer('/vsimem/some_memfile', f.read())
然后,您可以打开该虚拟位置,就好像它是一个普通文件:
ds = gdal.Open('/vsimem/some_memfile')
当你完成文件后,请确保取消链接,否则它会一直存在。
ds = None
gdal.Unlink('/vsimem/some_memfile')
答案 1 :(得分:1)
以下是在linux上使用jni从java bytearray处理geotiff文件的C ++的完整示例:
#include <jni.h>
#include "gdal_priv.h"
#include "cpl_string.h"
#include "cpl_conv.h"
#include "gdalwarper.h"
#include "cpl_vsi.h"
JNIEXPORT void JNICALL Java_com_box_processing_GEOTransform_run(JNIEnv *env, jobject obj, jbyteArray array) {
GDALAllRegister();
jboolean isCopy;
jbyte* buf = env->GetByteArrayElements(array, &isCopy);
jsize lengthOfArray = env->GetArrayLength(array);
const char *pszFormat = "GTiff";
GDALDriver *poDriver;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
VSILFILE* fpMem = VSIFileFromMemBuffer ("/vsimem/temp.tif", (GByte*) buf, (vsi_l_offset) lengthOfArray, FALSE );
VSIFCloseL(fpMem);
GDALDataset *poSrcDS = (GDALDataset *) GDALOpen( "/vsimem/temp.tif", GA_ReadOnly );
GDALDataset *poDstDS;
const char *pszSrcWKT = NULL;
pszSrcWKT=GDALGetProjectionRef(poSrcDS);
double error_threshold = 0.125;
GDALResampleAlg resampling = GRA_Cubic;
char* pszDstWKT = NULL;
GDALDataset * tmpDS = (GDALDataset*)(GDALDataset*)GDALAutoCreateWarpedVRT(poSrcDS, pszSrcWKT, pszDstWKT, resampling, error_threshold, NULL);
poDstDS = poDriver->CreateCopy( "/some/folder/example1.tif", (GDALDataset*)tmpDS, FALSE, NULL, NULL, NULL );
GDALClose( (GDALDatasetH) poDstDS );
GDALClose( (GDALDatasetH) poSrcDS );
VSIUnlink( "/vsimem/temp.tif" );
}