SEXP RGDAL_GetRAT(SEXP sxpRasterBand) { SEXP ans, GFT_type, GFT_usage, nc_names; int nc, nr, i, j, ival, np=0; double val; GDALRATFieldType *nc_types; GDALRATFieldUsage *nc_usages; const char *GFU_type_string[] = {"GFT_Integer", "GFT_Real", "GFT_String"}; const char *GFU_usage_string[] = {"GFU_Generic", "GFU_PixelCount", "GFU_Name", "GFU_Min", "GFU_Max", "GFU_MinMax", "GFU_Red", "GFU_Green", "GFU_Blue", "GFU_Alpha", "GFU_RedMin", "GFU_GreenMin", "GFU_BlueMin", "GFU_AlphaMin", "GFU_RedMax", "GFU_GreenMax", "GFU_BlueMax", "GFU_AlphaMax", "GFU_MaxCount"}; GDALRasterBand *pRasterBand = getGDALRasterPtr(sxpRasterBand); installErrorHandler(); const GDALRasterAttributeTable *pRAT = pRasterBand->GetDefaultRAT(); uninstallErrorHandlerAndTriggerError(); if (pRAT == NULL) return(R_NilValue); installErrorHandler(); nc = (int) pRAT->GetColumnCount(); uninstallErrorHandlerAndTriggerError(); PROTECT(ans = NEW_LIST(nc));np++; PROTECT(nc_names = NEW_CHARACTER(nc));np++; nc_types = (GDALRATFieldType *) R_alloc((size_t) nc, sizeof(GDALRATFieldType)); nc_usages = (GDALRATFieldUsage *) R_alloc((size_t) nc, sizeof(GDALRATFieldUsage)); installErrorHandler(); nr = (int) pRAT->GetRowCount(); uninstallErrorHandlerAndTriggerError(); installErrorHandler(); for (i=0; i<nc; i++) { nc_types[i] = pRAT->GetTypeOfCol(i); nc_usages[i] = pRAT->GetUsageOfCol(i); SET_STRING_ELT(nc_names, i, COPY_TO_USER_STRING(pRAT->GetNameOfCol(i))); if (nc_types[i] == GFT_Integer) { SET_VECTOR_ELT(ans, i, NEW_INTEGER(nr)); } else if (nc_types[i] == GFT_Real) { SET_VECTOR_ELT(ans, i, NEW_NUMERIC(nr)); } else if (nc_types[i] == GFT_String) { SET_VECTOR_ELT(ans, i, NEW_CHARACTER(nr)); } else { error("unknown column type"); } } uninstallErrorHandlerAndTriggerError(); installErrorHandler(); for (i=0; i<nc; i++) { if (nc_types[i] == GFT_Integer) { for (j=0; j<nr; j++) { ival = (int) pRAT->GetValueAsInt(j, i); INTEGER_POINTER(VECTOR_ELT(ans, i))[j] = ival; } } else if (nc_types[i] == GFT_Real) { for (j=0; j<nr; j++) { val = (double) pRAT->GetValueAsDouble(j, i); NUMERIC_POINTER(VECTOR_ELT(ans, i))[j] = val; } } else if (nc_types[i] == GFT_String) { for (j=0; j<nr; j++) { SET_STRING_ELT(VECTOR_ELT(ans, i), j, COPY_TO_USER_STRING(pRAT->GetValueAsString(j, i))); } } } uninstallErrorHandlerAndTriggerError(); PROTECT(GFT_type = NEW_CHARACTER(nc));np++; PROTECT(GFT_usage = NEW_CHARACTER(nc));np++; for (i=0; i<nc; i++) { SET_STRING_ELT(GFT_type, i, COPY_TO_USER_STRING(GFU_type_string[nc_types[i]])); SET_STRING_ELT(GFT_usage, i, COPY_TO_USER_STRING(GFU_usage_string[nc_usages[i]])); } setAttrib(ans, install("GFT_type"), GFT_type); setAttrib(ans, install("GFT_usage"), GFT_usage); setAttrib(ans, R_NamesSymbol, nc_names); UNPROTECT(np); return(ans); }
CPLErr GDALPamDataset::TryLoadAux() { /* -------------------------------------------------------------------- */ /* Initialize PAM. */ /* -------------------------------------------------------------------- */ PamInitialize(); if( psPam == NULL ) return CE_None; /* -------------------------------------------------------------------- */ /* What is the name of the physical file we are referencing? */ /* We allow an override via the psPam->pszPhysicalFile item. */ /* -------------------------------------------------------------------- */ const char *pszPhysicalFile = psPam->osPhysicalFilename; if( strlen(pszPhysicalFile) == 0 && GetDescription() != NULL ) pszPhysicalFile = GetDescription(); if( strlen(pszPhysicalFile) == 0 ) return CE_None; /* -------------------------------------------------------------------- */ /* Try to open .aux file. */ /* -------------------------------------------------------------------- */ GDALDataset *poAuxDS = GDALFindAssociatedAuxFile( pszPhysicalFile, GA_ReadOnly, this ); if( poAuxDS == NULL ) return CE_None; /* -------------------------------------------------------------------- */ /* Do we have an SRS on the aux file? */ /* -------------------------------------------------------------------- */ if( strlen(poAuxDS->GetProjectionRef()) > 0 ) GDALPamDataset::SetProjection( poAuxDS->GetProjectionRef() ); /* -------------------------------------------------------------------- */ /* Geotransform. */ /* -------------------------------------------------------------------- */ if( poAuxDS->GetGeoTransform( psPam->adfGeoTransform ) == CE_None ) psPam->bHaveGeoTransform = TRUE; /* -------------------------------------------------------------------- */ /* GCPs */ /* -------------------------------------------------------------------- */ if( poAuxDS->GetGCPCount() > 0 ) { psPam->nGCPCount = poAuxDS->GetGCPCount(); psPam->pasGCPList = GDALDuplicateGCPs( psPam->nGCPCount, poAuxDS->GetGCPs() ); } /* -------------------------------------------------------------------- */ /* Apply metadata. We likely ought to be merging this in rather */ /* than overwriting everything that was there. */ /* -------------------------------------------------------------------- */ char **papszMD = poAuxDS->GetMetadata(); if( CSLCount(papszMD) > 0 ) { char **papszMerged = CSLMerge( CSLDuplicate(GetMetadata()), papszMD ); GDALPamDataset::SetMetadata( papszMerged ); CSLDestroy( papszMerged ); } papszMD = poAuxDS->GetMetadata("XFORMS"); if( CSLCount(papszMD) > 0 ) { char **papszMerged = CSLMerge( CSLDuplicate(GetMetadata("XFORMS")), papszMD ); GDALPamDataset::SetMetadata( papszMerged, "XFORMS" ); CSLDestroy( papszMerged ); } /* ==================================================================== */ /* Process bands. */ /* ==================================================================== */ int iBand; for( iBand = 0; iBand < poAuxDS->GetRasterCount(); iBand++ ) { if( iBand >= GetRasterCount() ) break; GDALRasterBand *poAuxBand = poAuxDS->GetRasterBand( iBand+1 ); GDALRasterBand *poBand = GetRasterBand( iBand+1 ); papszMD = poAuxBand->GetMetadata(); if( CSLCount(papszMD) > 0 ) { char **papszMerged = CSLMerge( CSLDuplicate(poBand->GetMetadata()), papszMD ); poBand->SetMetadata( papszMerged ); CSLDestroy( papszMerged ); } if( poAuxBand->GetCategoryNames() != NULL ) poBand->SetCategoryNames( poAuxBand->GetCategoryNames() ); if( poAuxBand->GetColorTable() != NULL && poBand->GetColorTable() == NULL ) poBand->SetColorTable( poAuxBand->GetColorTable() ); // histograms? double dfMin, dfMax; int nBuckets, *panHistogram=NULL; if( poAuxBand->GetDefaultHistogram( &dfMin, &dfMax, &nBuckets, &panHistogram, FALSE, NULL, NULL ) == CE_None ) { poBand->SetDefaultHistogram( dfMin, dfMax, nBuckets, panHistogram ); CPLFree( panHistogram ); } // RAT if( poAuxBand->GetDefaultRAT() != NULL ) poBand->SetDefaultRAT( poAuxBand->GetDefaultRAT() ); // NoData int bSuccess = FALSE; double dfNoDataValue = poAuxBand->GetNoDataValue( &bSuccess ); if( bSuccess ) poBand->SetNoDataValue( dfNoDataValue ); } GDALClose( poAuxDS ); /* -------------------------------------------------------------------- */ /* Mark PAM info as clean. */ /* -------------------------------------------------------------------- */ nPamFlags &= ~GPF_DIRTY; return CE_Failure; }