Skip to content

Commit

Permalink
fix a bunch of string vector cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Jun 10, 2024
1 parent 3f01321 commit 014c1fe
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 122 deletions.
8 changes: 5 additions & 3 deletions R/00_read_block.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ vapour_create_options <- function(driver = "GTiff") {
#' file.remove(tfile)
#' }
vapour_create <- function(filename, driver = "GTiff", extent = c(-180, 180, -90, 90),
dimension = c(2048, 1024), projection = "OGC:CRS84", n_bands = 1L, overwrite = FALSE,
dimension = c(2048, 1024), projection = "EPSG:4326", n_bands = 1L, overwrite = FALSE,
datatype = "Float32",
options = vapour_create_options(driver)) {

Expand All @@ -80,8 +80,10 @@ vapour_create <- function(filename, driver = "GTiff", extent = c(-180, 180, -90,
if (!is.character(options)) options <- character()
if (length(options) < 1) options <- character()
if (!nzchar(options[1])) options <- character()
if (is.na(options[1])) options <- character()
vapour_create_cpp(filename, driver, extent, dimension, projection, n_bands, datatype, options)
if (length(options) == 0 || is.na(options[1])) options <- list()
if (length(options) > 0) options <- strsplit(options, "=")
## options must a split list of name/values pairs
vapour_create_cpp(filename, driver, as.numeric(extent), as.integer(dimension), projection, as.integer(n_bands), datatype, options)
}


Expand Down
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ vapour_create_copy_cpp <- function(dsource, dtarget, driver) {
.Call('_vapour_vapour_create_copy_cpp', PACKAGE = 'vapour', dsource, dtarget, driver)
}

vapour_create_cpp <- function(filename, driver, extent, dimension, projection, n_bands, datatype, options) {
.Call('_vapour_vapour_create_cpp', PACKAGE = 'vapour', filename, driver, extent, dimension, projection, n_bands, datatype, options)
vapour_create_cpp <- function(filename, driver, extent, dimension, projection, n_bands, datatype, options_list_pairs) {
.Call('_vapour_vapour_create_cpp', PACKAGE = 'vapour', filename, driver, extent, dimension, projection, n_bands, datatype, options_list_pairs)
}

vapour_read_raster_value_cpp <- function(dsource, col, row, band, band_output_type) {
Expand Down
16 changes: 15 additions & 1 deletion R/vapour_vrt.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = N
if (is.na(sds[1]) || length(sds) > 1L || !is.numeric(sds) || sds < 1) {
stop("'sds' must be a valid name or integer for a GDAL-subdataset (1-based)")
}

## FIXME: we can't do bands atm, also what else does boilerplate checks do that the new C++ in gdalraster doesn't do?
# x <- sds_boilerplate_checks(x, sds)
# if (!is.null(bands)) {
Expand Down Expand Up @@ -139,6 +138,21 @@ vapour_vrt <- function(x, extent = NULL, projection = NULL, sds = 1L, bands = N
overview <- as.integer(overview[1])
if (is.na(overview)) overview <- -1L


#
# CharacterVector dsn,
# NumericVector extent,
# CharacterVector projection,
# IntegerVector sds,
# IntegerVector bands,
# CharacterVector geolocation,
# LogicalVector nomd,
# IntegerVector overview,
# CharacterVector options
#
#


out <- raster_vrt_cpp(x, extent, projection[1L], sds, bands, geolocation, nomd, overview, options)
## scrub any transform, because of #210
if (nzchar(geolocation[1L])) {
Expand Down
3 changes: 2 additions & 1 deletion inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ GTiff
GeoJSON
GeoPackage
GeoTIFF
GeoTransform
Geodatabase
GeometryCollection
Geopackage
Expand All @@ -53,6 +54,7 @@ Hillshade
IJ
ImageServer
Inaki
IntegerVector
JPL
JSON
KML
Expand Down Expand Up @@ -155,7 +157,6 @@ gdalbuildvrt
gdalheaders
gdalio
gdallibrary
gdalmin
gdalraster
gdalwarp
gdalwarpmem
Expand Down
38 changes: 18 additions & 20 deletions inst/include/gdalraster/gdalraster.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ inline CharacterVector gdal_subdataset_1(GDALDataset *poDataset, int i_sds) {
// owned by the object
CharacterVector ret(1);

// owned by the dataset
char **SDS2 = poDataset->GetMetadata("SUBDATASETS");
while (SDS2 && SDS2[sdi] != NULL) {

Expand Down Expand Up @@ -330,6 +331,7 @@ inline CharacterVector gdal_dsn_vrt(CharacterVector dsn, NumericVector extent, C
GDALClose(DS);
}
}

return out;
}

Expand Down Expand Up @@ -413,25 +415,19 @@ inline List gdal_raster_info(CharacterVector dsn, LogicalVector min_max)
Rcpp::DoubleVector trans(6);
for (int ii = 0; ii < 6; ii++) trans[ii] = adfGeoTransform[ii];

char **pfilelist = GDALGetFileList(hDataset);
int fdi = 0;
while (pfilelist && pfilelist[fdi] != NULL) {
fdi++; // count
}
int ilist = fdi;
if (ilist < 1) {
ilist = 1;
}
CharacterVector FileList(ilist);
// might be no files, because image server
if (pfilelist == nullptr) {
FileList[0] = NA_STRING;
} else {
for (int ifile = 0; ifile < fdi; ifile++) {
FileList[ifile] = pfilelist[ifile];



char **filelist = GDALGetFileList(hDataset);
//std::vector <std::string> files;
CharacterVector files(0);
if (filelist != NULL) {
for (size_t i=0; filelist[i] != NULL; i++) {
files.push_back(filelist[i]);
}
}
CSLDestroy(pfilelist);
CSLDestroy( filelist );

GDALRasterBandH hBand;
int nBlockXSize, nBlockYSize;
double adfMinMax[2];
Expand Down Expand Up @@ -491,6 +487,7 @@ inline List gdal_raster_info(CharacterVector dsn, LogicalVector min_max)
#else
oSRS->importFromWkt( (const char**) cwkt);
#endif
CSLDestroy(cwkt);
oSRS->exportToProj4(&stri);
out[6] = Rcpp::CharacterVector::create(stri); //Rcpp::CharacterVector::create(stri);
names[6] = "projstring";
Expand Down Expand Up @@ -524,7 +521,7 @@ inline List gdal_raster_info(CharacterVector dsn, LogicalVector min_max)
out[8] = oviews;
names[8] = "overviews";

out[9] = FileList;
out[9] = files;
names[9] = "filelist";


Expand Down Expand Up @@ -1251,6 +1248,7 @@ inline LogicalVector gdal_has_geolocation(CharacterVector dsn, IntegerVector sds
poDataset = (GDALDataset*)gdalH_open_dsn(dsn[0], sds);

bool has_geol = false;
// owned by the dataset
char **papszGeolocationInfo = poDataset->GetMetadata("GEOLOCATION");
if( papszGeolocationInfo != nullptr ) {
has_geol = true;
Expand All @@ -1271,8 +1269,8 @@ inline List gdal_list_geolocation(CharacterVector dsn, IntegerVector sds) {
GDALDataset* poDataset;
poDataset = (GDALDataset*)gdalH_open_dsn(dsn[0], sds);


auto papszGeolocationInfo = poDataset->GetMetadata("GEOLOCATION");
// owned by the dataset
char **papszGeolocationInfo = poDataset->GetMetadata("GEOLOCATION");

if( papszGeolocationInfo == nullptr ) {
GDALClose(poDataset);
Expand Down
167 changes: 81 additions & 86 deletions inst/include/gdalreadwrite/gdalreadwrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ inline GDALDataType init_datatype(CharacterVector datatype) {
if (datatype[0] == "Int32") {
return GDT_Int32;
}

if (datatype[0] == "Float32") {
return GDT_Float32;
}
Expand Down Expand Up @@ -71,116 +71,109 @@ inline GDALDataType init_datatype(CharacterVector datatype) {
}
#endif

// Rcpp::stop("datatype not suppported %s\n", datatype[0]);
// Rcpp::stop("datatype not suppported %s\n", datatype[0]);
return GDT_Unknown;
}



inline CharacterVector gdal_create(CharacterVector filename, CharacterVector driver,
NumericVector extent, IntegerVector dimension,
inline CharacterVector gdal_create(CharacterVector filename,
CharacterVector driver,
NumericVector extent,
IntegerVector dimension,
CharacterVector projection,
IntegerVector n_bands,
CharacterVector datatype,
CharacterVector options) {
List options_list_pairs) {


// const char *pszFormat;
// pszFormat = (const char *)driver[0];

GDALDataType gdt_type = init_datatype(datatype);
//GDALDataType gdt_type = GDT_Float32;
OGRSpatialReference* oTargetSRS = nullptr;
oTargetSRS = new OGRSpatialReference;
OGRErr target_chk = oTargetSRS->SetFromUserInput((const char*)projection[0]);
if (target_chk != OGRERR_NONE) {
if (oTargetSRS != nullptr) {
delete oTargetSRS;
}
Rcpp::stop("cannot initialize target projection");
}

char **papszOptions = NULL;
if (options.size() > 0) {
for (int i = 0; i < options.size(); i++) {
// do this in R
// if (EQUAL(options[i], "-co") || CSLPartialFindString(options[i], "=") > -1) {
// Rcpp::warning("create options should not include '-co' or '='")
// }
if (!options[i].empty()) {
papszOptions = CSLAddString(papszOptions, (const char*) options[i]);
}

OGRSpatialReference oSRS;
oSRS.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);

if (oSRS.SetFromUserInput(projection[0]) != OGRERR_NONE)
{
Rcpp::warning(
"Failed to process 'projection' definition");

}

char *pszWKT = nullptr;
#if GDAL_VERSION_MAJOR >= 3
const char *optionsWKT[3] = { "MULTILINE=YES", "FORMAT=WKT2", NULL };
OGRErr err = oSRS.exportToWkt(&pszWKT, optionsWKT);
#else
OGRErr err = oSRS.exportToWkt(&pszWKT);
#endif


GDALDriverH hDriver;
hDriver = GDALGetDriverByName(driver[0]);
if( hDriver == nullptr ) {
//delete(poTargetSRS);
Rcpp::stop("failed to get nominated 'driver'");
}




GDALDriver *poDriver;
poDriver = GetGDALDriverManager()->GetDriverByName("VRT");
if( poDriver == NULL ) {
return Rcpp::CharacterVector::create(NA_STRING);
}
// char **papszMetadata;
// papszMetadata = poDriver->GetMetadata();
// if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) ) {
// Rprintf( "Driver %s supports Create() method.\n", (const char *)driver[0]);
// } else {
// Rprintf( "Driver %s does not support Create() method.\n", (const char *)driver[0]);
// return Rcpp::CharacterVector::create(NA_STRING);
// }
GDALDataset *poVrtDS;
poVrtDS = poDriver->Create("", dimension[0], dimension[1], n_bands[0], gdt_type, NULL);
if (poVrtDS == NULL) {
Rprintf( "Failed to Create virtual datase\n");

return Rcpp::CharacterVector::create(NA_STRING);




char **papszOptions = nullptr;
if (options_list_pairs.size() > 0) {
for (int i = 0; i < options_list_pairs.size(); i++) {
CharacterVector options2 = options_list_pairs[i];
if (options2.size() == 2) {
//Rprintf("options: %s %s\n", (char *)options2[0], (char *)options2[1]);
papszOptions = CSLSetNameValue(papszOptions, (char *)options2[0], (char *)options2[1]);
}
}
}

double adfGeoTransform[6] = { extent[0], (extent[1] - extent[0])/ dimension[0], 0,
extent[3], 0, (extent[2] - extent[3])/ dimension[1]};
poVrtDS->SetGeoTransform( adfGeoTransform );
poVrtDS->SetSpatialRef(oTargetSRS);

char **papszMetadata;
GDALDriver *outDriver;
outDriver = GetGDALDriverManager()->GetDriverByName(driver[0]);

papszMetadata = outDriver->GetMetadata();
if( !CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) ) {
if( poVrtDS != NULL )
GDALClose( (GDALDatasetH) poVrtDS );
Rcpp::stop("driver does not support CreateCopy: %s", driver);


GDALDatasetH hDS = nullptr;
hDS = GDALCreate(hDriver, filename[0],
dimension[0], dimension[1], n_bands[0], gdt_type,
papszOptions);


if (hDS == nullptr) {
Rprintf( "Failed to create dataset\n");
if (pszWKT != nullptr) CPLFree(pszWKT);
CSLDestroy(papszOptions);

return Rcpp::CharacterVector::create(NA_STRING);
}

GDALDataset *poDS;
poDS = outDriver->CreateCopy(filename[0], poVrtDS, false, papszOptions, NULL, NULL);
CPLFree(papszOptions);
if( poDS != NULL )
GDALClose( (GDALDatasetH) poDS );
double adfGeoTransform[6] = { extent[0], (extent[1] - extent[0])/ dimension[0], 0,
extent[3], 0, (extent[2] - extent[3])/ dimension[1]};
GDALSetGeoTransform(hDS, adfGeoTransform );
GDALSetProjection(hDS, pszWKT);

if( poVrtDS != NULL )
GDALClose( (GDALDatasetH) poVrtDS );
if (oTargetSRS != nullptr) {
delete oTargetSRS;
}
return Rcpp::CharacterVector::create(filename[0]);
if (pszWKT != nullptr) CPLFree(pszWKT);
CSLDestroy(papszOptions);


if( hDS != nullptr ) GDALClose( hDS );
return filename;
}





inline CharacterVector gdal_create_copy(CharacterVector dsource, CharacterVector dtarget, CharacterVector driver) {

const char *pszFormat;
pszFormat = (const char *)driver[0];
GDALDriver *poDriver;
//char **papszMetadata;
poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);

poDriver = GetGDALDriverManager()->GetDriverByName(driver[0]);

GDALDataset *poSrcDS = (GDALDataset *) GDALOpen( dsource[0], GA_ReadOnly );
if (poSrcDS == NULL ) stop("unable to open raster source for reading: %s", (char *)dsource[0]);

//
GDALDataset *poDstDS;
char **papszOptions = NULL;
char **papszOptions = nullptr;
papszOptions = CSLSetNameValue( papszOptions, "SPARSE_OK", "YES" );
poDstDS = poDriver->CreateCopy( dtarget[0], poSrcDS, FALSE,
papszOptions, NULL, NULL );
Expand All @@ -189,11 +182,13 @@ inline CharacterVector gdal_create_copy(CharacterVector dsource, CharacterVector
if( poDstDS == NULL ) {
GDALClose( (GDALDatasetH) poSrcDS );
Rprintf("unable to open raster source for CreateCopy: %s", (char *)dtarget[0]);
CSLDestroy(papszOptions);
return CharacterVector::create("");
} else {
GDALClose( (GDALDatasetH) poDstDS );
}
GDALClose( (GDALDatasetH) poSrcDS );
}
CSLDestroy(papszOptions);

GDALClose( (GDALDatasetH) poDstDS );



return dtarget;
Expand Down
Loading

0 comments on commit 014c1fe

Please sign in to comment.