49 min = std::numeric_limits<T>::max();
50 max = std::numeric_limits<T>::min();
51 T nan =
static_cast<T
>(band.
nan);
55 for (
int yBlock = 0; yBlock < yBlocks; yBlock++) {
56 for (
int xBlock = 0; xBlock < xBlocks; xBlock++) {
59 band.
p_band->GetActualBlockSize(xBlock, yBlock, &xValid, &yValid);
63 rasterBandIO(band, p_data, band.
xBlockSize, band.
yBlockSize, xBlock, yBlock, xValid, yValid,
true,
true);
65 for (
int y = 0; y < yValid; y++) {
66 int index = y * width;
67 for (
int x = 0; x < xValid; x++) {
68 T val =
reinterpret_cast<T *
>(p_data)[index];
69 if (val != nan && !std::isnan(val)) {
70 min = std::min(min, val);
71 max = std::max(max, val);
117 std::vector<double>& dbins,
118 std::vector<T>& tbins)
120 dbins.resize(nBins + 1);
124 double step = ((double)max - (double)min) / ((
double)nBins);
125 double cur = (double)min;
126 for (
int i = 0; i <= nBins; i++) {
134 if (type == GDT_Float32 || type == GDT_Float64) {
135 for (
int i = 0; i < nBins; i++) {
136 tbins[i] =
static_cast<T
>(dbins[i]);
141 for (
int i = 1; i < nBins; i++) {
143 tbins[i] = std::ceil(cur);
177 std::vector<T>& binVals,
178 std::vector<int64_t>& counts)
183 T nan =
static_cast<T
>(band.
nan);
186 for (
int yBlock = 0; yBlock < yBlocks; yBlock++) {
187 for (
int xBlock = 0; xBlock < xBlocks; xBlock++) {
191 band.
p_band->GetActualBlockSize(xBlock, yBlock, &xValid, &yValid);
195 rasterBandIO(band, p_data, band.
xBlockSize, band.
yBlockSize, xBlock, yBlock, xValid, yValid,
true,
true);
197 for (
int y = 0; y < yValid; y++) {
198 int index = y * width;
199 for (
int x = 0; x < xValid; x++) {
200 T val =
reinterpret_cast<T *
>(p_data)[index];
202 if (val != nan && !std::isnan(val)) {
203 for (
int i = nBins - 1; i >= 0; i--) {
204 if (binVals[i] <= val) {
236 std::vector<helper::Index>& samples,
237 std::vector<T> binVals,
240 std::vector<int64_t> retval(nBins, 0);
244 band.
p_band->RasterIO(GF_Read, index.x, index.y, 1, 1, &val, 1, 1, band.
type, 0, 0);
245 for (
int i = nBins - 1; i >= 0; i--) {
246 if (binVals[i] <= val) {
281 std::vector<helper::Index>& sampled,
285 std::unordered_map<std::string, std::pair<std::vector<double>, std::vector<int64_t>>>& retval)
296 std::vector<double> dbins;
297 std::vector<T> tbins;
301 std::vector<int64_t> counts(nBins, 0);
305 retval.insert({std::string(
"population"), {dbins, counts}});
308 if (sampled.size() != 0) {
310 retval.insert({std::string(
"sample"), {dbins, sampleCounts}});
344 std::mutex datasetMutex;
347 GDALInvGeoTransform(GT, IGT);
350 std::vector<helper::Index> sampled;
352 OGRLayer *p_layer = p_vector->
getDataset()->GetLayerByName(layer.c_str());
355 OGRSpatialReference rastSRS;
356 rastSRS.importFromWkt(p_raster->
getDataset()->GetProjectionRef());
357 const OGRSpatialReference *p_sampSRS = p_layer->GetSpatialRef();
358 if (!rastSRS.IsSame(p_sampSRS)) {
359 throw std::runtime_error(
"existing sample vector and raster do not have the same spatial reference system.");
362 for (
const auto& p_feature : *p_layer) {
363 OGRGeometry *p_geometry = p_feature->GetGeometryRef();
364 switch (wkbFlatten(p_geometry->getGeometryType())) {
365 case OGRwkbGeometryType::wkbPoint: {
366 OGRPoint *p_point = p_geometry->toPoint();
367 double xCoord = p_point->getX();
368 double yCoord = p_point->getY();
370 static_cast<int>(IGT[0] + xCoord * IGT[1] + yCoord * IGT[2]),
371 static_cast<int>(IGT[3] + xCoord * IGT[4] + yCoord * IGT[5])
375 case OGRwkbGeometryType::wkbMultiPoint: {
376 for (
const auto& p_point : *p_geometry->toMultiPoint()) {
377 double xCoord = p_point->getX();
378 double yCoord = p_point->getY();
380 static_cast<int>(IGT[0] + xCoord * IGT[1] + yCoord * IGT[2]),
381 static_cast<int>(IGT[3] + xCoord * IGT[4] + yCoord * IGT[5])
387 throw std::runtime_error(
"encountered a geometry which was not a Point MultiPoint.");
392 std::mutex bandMutex;
399 band.
nan = band.
p_band->GetNoDataValue();
406 std::unordered_map<std::string, std::pair<std::vector<double>, std::vector<int64_t>>> retval;
430 throw std::runtime_error(
"raster pixel data type not supported.");