Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. - (WarpResultC*)warpImageWithGeoTransformMBTiles:(NSArray<NSNumber*>*)geoTransformArray sourceFile:(NSString*)inFilepath destinationFile:(NSString*)outFilepath
  2. {
  3. char *argv[] = { strdup("gdal_translate"),
  4. strdup("-of"), strdup("MBTILES"),
  5. strdup(inFilepath.UTF8String), strdup(outFilepath.UTF8String),
  6. NULL };
  7.  
  8. int argc = sizeof(argv) / sizeof(char*) - 1;
  9.  
  10. GDALDatasetH hDataset, hOutDS;
  11. int bUsageError;
  12.  
  13. EarlySetConfigOptions(argc, argv);
  14.  
  15. /* ------------------------------------------- */
  16. /* Register standard GDAL drivers. */
  17. /* ------------------------------------------- */
  18. GDALAllRegister();
  19.  
  20. /* -------------------------------------------------------------------- */
  21. /* Set optimal setting for best performance with huge input VRT. */
  22. /* The rationale for 450 is that typical Linux process allow */
  23. /* only 1024 file descriptors per process and we need to keep some */
  24. /* spare for shared libraries, etc. so let's go down to 900. */
  25. /* And some datasets may need 2 file descriptors, so divide by 2 */
  26. /* for security. */
  27. /* -------------------------------------------------------------------- */
  28. if( CPLGetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", NULL) == NULL )
  29. {
  30. CPLSetConfigOption("GDAL_MAX_DATASET_POOL_SIZE", "450");
  31. }
  32.  
  33. GDALTranslateOptionsForBinary* psOptionsForBinary = GDALTranslateOptionsForBinaryNew();
  34. GDALTranslateOptions *psOptions = GDALTranslateOptionsNew(argv + 1, psOptionsForBinary);
  35.  
  36. GDALTranslateOptionsSetProgress(psOptions, GDALTermProgress, NULL);
  37.  
  38. /* -------------------------------------------------------------------- */
  39. /* Attempt to open source file. */
  40. /* -------------------------------------------------------------------- */
  41.  
  42. hDataset = GDALOpenEx( psOptionsForBinary->pszSource, GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR, NULL,
  43. (const char* const* )psOptionsForBinary->papszOpenOptions, NULL );
  44.  
  45. if( hDataset == NULL )
  46. {
  47. GDALDestroyDriverManager();
  48. exit( 1 );
  49. }
  50.  
  51. char *pszSRS_WKT = NULL;
  52. OGRSpatialReference oSRS;
  53. oSRS.SetWellKnownGeogCS("WGS84");
  54. oSRS.exportToWkt( &pszSRS_WKT );
  55. GDALSetProjection(hDataset, pszSRS_WKT);
  56. CPLFree( pszSRS_WKT );
  57.  
  58. // Set the GeoTransform on the source image
  59. double geoTransform[] = { geoTransformArray[0].doubleValue, geoTransformArray[1].doubleValue, geoTransformArray[2].doubleValue, geoTransformArray[3].doubleValue, geoTransformArray[4].doubleValue, geoTransformArray[5].doubleValue };
  60. GDALSetGeoTransform(hDataset, geoTransform);
  61.  
  62. /* -------------------------------------------------------------------- */
  63. /* Handle subdatasets. */
  64. /* -------------------------------------------------------------------- */
  65. if( !psOptionsForBinary->bCopySubDatasets
  66. && CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0
  67. && GDALGetRasterCount(hDataset) == 0 )
  68. {
  69. fprintf( stderr, "Input file contains subdatasets. Please, select one of them for reading.\n" );
  70. GDALClose( hDataset );
  71. GDALDestroyDriverManager();
  72. exit( 1 );
  73. }
  74.  
  75. if( CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0
  76. && psOptionsForBinary->bCopySubDatasets )
  77. {
  78. char **papszSubdatasets = GDALGetMetadata(hDataset,"SUBDATASETS");
  79. char *pszSubDest = (char *) CPLMalloc(strlen(psOptionsForBinary->pszDest)+32);
  80. int i;
  81.  
  82. CPLString osPath = CPLGetPath(psOptionsForBinary->pszDest);
  83. CPLString osBasename = CPLGetBasename(psOptionsForBinary->pszDest);
  84. CPLString osExtension = CPLGetExtension(psOptionsForBinary->pszDest);
  85. CPLString osTemp;
  86.  
  87. const char* pszFormat = NULL;
  88. if ( CSLCount(papszSubdatasets)/2 < 10 )
  89. {
  90. pszFormat = "%s_%d";
  91. }
  92. else if ( CSLCount(papszSubdatasets)/2 < 100 )
  93. {
  94. pszFormat = "%s_%002d";
  95. }
  96. else
  97. {
  98. pszFormat = "%s_%003d";
  99. }
  100.  
  101. const char* pszDest = pszSubDest;
  102.  
  103. for( i = 0; papszSubdatasets[i] != NULL; i += 2 )
  104. {
  105. char* pszSource = CPLStrdup(strstr(papszSubdatasets[i],"=")+1);
  106. osTemp = CPLSPrintf( pszFormat, osBasename.c_str(), i/2 + 1 );
  107. osTemp = CPLFormFilename( osPath, osTemp, osExtension );
  108. strcpy( pszSubDest, osTemp.c_str() );
  109. hDataset = GDALOpenEx( pszSource, GDAL_OF_RASTER, NULL,
  110. (const char* const* )psOptionsForBinary->papszOpenOptions, NULL );
  111. CPLFree(pszSource);
  112. if( !psOptionsForBinary->bQuiet )
  113. printf("Input file size is %d, %d\n", GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset));
  114. hOutDS = GDALTranslate(pszDest, hDataset, psOptions, &bUsageError);
  115. if(bUsageError == TRUE)
  116. printf("Usage error");
  117. if (hOutDS == NULL)
  118. break;
  119. GDALClose(hOutDS);
  120. }
  121.  
  122. GDALClose(hDataset);
  123. GDALTranslateOptionsFree(psOptions);
  124. GDALTranslateOptionsForBinaryFree(psOptionsForBinary);
  125. CPLFree(pszSubDest);
  126.  
  127. GDALDestroyDriverManager();
  128. return 0;
  129.  
  130. }
  131.  
  132. printf("Input file size is %d, %d\n", GDALGetRasterXSize(hDataset), GDALGetRasterYSize(hDataset));
  133.  
  134. hOutDS = GDALTranslate(psOptionsForBinary->pszDest, hDataset, psOptions, &bUsageError);
  135.  
  136. if(bUsageError == TRUE)
  137. printf("Usage error");
  138.  
  139. int overviewList[3] = { 2, 4, 8 };
  140. // WARNING: This doesn't create overviews and store it in MBTiles, it stores in a separate file
  141. CPLErr eErr = GDALBuildOverviews(hDataset, "NEAREST", 3, overviewList, 0, NULL, GDALDummyProgress, NULL);
  142. CPLAssert( eErr == CE_None );
  143.  
  144. /* Close hOutDS before hDataset for the -f VRT case */
  145. GDALClose(hOutDS);
  146. GDALClose(hDataset);
  147. GDALTranslateOptionsFree(psOptions);
  148. GDALTranslateOptionsForBinaryFree(psOptionsForBinary);
  149.  
  150. GDALDestroyDriverManager();
  151.  
  152. WarpResultC* warpResultC = [WarpResultC new];
  153. warpResultC.success = (hOutDS) ? false : true;
  154.  
  155. return warpResultC;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement