SEXP RGDAL_GetMetadata(SEXP sDataset, SEXP tag) { char **papszMetadata; SEXP ans; int i, n, pc=0; GDALDataset *pDataset = getGDALDatasetPtr(sDataset); installErrorHandler(); if (tag == R_NilValue) { papszMetadata = pDataset->GetMetadata( NULL ); } else { papszMetadata = pDataset->GetMetadata(CHAR(STRING_ELT(tag, 0))); } uninstallErrorHandlerAndTriggerError(); if (CSLCount(papszMetadata) == 0) return(R_NilValue); for (n=0; papszMetadata[n] != NULL; n++); PROTECT(ans = NEW_CHARACTER(n)); pc++; for (i=0; i<n; i++) SET_STRING_ELT(ans, i, COPY_TO_USER_STRING(papszMetadata[i])); UNPROTECT(pc); return(ans); }
SEXP RGDAL_CopyDataset(SEXP sxpDataset, SEXP sxpDriver, SEXP sxpStrict, SEXP sxpOpts, SEXP sxpFile) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); const char *filename = asString(sxpFile); if (filename == NULL) error("Invalid filename\n"); GDALDriver *pDriver = getGDALDriverPtr(sxpDriver); GDALDataset *pDatasetCopy = pDriver->CreateCopy(filename, pDataset, asInteger(sxpStrict), NULL, NULL, NULL); if (pDatasetCopy == NULL) error("Dataset copy failed\n"); SEXP sxpHandle = R_MakeExternalPtr((void *) pDatasetCopy, mkChar("GDAL Dataset"), R_NilValue); return(sxpHandle); }
/* changed to return proj4 string 20060212 RSB */ SEXP RGDAL_GetProjectionRef(SEXP sDataset) { OGRSpatialReference oSRS; char *pszSRS_WKT = NULL; SEXP ans; GDALDataset *pDataset = getGDALDatasetPtr(sDataset); installErrorHandler(); pszSRS_WKT = (char*) pDataset->GetProjectionRef(); uninstallErrorHandlerAndTriggerError(); installErrorHandler(); oSRS.importFromWkt( &pszSRS_WKT ); oSRS.exportToProj4( &pszSRS_WKT ); uninstallErrorHandlerAndTriggerError(); PROTECT(ans = NEW_CHARACTER(1)); SET_STRING_ELT(ans, 0, COPY_TO_USER_STRING(pszSRS_WKT)); installErrorHandler(); CPLFree( pszSRS_WKT ); uninstallErrorHandlerAndTriggerError(); UNPROTECT(1); return(ans); }
SEXP RGDAL_GetGeoTransform(SEXP sxpDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); SEXP sxpGeoTrans = allocVector(REALSXP, 6); SEXP ceFail = NEW_LOGICAL(1); LOGICAL_POINTER(ceFail)[0] = FALSE; installErrorHandler(); CPLErr err = pDataset->GetGeoTransform(REAL(sxpGeoTrans)); if (err == CE_Failure) { REAL(sxpGeoTrans)[0] = 0; // x-origin ul REAL(sxpGeoTrans)[1] = 1; // x-resolution (pixel width) REAL(sxpGeoTrans)[2] = 0; // x-oblique REAL(sxpGeoTrans)[3] = (double) pDataset->GetRasterYSize(); // y-origin ul; 091028 REAL(sxpGeoTrans)[4] = 0; // y-oblique REAL(sxpGeoTrans)[5] = -1; // y-resolution (pixel height); 091028 added sign LOGICAL_POINTER(ceFail)[0] = TRUE; } setAttrib(sxpGeoTrans, install("CE_Failure"), ceFail); uninstallErrorHandlerAndTriggerError(); return(sxpGeoTrans); }
SEXP RGDAL_GetAccess(SEXP sxpDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); return(ScalarLogical(pDataset->GetAccess() == GA_ReadOnly)); }
SEXP RGDAL_GetRasterCount(SEXP sDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sDataset); return(ScalarInteger(pDataset->GetRasterCount())); }
SEXP RGDAL_GetAccess(SEXP sxpDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); int res; installErrorHandler(); res = pDataset->GetAccess() == GA_ReadOnly; uninstallErrorHandlerAndTriggerError(); return(ScalarLogical(res)); }
SEXP RGDAL_GetRasterCount(SEXP sDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sDataset); int res; installErrorHandler(); res = pDataset->GetRasterCount(); uninstallErrorHandlerAndTriggerError(); return(ScalarInteger(res)); }
SEXP RGDAL_GetDatasetDriver(SEXP sDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sDataset); GDALDriver *pDriver = pDataset->GetDriver(); SEXP sxpDriver = R_MakeExternalPtr((void *) pDriver, mkChar("GDAL Dataset"), R_NilValue); return(sxpDriver); }
SEXP RGDAL_GetRasterBand(SEXP sDataset, SEXP sBand) { GDALDataset *pDataset = getGDALDatasetPtr(sDataset); int band = asInteger(sBand); GDALRasterBand *pRasterBand = pDataset->GetRasterBand(band); SEXP rpRasterBand = R_MakeExternalPtr((void *) pRasterBand, mkChar("GDAL Raster Band"), R_NilValue); return(rpRasterBand); }
SEXP RGDAL_SetGeoTransform(SEXP sxpDataset, SEXP GeoTransform) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); if (LENGTH(GeoTransform) != 6) error("GeoTransform argument should have length 6"); CPLErr err = pDataset->SetGeoTransform(NUMERIC_POINTER(GeoTransform)); if (err == CE_Failure) warning("Failed to set GeoTransform\n"); return(sxpDataset); }
SEXP RGDAL_GetDatasetDriver(SEXP sDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sDataset); installErrorHandler(); GDALDriver *pDriver = pDataset->GetDriver(); uninstallErrorHandlerAndTriggerError(); SEXP sxpDriver = R_MakeExternalPtr((void *) pDriver, mkChar("GDAL Dataset"), R_NilValue); return(sxpDriver); }
SEXP RGDAL_CopyDataset(SEXP sxpDataset, SEXP sxpDriver, SEXP sxpStrict, SEXP sxpOpts, SEXP sxpFile) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); char **papszCreateOptions = NULL; int i; const char *filename = asString(sxpFile); if (filename == NULL) error("Invalid filename\n"); GDALDriver *pDriver = getGDALDriverPtr(sxpDriver); installErrorHandler(); for (i=0; i < length(sxpOpts); i++) papszCreateOptions = CSLAddString( papszCreateOptions, CHAR(STRING_ELT(sxpOpts, i)) ); uninstallErrorHandlerAndTriggerError(); #ifdef RGDALDEBUG installErrorHandler(); for (i=0; i < CSLCount(papszCreateOptions); i++) Rprintf("option %d: %s\n", i, CSLGetField(papszCreateOptions, i)); uninstallErrorHandlerAndTriggerError(); #endif installErrorHandler(); GDALDataset *pDatasetCopy = pDriver->CreateCopy(filename, pDataset, asInteger(sxpStrict), papszCreateOptions, NULL, NULL); uninstallErrorHandlerAndTriggerError(); if (pDatasetCopy == NULL) error("Dataset copy failed\n"); installErrorHandler(); CSLDestroy(papszCreateOptions); uninstallErrorHandlerAndTriggerError(); SEXP sxpHandle = R_MakeExternalPtr((void *) pDatasetCopy, mkChar("GDAL Dataset"), R_NilValue); return(sxpHandle); }
/* added RSB 20060212 */ SEXP RGDAL_SetProject(SEXP sxpDataset, SEXP proj4string) { OGRSpatialReference oSRS; char *pszSRS_WKT = NULL; GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); oSRS.importFromProj4(CHAR(STRING_ELT(proj4string, 0))); oSRS.exportToWkt( &pszSRS_WKT ); OGRErr err = pDataset->SetProjection(pszSRS_WKT); CPLFree( pszSRS_WKT ); if (err == CE_Failure) warning("Failed to set projection\n"); return(sxpDataset); }
SEXP RGDAL_GetGeoTransform(SEXP sxpDataset) { GDALDataset *pDataset = getGDALDatasetPtr(sxpDataset); SEXP sxpGeoTrans = allocVector(REALSXP, 6); CPLErr err = pDataset->GetGeoTransform(REAL(sxpGeoTrans)); if (err == CE_Failure) { REAL(sxpGeoTrans)[0] = 0; REAL(sxpGeoTrans)[1] = 1; REAL(sxpGeoTrans)[2] = 0; REAL(sxpGeoTrans)[3] = 0; REAL(sxpGeoTrans)[4] = 0; REAL(sxpGeoTrans)[5] = 1; } return(sxpGeoTrans); }