2017-09-29 41 views

回答

1

這裏是C++從Java的處理的GeoTIFF文件字節組在Linux上使用JNI的完整的例子:

#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"); 
} 
1

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') 
+0

我知道如何使它在python,我剛纔問了C++,但我我會找到答案,謝謝 –

相關問題