static void WriteRightJustified(VSILFILE* fp, double dfValue, int nWidth, int nDecimals = -1) { char szFormat[32]; if (nDecimals >= 0) sprintf(szFormat, "%%.%df", nDecimals); else sprintf(szFormat, "%%g"); char* pszValue = (char*)CPLSPrintf(szFormat, dfValue); char* pszE = strchr(pszValue, 'e'); if (pszE) *pszE = 'E'; if ((int)strlen(pszValue) > nWidth) { sprintf(szFormat, "%%.%dg", nDecimals); pszValue = (char*)CPLSPrintf(szFormat, dfValue); pszE = strchr(pszValue, 'e'); if (pszE) *pszE = 'E'; } CPLString osValue(pszValue); WriteRightJustified(fp, osValue.c_str(), nWidth); }
static void WriteRightJustified(VSILFILE* fp, double dfValue, int nWidth, int nDecimals = -1) { char szFormat[32]; if (nDecimals >= 0) snprintf(szFormat, sizeof(szFormat), "%%.%df", nDecimals); else snprintf(szFormat, sizeof(szFormat), "%%g"); char* pszValue = const_cast<char *>( CPLSPrintf(szFormat, dfValue) ); char* pszE = strchr(pszValue, 'e'); if (pszE) *pszE = 'E'; if( static_cast<int>( strlen(pszValue) ) > nWidth) { snprintf(szFormat, sizeof(szFormat), "%%.%dg", nDecimals); pszValue = const_cast<char *>( CPLSPrintf(szFormat, dfValue) ); pszE = strchr(pszValue, 'e'); if (pszE) *pszE = 'E'; } CPLString osValue(pszValue); WriteRightJustified(fp, osValue.c_str(), nWidth); }
GDALDataset* ZMapDataset::CreateCopy( const char * pszFilename, GDALDataset *poSrcDS, int bStrict, char ** papszOptions, GDALProgressFunc pfnProgress, void * pProgressData ) { /* -------------------------------------------------------------------- */ /* Some some rudimentary checks */ /* -------------------------------------------------------------------- */ int nBands = poSrcDS->GetRasterCount(); if (nBands == 0) { CPLError( CE_Failure, CPLE_NotSupported, "ZMap driver does not support source dataset with zero band.\n"); return NULL; } if (nBands != 1) { CPLError( (bStrict) ? CE_Failure : CE_Warning, CPLE_NotSupported, "ZMap driver only uses the first band of the dataset.\n"); if (bStrict) return NULL; } if( pfnProgress && !pfnProgress( 0.0, NULL, pProgressData ) ) return NULL; /* -------------------------------------------------------------------- */ /* Get source dataset info */ /* -------------------------------------------------------------------- */ int nXSize = poSrcDS->GetRasterXSize(); int nYSize = poSrcDS->GetRasterYSize(); if (nXSize == 1 || nYSize == 1) { return NULL; } double adfGeoTransform[6]; poSrcDS->GetGeoTransform(adfGeoTransform); if (adfGeoTransform[2] != 0 || adfGeoTransform[4] != 0) { CPLError( CE_Failure, CPLE_NotSupported, "ZMap driver does not support CreateCopy() from skewed or rotated dataset.\n"); return NULL; } /* -------------------------------------------------------------------- */ /* Create target file */ /* -------------------------------------------------------------------- */ VSILFILE* fp = VSIFOpenL(pszFilename, "wb"); if (fp == NULL) { CPLError( CE_Failure, CPLE_AppDefined, "Cannot create %s", pszFilename ); return NULL; } int nFieldSize = 20; int nValuesPerLine = 4; int nDecimalCount = 7; int bHasNoDataValue = FALSE; double dfNoDataValue = poSrcDS->GetRasterBand(1)->GetNoDataValue(&bHasNoDataValue); if (!bHasNoDataValue) dfNoDataValue = 1.e30; VSIFPrintfL(fp, "!\n"); VSIFPrintfL(fp, "! Created by GDAL.\n"); VSIFPrintfL(fp, "!\n"); VSIFPrintfL(fp, "@GRID FILE, GRID, %d\n", nValuesPerLine); WriteRightJustified(fp, nFieldSize, 10); VSIFPrintfL(fp, ","); WriteRightJustified(fp, dfNoDataValue, 10); VSIFPrintfL(fp, ","); WriteRightJustified(fp, "", 10); VSIFPrintfL(fp, ","); WriteRightJustified(fp, nDecimalCount, 10); VSIFPrintfL(fp, ","); WriteRightJustified(fp, 1, 10); VSIFPrintfL(fp, "\n"); WriteRightJustified(fp, nYSize, 10); VSIFPrintfL(fp, ","); WriteRightJustified(fp, nXSize, 10); VSIFPrintfL(fp, ","); if (CSLTestBoolean(CPLGetConfigOption("ZMAP_PIXEL_IS_POINT", "FALSE"))) { WriteRightJustified(fp, adfGeoTransform[0] + adfGeoTransform[1] / 2, 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[0] + adfGeoTransform[1] * nXSize - adfGeoTransform[1] / 2, 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[3] + adfGeoTransform[5] * nYSize - adfGeoTransform[5] / 2, 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[3] + adfGeoTransform[5] / 2, 14, 7); } else { WriteRightJustified(fp, adfGeoTransform[0], 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[0] + adfGeoTransform[1] * nXSize, 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[3] + adfGeoTransform[5] * nYSize, 14, 7); VSIFPrintfL(fp, ","); WriteRightJustified(fp, adfGeoTransform[3], 14, 7); } VSIFPrintfL(fp, "\n"); VSIFPrintfL(fp, "0.0, 0.0, 0.0\n"); VSIFPrintfL(fp, "@\n"); /* -------------------------------------------------------------------- */ /* Copy imagery */ /* -------------------------------------------------------------------- */ double* padfLineBuffer = (double*) CPLMalloc(nYSize * sizeof(double)); int i, j; CPLErr eErr = CE_None; for(i=0;i<nXSize && eErr == CE_None;i++) { eErr = poSrcDS->GetRasterBand(1)->RasterIO( GF_Read, i, 0, 1, nYSize, padfLineBuffer, 1, nYSize, GDT_Float64, 0, 0); if (eErr != CE_None) break; int bEOLPrinted = FALSE; for(j=0;j<nYSize;j++) { WriteRightJustified(fp, padfLineBuffer[j], nFieldSize, nDecimalCount); if (((j + 1) % nValuesPerLine) == 0) { bEOLPrinted = TRUE; VSIFPrintfL(fp, "\n"); } else bEOLPrinted = FALSE; } if (!bEOLPrinted) VSIFPrintfL(fp, "\n"); if (!pfnProgress( (j+1) * 1.0 / nYSize, NULL, pProgressData)) { eErr = CE_Failure; break; } } CPLFree(padfLineBuffer); VSIFCloseL(fp); if (eErr != CE_None) return NULL; return (GDALDataset*) GDALOpen(pszFilename, GA_ReadOnly); }
static void WriteRightJustified(VSILFILE* fp, int nValue, int nWidth) { CPLString osValue(CPLSPrintf("%d", nValue)); WriteRightJustified(fp, osValue.c_str(), nWidth); }