217 char **paths =
nullptr;
218 paths = CSLAddString(paths, projDBPath.c_str());
219 OSRSetPROJSearchPaths(paths);
226 GDALDataset *p_dataset = GDALDataset::FromHandle(GDALOpen(filename.c_str(), GA_ReadOnly));
228 throw std::runtime_error(
"file given does not result in a valid dataset, check to ensure file path is accurate.");
231 this->createFromDataset(p_dataset);
274 GDALRasterWrapper(py::buffer buffer, std::vector<double> geotransform, std::string projection, std::vector<double> nanVals, std::vector<std::string> names, std::string projDBPath) {
276 char **paths =
nullptr;
277 paths = CSLAddString(paths, projDBPath.c_str());
278 OSRSetPROJSearchPaths(paths);
281 py::buffer_info info = buffer.request();
285 size_t bandCount, bandSize;
286 if (info.ndim == 3) {
287 bandCount = info.shape[0];
288 height = info.shape[1];
289 width = info.shape[2];
290 bandSize = info.strides[0];
292 else if (info.ndim == 2) {
294 height = info.shape[0];
295 width = info.shape[1];
298 throw std::runtime_error(
"dimension of numpy array must be 2 or 3");
301 if (bandCount != names.size()) {
302 throw std::runtime_error(
"band names array does not have the same number of bands as the py buffer");
308 if (info.format == py::format_descriptor<int8_t>::format()) {
310 size =
sizeof(int8_t);
312 else if (info.format == py::format_descriptor<int16_t>::format()) {
314 size =
sizeof(int16_t);
316 else if (info.format == py::format_descriptor<uint16_t>::format()) {
318 size =
sizeof(uint16_t);
320 else if (info.format == py::format_descriptor<int32_t>::format()) {
322 size =
sizeof(int32_t);
324 else if (info.format == py::format_descriptor<uint32_t>::format()) {
326 size =
sizeof(uint32_t);
328 else if (info.format == py::format_descriptor<float>::format()) {
330 size =
sizeof(float);
332 else if (info.format == py::format_descriptor<double>::format()) {
334 size =
sizeof(double);
337 throw std::runtime_error(
"data type of array must be one of int8, int16, uint16, int32, uint32, float32, or float64.");
342 std::vector<void *> bands(bandCount);
343 for (
size_t i = 0; i < bandCount; i++) {
345 band.
p_buffer = (
void *)((
size_t)info.ptr + (i * bandSize));
348 band.
nan = nanVals[i];
349 band.
name = names[i];
355 this->createFromDataset(p_dataset);
356 this->rasterBandPointers = bands;
357 this->rasterBandRead = std::vector<bool>(bandCount,
true);
358 this->externalRasterData =
true;
373 if (this->rasterBandRead[i] && !this->externalRasterData) {
374 CPLFree(this->rasterBandPointers[i]);
377 if (this->displayRasterBandRead[i]) {
378 CPLFree(this->displayRasterBandPointers[i]);
382 GDALClose(GDALDataset::ToHandle(this->p_dataset.release()));
384 if (this->tempDir !=
"") {
385 std::filesystem::path temp = this->tempDir;
386 std::filesystem::remove_all(temp);
405 if (this->rasterBandRead[i]) {
406 CPLFree(this->rasterBandPointers[i]);
409 if (this->displayRasterBandRead[i]) {
410 CPLFree(this->displayRasterBandPointers[i]);
414 GDALClose(GDALDataset::ToHandle(this->p_dataset.release()));
416 if (this->tempDir !=
"") {
417 std::filesystem::path temp = this->tempDir;
418 std::filesystem::remove_all(temp);
630 if (!display && !this->rasterBandRead[band]) {
631 this->readRasterBand(width, height, band);
636 if (width != this->displayRasterWidth || height != this->displayRasterHeight) {
637 free(this->displayRasterBandPointers[band]);
638 this->displayRasterBandRead[band] =
false;
641 if (this->displayRasterBandRead[band] ==
false) {
642 this->readRasterBand(width, height, band);
647 p_buffer = (!display) ?
648 this->rasterBandPointers[band] :
649 this->displayRasterBandPointers[band];
653 return getBuffer<int8_t>(
sizeof(int8_t), p_buffer, width, height);
655 return getBuffer<uint16_t>(
sizeof(uint16_t), p_buffer, width, height);
657 return getBuffer<int16_t>(
sizeof(int16_t), p_buffer, width, height);
659 return getBuffer<uint32_t>(
sizeof(uint32_t), p_buffer, width, height);
661 return getBuffer<int32_t>(
sizeof(int32_t), p_buffer, width, height);
663 return getBuffer<float>(
sizeof(
float), p_buffer, width, height);
665 return getBuffer<double>(
sizeof(
double), p_buffer, width, height);
667 throw std::runtime_error(
"raster pixel data type not supported.");
761 std::filesystem::path filepath = filename;
762 std::string extension = filepath.extension().string();
764 if (extension !=
".tif") {
765 throw std::runtime_error(
"write only supports .tif files right now");
768 GDALDriver *p_driver = GetGDALDriverManager()->GetDriverByName(
"GTiff");
770 GDALClose(p_driver->CreateCopy(filename.c_str(), this->p_dataset.get(), (
int)
false,
nullptr,
nullptr,
nullptr));