OmniSciDB  ca0c39ec8f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeoRasterTableFunctions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __CUDACC__
18 
19 #include <cmath>
20 #include <vector>
21 
22 #include <tbb/parallel_for.h>
23 #include <tbb/task_arena.h>
25 #include "Shared/math_consts.h"
26 
27 const size_t max_inputs_per_thread = 1000000L;
28 const size_t max_temp_output_entries = 200000000L;
29 
30 // Allow input types to GeoRaster that are different than class types/output Z type
31 // So we can move everything to the type of T and Z (which can each be either float
32 // or double)
33 template <typename T, typename Z>
34 template <typename T2, typename Z2>
36  const Column<T2>& input_y,
37  const Column<Z2>& input_z,
38  const RasterAggType raster_agg_type,
39  const double bin_dim_meters,
40  const bool geographic_coords,
41  const bool align_bins_to_zero_based_grid)
42  : raster_agg_type_(raster_agg_type)
43  , bin_dim_meters_(bin_dim_meters)
44  , geographic_coords_(geographic_coords)
45  , null_sentinel_(inline_null_value<Z>()) {
46  auto timer = DEBUG_TIMER(__func__);
47  const int64_t input_size{input_z.size()};
48  if (input_size <= 0) {
49  num_bins_ = 0;
50  num_x_bins_ = 0;
51  num_y_bins_ = 0;
52  return;
53  }
54  const auto min_max_x = get_column_min_max(input_x);
55  const auto min_max_y = get_column_min_max(input_y);
56  x_min_ = min_max_x.first;
57  x_max_ = min_max_x.second;
58  y_min_ = min_max_y.first;
59  y_max_ = min_max_y.second;
60 
61  if (align_bins_to_zero_based_grid && !geographic_coords_) {
62  // For implicit, data-defined bounds, we treat the max of the x and y ranges as
63  // inclusive (closed interval), since if the max of the data in either x/y dimensions
64  // is at the first value of the next bin, values at that max will be discarded if we
65  // don't include the final bin. For exmaple, if the input data (perhaps already binned
66  // with a group by query) goes from 0.0 to 40.0 in both x and y directions, we should
67  // have the last x/y bins cover the range [40.0, 50.0), not [30.0, 40.0)
69  }
70 
72  compute(input_x, input_y, input_z, max_inputs_per_thread);
73 }
74 
75 // Allow input types to GeoRaster that are different than class types/output Z type
76 // So we can move everything to the type of T and Z (which can each be either float
77 // or double)
78 template <typename T, typename Z>
79 template <typename T2, typename Z2>
81  const Column<T2>& input_y,
82  const Column<Z2>& input_z,
83  const RasterAggType raster_agg_type,
84  const double bin_dim_meters,
85  const bool geographic_coords,
86  const bool align_bins_to_zero_based_grid,
87  const T x_min,
88  const T x_max,
89  const T y_min,
90  const T y_max)
91  : raster_agg_type_(raster_agg_type)
92  , bin_dim_meters_(bin_dim_meters)
93  , geographic_coords_(geographic_coords)
94  , null_sentinel_(inline_null_value<Z>())
95  , x_min_(x_min)
96  , x_max_(x_max)
97  , y_min_(y_min)
98  , y_max_(y_max) {
99  auto timer = DEBUG_TIMER(__func__);
100  if (align_bins_to_zero_based_grid && !geographic_coords_) {
101  // For explicit, user-defined bounds, we treat the max of the x and y ranges as
102  // exclusive (open interval), since if the user specifies the max x/y as the end of
103  // the bin, they do not intend to add the next full bin For example, if a user
104  // specifies a bin_dim_meters of 10.0 and an x and y range from 0 to 40.0, they almost
105  // assuredly intend for there to be 4 bins in each of the x and y dimensions, with the
106  // last bin of range [30.0, 40.0), not 5 with the final bin's range from [40.0, 50.0)
108  }
110  compute(input_x, input_y, input_z, max_inputs_per_thread);
111 }
112 
113 template <typename T, typename Z>
114 inline Z GeoRaster<T, Z>::offset_source_z_from_raster_z(const int64_t source_x_bin,
115  const int64_t source_y_bin,
116  const Z source_z_offset) const {
117  if (is_bin_out_of_bounds(source_x_bin, source_y_bin)) {
118  return null_sentinel_;
119  }
120  const Z terrain_z = z_[x_y_bin_to_bin_index(source_x_bin, source_y_bin, num_x_bins_)];
121  if (terrain_z == null_sentinel_) {
122  return terrain_z;
123  }
124  return terrain_z + source_z_offset;
125 }
126 
127 template <typename T, typename Z>
129  x_min_ = std::floor(x_min_ / bin_dim_meters_) * bin_dim_meters_;
130  x_max_ = std::floor(x_max_ / bin_dim_meters_) * bin_dim_meters_ +
131  bin_dim_meters_; // Snap to end of bin
132  y_min_ = std::floor(y_min_ / bin_dim_meters_) * bin_dim_meters_;
133  y_max_ = std::floor(y_max_ / bin_dim_meters_) * bin_dim_meters_ +
134  bin_dim_meters_; // Snap to end of bin
135 }
136 
137 template <typename T, typename Z>
139  x_min_ = std::floor(x_min_ / bin_dim_meters_) * bin_dim_meters_;
140  x_max_ = std::ceil(x_max_ / bin_dim_meters_) * bin_dim_meters_;
141  y_min_ = std::floor(y_min_ / bin_dim_meters_) * bin_dim_meters_;
142  y_max_ = std::ceil(y_max_ / bin_dim_meters_) * bin_dim_meters_;
143 }
144 
145 template <typename T, typename Z>
147  x_range_ = x_max_ - x_min_;
148  y_range_ = y_max_ - y_min_;
149  if (geographic_coords_) {
150  const T x_centroid = (x_min_ + x_max_) * 0.5;
151  const T y_centroid = (y_min_ + y_max_) * 0.5;
152  x_meters_per_degree_ =
153  distance_in_meters(x_min_, y_centroid, x_max_, y_centroid) / x_range_;
154 
155  y_meters_per_degree_ =
156  distance_in_meters(x_centroid, y_min_, x_centroid, y_max_) / y_range_;
157 
158  num_x_bins_ = x_range_ * x_meters_per_degree_ / bin_dim_meters_;
159  num_y_bins_ = y_range_ * y_meters_per_degree_ / bin_dim_meters_;
160 
161  x_scale_input_to_bin_ = x_meters_per_degree_ / bin_dim_meters_;
162  y_scale_input_to_bin_ = y_meters_per_degree_ / bin_dim_meters_;
163  x_scale_bin_to_input_ = bin_dim_meters_ / x_meters_per_degree_;
164  y_scale_bin_to_input_ = bin_dim_meters_ / y_meters_per_degree_;
165 
166  } else {
167  num_x_bins_ = x_range_ / bin_dim_meters_;
168  num_y_bins_ = y_range_ / bin_dim_meters_;
169 
170  x_scale_input_to_bin_ = 1.0 / bin_dim_meters_;
171  y_scale_input_to_bin_ = 1.0 / bin_dim_meters_;
172  x_scale_bin_to_input_ = bin_dim_meters_;
173  y_scale_bin_to_input_ = bin_dim_meters_;
174  }
175  num_bins_ = num_x_bins_ * num_y_bins_;
176 }
177 
178 template <typename T, typename Z>
179 template <RasterAggType AggType, typename T2, typename Z2>
181  const Column<T2>& input_y,
182  const Column<Z2>& input_z,
183  std::vector<Z>& output_z) {
184  auto timer = DEBUG_TIMER(__func__);
185  const int64_t input_size{input_z.size()};
186  const Z agg_sentinel = raster_agg_type_ == RasterAggType::MIN
187  ? std::numeric_limits<Z>::max()
188  : std::numeric_limits<Z>::lowest();
189  output_z.resize(num_bins_, agg_sentinel);
190  ComputeAgg<AggType> compute_agg;
191  for (int64_t sparse_idx = 0; sparse_idx != input_size; ++sparse_idx) {
192  const int64_t x_bin = get_x_bin(input_x[sparse_idx]);
193  const int64_t y_bin = get_y_bin(input_y[sparse_idx]);
194  if (x_bin < 0 || x_bin >= num_x_bins_ || y_bin < 0 || y_bin >= num_y_bins_) {
195  continue;
196  }
197  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
198  compute_agg(input_z[sparse_idx], output_z[bin_idx], inline_null_value<Z2>());
199  }
200  for (int64_t bin_idx = 0; bin_idx != num_bins_; ++bin_idx) {
201  if (output_z[bin_idx] == agg_sentinel) {
202  output_z[bin_idx] = null_sentinel_;
203  }
204  }
205 }
206 
207 template <typename T, typename Z>
208 template <RasterAggType AggType>
210  const std::vector<std::vector<Z>>& z_inputs,
211  std::vector<Z>& output_z,
212  const Z agg_sentinel) {
213  const size_t num_inputs = z_inputs.size();
214  output_z.resize(num_bins_, agg_sentinel);
215 
216  ComputeAgg<AggType> reduction_agg;
218  tbb::blocked_range<size_t>(0, num_bins_), [&](const tbb::blocked_range<size_t>& r) {
219  const size_t start_idx = r.begin();
220  const size_t end_idx = r.end();
221  for (size_t bin_idx = start_idx; bin_idx != end_idx; ++bin_idx) {
222  for (size_t input_idx = 0; input_idx < num_inputs; ++input_idx) {
223  reduction_agg(z_inputs[input_idx][bin_idx], output_z[bin_idx], agg_sentinel);
224  }
225  }
226  });
227 
228  tbb::parallel_for(tbb::blocked_range<size_t>(0, num_bins_),
229  [&](const tbb::blocked_range<size_t>& r) {
230  const size_t start_idx = r.begin();
231  const size_t end_idx = r.end();
232  for (size_t bin_idx = start_idx; bin_idx != end_idx; ++bin_idx) {
233  if (output_z[bin_idx] == agg_sentinel) {
234  output_z[bin_idx] = null_sentinel_;
235  }
236  }
237  });
238 }
239 
240 template <typename T, typename Z>
241 template <RasterAggType AggType, typename T2, typename Z2>
243  const Column<T2>& input_y,
244  const Column<Z2>& input_z,
245  std::vector<Z>& output_z,
246  const size_t max_inputs_per_thread) {
247  const size_t input_size = input_z.size();
248  const size_t max_thread_count = std::thread::hardware_concurrency();
249  const size_t num_threads_by_input_elements =
250  std::min(max_thread_count,
251  ((input_size + max_inputs_per_thread - 1) / max_inputs_per_thread));
252  const size_t num_threads_by_output_size =
253  std::min(max_thread_count, ((max_temp_output_entries + num_bins_ - 1) / num_bins_));
254  const size_t num_threads =
255  std::min(num_threads_by_input_elements, num_threads_by_output_size);
256  if (num_threads <= 1) {
257  computeSerialImpl<AggType, T2, Z2>(input_x, input_y, input_z, output_z);
258  return;
259  }
260  auto timer = DEBUG_TIMER(__func__);
261 
262  std::vector<std::vector<Z>> per_thread_z_outputs(num_threads);
263  // Fix
264  const Z agg_sentinel = raster_agg_type_ == RasterAggType::MIN
265  ? std::numeric_limits<Z>::max()
266  : std::numeric_limits<Z>::lowest();
267 
268  tbb::parallel_for(tbb::blocked_range<size_t>(0, num_threads),
269  [&](const tbb::blocked_range<size_t>& r) {
270  for (size_t t = r.begin(); t != r.end(); ++t) {
271  per_thread_z_outputs[t].resize(num_bins_, agg_sentinel);
272  }
273  });
274 
275  ComputeAgg<AggType> compute_agg;
276  tbb::task_arena limited_arena(num_threads);
277  limited_arena.execute([&] {
279  tbb::blocked_range<int64_t>(0, input_size),
280  [&](const tbb::blocked_range<int64_t>& r) {
281  const int64_t start_idx = r.begin();
282  const int64_t end_idx = r.end();
283  size_t thread_idx = tbb::this_task_arena::current_thread_index();
284  std::vector<Z>& this_thread_z_output = per_thread_z_outputs[thread_idx];
285 
286  for (int64_t sparse_idx = start_idx; sparse_idx != end_idx; ++sparse_idx) {
287  const int64_t x_bin = get_x_bin(input_x[sparse_idx]);
288  const int64_t y_bin = get_y_bin(input_y[sparse_idx]);
289  if (x_bin < 0 || x_bin >= num_x_bins_ || y_bin < 0 || y_bin >= num_y_bins_) {
290  continue;
291  }
292  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
293  compute_agg(input_z[sparse_idx],
294  this_thread_z_output[bin_idx],
295  inline_null_value<Z2>());
296  }
297  });
298  });
299 
300  // Reduce
301  if constexpr (AggType == RasterAggType::COUNT) {
302  // Counts can't be counted, they must be summed
303  computeParallelReductionAggImpl<RasterAggType::SUM>(
304  per_thread_z_outputs, output_z, agg_sentinel);
305  } else {
306  computeParallelReductionAggImpl<AggType>(
307  per_thread_z_outputs, output_z, agg_sentinel);
308  }
309 }
310 
311 template <typename T, typename Z>
312 template <typename T2, typename Z2>
314  const Column<T2>& input_y,
315  const Column<Z2>& input_z,
316  const size_t max_inputs_per_thread) {
317  switch (raster_agg_type_) {
318  case RasterAggType::COUNT: {
319  computeParallelImpl<RasterAggType::COUNT, T2, Z2>(
320  input_x, input_y, input_z, z_, max_inputs_per_thread);
321  break;
322  }
323  case RasterAggType::MIN: {
324  computeParallelImpl<RasterAggType::MIN, T2, Z2>(
325  input_x, input_y, input_z, z_, max_inputs_per_thread);
326  break;
327  }
328  case RasterAggType::MAX: {
329  computeParallelImpl<RasterAggType::MAX, T2, Z2>(
330  input_x, input_y, input_z, z_, max_inputs_per_thread);
331  break;
332  }
333  case RasterAggType::SUM: {
334  computeParallelImpl<RasterAggType::SUM, T2, Z2>(
335  input_x, input_y, input_z, z_, max_inputs_per_thread);
336  break;
337  }
338  case RasterAggType::AVG: {
339  computeParallelImpl<RasterAggType::SUM, T2, Z2>(
340  input_x, input_y, input_z, z_, max_inputs_per_thread);
341  computeParallelImpl<RasterAggType::COUNT, T2, Z2>(
342  input_x, input_y, input_z, counts_, max_inputs_per_thread);
343  tbb::parallel_for(tbb::blocked_range<size_t>(0, num_bins_),
344  [&](const tbb::blocked_range<size_t>& r) {
345  const size_t start_idx = r.begin();
346  const size_t end_idx = r.end();
347  for (size_t bin_idx = start_idx; bin_idx != end_idx;
348  ++bin_idx) {
349  // counts[bin_idx] > 1 will avoid division for nulls and 1
350  // counts
351  if (counts_[bin_idx] > 1) {
352  z_[bin_idx] /= counts_[bin_idx];
353  }
354  }
355  });
356  break;
357  }
358  default: {
359  CHECK(false);
360  }
361  }
362 }
363 
364 template <typename T, typename Z>
366  const int64_t x_centroid_bin,
367  const int64_t y_centroid_bin,
368  const int64_t bins_radius) const {
369  Z val = 0.0;
370  int32_t count = 0;
371  for (int64_t y_bin = y_centroid_bin - bins_radius;
372  y_bin <= y_centroid_bin + bins_radius;
373  y_bin++) {
374  for (int64_t x_bin = x_centroid_bin - bins_radius;
375  x_bin <= x_centroid_bin + bins_radius;
376  x_bin++) {
377  if (x_bin >= 0 && x_bin < num_x_bins_ && y_bin >= 0 && y_bin < num_y_bins_) {
378  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
379  const Z bin_val = z_[bin_idx];
380  if (bin_val != null_sentinel_) {
381  count++;
382  val += bin_val;
383  }
384  }
385  }
386  }
387  return (count == 0) ? null_sentinel_ : val / count;
388 }
389 
390 std::vector<double> generate_1d_gaussian_kernel(const int64_t fill_radius, double sigma) {
391  const int64_t kernel_size = fill_radius * 2 + 1;
392  std::vector<double> gaussian_kernel(kernel_size);
393  const double expr = 1.0 / (sigma * sqrt(2.0 * M_PI));
394  for (int64_t kernel_idx = -fill_radius; kernel_idx <= fill_radius; ++kernel_idx) {
395  gaussian_kernel[kernel_idx + fill_radius] =
396  expr * exp((kernel_idx * kernel_idx) / (-2.0 * (sigma * sigma)));
397  }
398  return gaussian_kernel;
399 }
400 
401 template <typename T, typename Z>
403  const int64_t neighborhood_fill_radius,
404  const bool fill_only_nulls) {
405  const double sigma = neighborhood_fill_radius * 2.0 / 6.0;
406  const auto gaussian_kernel =
407  generate_1d_gaussian_kernel(neighborhood_fill_radius, sigma);
408  std::vector<Z> only_nulls_source_z;
409  if (fill_only_nulls) {
410  // Copy z_
411  only_nulls_source_z.resize(z_.size());
412  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_bins_),
413  [&](const tbb::blocked_range<int64_t>& r) {
414  const int64_t end_bin_idx = r.end();
415  for (int64_t bin_idx = r.begin(); bin_idx < end_bin_idx;
416  ++bin_idx) {
417  only_nulls_source_z[bin_idx] = z_[bin_idx];
418  }
419  });
420  }
421  auto& source_z = fill_only_nulls ? only_nulls_source_z : z_;
422 
423  std::vector<Z> new_z(num_bins_);
425  tbb::blocked_range<int64_t>(0, num_y_bins_),
426  [&](const tbb::blocked_range<int64_t>& r) {
427  const int64_t end_y_bin = r.end();
428  for (int64_t y_start_bin = r.begin(); y_start_bin != end_y_bin; ++y_start_bin) {
429  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
430  Z val = 0.0;
431  Z sum_weights = 0.0;
432  for (int64_t y_offset = -neighborhood_fill_radius;
433  y_offset <= neighborhood_fill_radius;
434  ++y_offset) {
435  const auto y_bin = y_start_bin + y_offset;
436  if (y_bin >= 0 && y_bin < num_y_bins_) {
437  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
438  const Z bin_val = source_z[bin_idx];
439  if (bin_val != null_sentinel_) {
440  const auto gaussian_weight =
441  gaussian_kernel[y_offset + neighborhood_fill_radius];
442  val += bin_val * gaussian_weight;
443  sum_weights += gaussian_weight;
444  }
445  }
446  }
447  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_start_bin, num_x_bins_);
448  new_z[bin_idx] = sum_weights > 0.0 ? (val / sum_weights) : null_sentinel_;
449  }
450  }
451  });
452  source_z.swap(new_z);
454  tbb::blocked_range<int64_t>(0, num_x_bins_),
455  [&](const tbb::blocked_range<int64_t>& r) {
456  const int64_t end_x_bin = r.end();
457  for (int64_t x_start_bin = r.begin(); x_start_bin != end_x_bin; ++x_start_bin) {
458  for (int64_t y_bin = 0; y_bin < num_y_bins_; ++y_bin) {
459  Z val = 0.0;
460  Z sum_weights = 0.0;
461  for (int64_t x_offset = -neighborhood_fill_radius;
462  x_offset <= neighborhood_fill_radius;
463  ++x_offset) {
464  const auto x_bin = x_start_bin + x_offset;
465  if (x_bin >= 0 && x_bin < num_x_bins_) {
466  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
467  const Z bin_val = source_z[bin_idx];
468  if (bin_val != null_sentinel_) {
469  const auto gaussian_weight =
470  gaussian_kernel[x_offset + neighborhood_fill_radius];
471  val += bin_val * gaussian_weight;
472  sum_weights += gaussian_weight;
473  }
474  }
475  }
476  const int64_t bin_idx = x_y_bin_to_bin_index(x_start_bin, y_bin, num_x_bins_);
477  new_z[bin_idx] = sum_weights > 0.0 ? (val / sum_weights) : null_sentinel_;
478  }
479  }
480  });
481  if (fill_only_nulls) {
482  // Only copy convolved results in new_z back to z_ for
483  // values in z_ that are null
484  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_bins_),
485  [&](const tbb::blocked_range<int64_t>& r) {
486  const int64_t end_bin_idx = r.end();
487  for (int64_t bin_idx = r.begin(); bin_idx < end_bin_idx;
488  ++bin_idx) {
489  if (z_[bin_idx] == null_sentinel_) {
490  z_[bin_idx] = new_z[bin_idx];
491  }
492  only_nulls_source_z[bin_idx] = z_[bin_idx];
493  }
494  });
495  } else {
496  source_z.swap(new_z);
497  }
498 }
499 
500 template <typename T, typename Z>
501 template <RasterAggType AggType>
503  const int64_t x_centroid_bin,
504  const int64_t y_centroid_bin,
505  const int64_t bins_radius,
506  const ComputeAgg<AggType>& compute_agg) const {
507  const Z agg_sentinel = AggType == RasterAggType::MIN ? std::numeric_limits<Z>::max()
508  : std::numeric_limits<Z>::lowest();
509  Z val{agg_sentinel};
510  for (int64_t y_bin = y_centroid_bin - bins_radius;
511  y_bin <= y_centroid_bin + bins_radius;
512  y_bin++) {
513  for (int64_t x_bin = x_centroid_bin - bins_radius;
514  x_bin <= x_centroid_bin + bins_radius;
515  x_bin++) {
516  if (x_bin >= 0 && x_bin < num_x_bins_ && y_bin >= 0 && y_bin < num_y_bins_) {
517  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
518  compute_agg(z_[bin_idx], val, null_sentinel_);
519  }
520  }
521  }
522  return val == agg_sentinel ? null_sentinel_ : val;
523 }
524 
525 template <typename T, typename Z>
526 template <RasterAggType AggType>
528  const int64_t neighborhood_fill_radius,
529  const bool fill_only_nulls) {
530  CHECK(AggType != RasterAggType::GAUSS_AVG); // Should have gone down Gaussian path
531  CHECK(AggType != RasterAggType::AVG); // Should be BOX_AVG for fill pass
532  CHECK(AggType != RasterAggType::INVALID);
533  std::vector<Z> new_z(num_bins_);
534  ComputeAgg<AggType> compute_agg;
535  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_y_bins_),
536  [&](const tbb::blocked_range<int64_t>& r) {
537  const int64_t end_y_bin = r.end();
538  for (int64_t y_bin = r.begin(); y_bin != end_y_bin; ++y_bin) {
539  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
540  const int64_t bin_idx =
541  x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
542  const Z z_val = z_[bin_idx];
543  if (!fill_only_nulls || z_val == null_sentinel_) {
544  if constexpr (AggType == RasterAggType::BOX_AVG) {
545  new_z[bin_idx] = fill_bin_from_avg_box_neighborhood(
546  x_bin, y_bin, neighborhood_fill_radius);
547  } else {
548  new_z[bin_idx] = fill_bin_from_box_neighborhood(
549  x_bin, y_bin, neighborhood_fill_radius, compute_agg);
550  }
551  } else {
552  new_z[bin_idx] = z_val;
553  }
554  }
555  }
556  });
557  z_.swap(new_z);
558 }
559 
560 template <typename T, typename Z>
561 void GeoRaster<T, Z>::fill_bins_from_neighbors(const int64_t neighborhood_fill_radius,
562  const bool fill_only_nulls,
563  const RasterAggType raster_fill_agg_type) {
564  auto timer = DEBUG_TIMER(__func__);
565  // Note: only GAUSSIAN_AVG supports Gaussian 2-pass technique currently,
566  // as only an AVG aggregate makes sense for Gaussian blur. However we
567  // should be able to implement 2-pass box convolution for the other aggregates
568  // when time allows for better performance with wide kernel sizes
569  // Todo (Todd): Implement 2-pass box blur
570 
571  switch (raster_fill_agg_type) {
572  case RasterAggType::COUNT: {
573  fill_bins_from_box_neighborhood<RasterAggType::COUNT>(neighborhood_fill_radius,
574  fill_only_nulls);
575  break;
576  }
577  case RasterAggType::MIN: {
578  fill_bins_from_box_neighborhood<RasterAggType::MIN>(neighborhood_fill_radius,
579  fill_only_nulls);
580  break;
581  }
582  case RasterAggType::MAX: {
583  fill_bins_from_box_neighborhood<RasterAggType::MAX>(neighborhood_fill_radius,
584  fill_only_nulls);
585  break;
586  }
587  case RasterAggType::SUM: {
588  fill_bins_from_box_neighborhood<RasterAggType::SUM>(neighborhood_fill_radius,
589  fill_only_nulls);
590  break;
591  }
592  case RasterAggType::BOX_AVG: {
593  fill_bins_from_box_neighborhood<RasterAggType::BOX_AVG>(neighborhood_fill_radius,
594  fill_only_nulls);
595  break;
596  }
598  fill_bins_from_gaussian_neighborhood(neighborhood_fill_radius, fill_only_nulls);
599  break;
600  }
601  default: {
602  UNREACHABLE();
603  }
604  }
605 }
606 
607 template <typename T, typename Z>
609  const int64_t x_bin,
610  const int64_t y_bin,
611  const int64_t num_bins_radius,
612  std::vector<Z>& neighboring_bins) const {
613  const size_t num_bins_per_dim = num_bins_radius * 2 + 1;
614  CHECK_EQ(neighboring_bins.size(), num_bins_per_dim * num_bins_per_dim);
615  const int64_t end_y_bin_idx = y_bin + num_bins_radius;
616  const int64_t end_x_bin_idx = x_bin + num_bins_radius;
617  size_t output_bin = 0;
618  for (int64_t y_bin_idx = y_bin - num_bins_radius; y_bin_idx <= end_y_bin_idx;
619  ++y_bin_idx) {
620  for (int64_t x_bin_idx = x_bin - num_bins_radius; x_bin_idx <= end_x_bin_idx;
621  ++x_bin_idx) {
622  if (x_bin_idx < 0 || x_bin_idx >= num_x_bins_ || y_bin_idx < 0 ||
623  y_bin_idx >= num_y_bins_) {
624  return false;
625  }
626  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin_idx, y_bin_idx, num_x_bins_);
627  neighboring_bins[output_bin++] = z_[bin_idx];
628  if (z_[bin_idx] == null_sentinel_) {
629  return false;
630  }
631  }
632  }
633  return true; // not_null
634 }
635 
636 template <typename T, typename Z>
638  const std::vector<Z>& neighboring_cells,
639  const bool compute_slope_in_degrees) const {
640  const Z dz_dx =
641  ((neighboring_cells[8] + 2 * neighboring_cells[5] + neighboring_cells[2]) -
642  (neighboring_cells[6] + 2 * neighboring_cells[3] + neighboring_cells[0])) /
643  (8 * bin_dim_meters_);
644  const Z dz_dy =
645  ((neighboring_cells[6] + 2 * neighboring_cells[7] + neighboring_cells[8]) -
646  (neighboring_cells[0] + 2 * neighboring_cells[1] + neighboring_cells[2])) /
647  (8 * bin_dim_meters_);
648  const Z slope = sqrt(dz_dx * dz_dx + dz_dy * dz_dy);
649  std::pair<Z, Z> slope_and_aspect;
650  slope_and_aspect.first =
651  compute_slope_in_degrees ? atan(slope) * math_consts::radians_to_degrees : slope;
652  if (slope < 0.0001) {
653  slope_and_aspect.second = null_sentinel_;
654  } else {
655  const Z aspect_degrees =
656  math_consts::radians_to_degrees * atan2(dz_dx, dz_dy); // -180.0 to 180.0
657  slope_and_aspect.second = aspect_degrees + 180.0;
658  // aspect_degrees < 0.0 ? 180.0 + aspect_degrees : aspect_degrees;
659  }
660  return slope_and_aspect;
661 }
662 
663 template <typename T, typename Z>
665  Column<Z>& slope,
666  Column<Z>& aspect,
667  const bool compute_slope_in_degrees) const {
668  auto timer = DEBUG_TIMER(__func__);
669  CHECK_EQ(slope.size(), num_bins_);
671  tbb::blocked_range<int64_t>(0, num_y_bins_),
672  [&](const tbb::blocked_range<int64_t>& r) {
673  std::vector<Z> neighboring_z_vals(9); // 3X3 calc
674  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
675  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
676  const bool not_null =
677  get_nxn_neighbors_if_not_null(x_bin, y_bin, 1, neighboring_z_vals);
678  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
679  if (!not_null) {
680  slope.setNull(bin_idx);
681  aspect.setNull(bin_idx);
682  } else {
683  const auto slope_and_aspect = calculate_slope_and_aspect_of_cell(
684  neighboring_z_vals, compute_slope_in_degrees);
685  slope[bin_idx] = slope_and_aspect.first;
686  if (slope_and_aspect.second == null_sentinel_) {
687  aspect.setNull(bin_idx);
688  } else {
689  aspect[bin_idx] = slope_and_aspect.second;
690  }
691  }
692  }
693  }
694  });
695 }
696 
697 template <typename T, typename Z>
700  Column<T>& output_x,
701  Column<T>& output_y,
702  Column<Z>& output_z,
703  const int64_t neighborhood_null_fill_radius) const {
704  auto timer = DEBUG_TIMER(__func__);
705  mgr.set_output_row_size(num_bins_);
707  tbb::blocked_range<int64_t>(0, num_y_bins_),
708  [&](const tbb::blocked_range<int64_t>& r) {
709  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
710  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
711  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
712  output_x[bin_idx] = x_min_ + (x_bin + 0.5) * x_scale_bin_to_input_;
713  output_y[bin_idx] = y_min_ + (y_bin + 0.5) * y_scale_bin_to_input_;
714  const Z z_val = z_[bin_idx];
715  if (z_val == null_sentinel_) {
716  output_z.setNull(bin_idx);
717  if (neighborhood_null_fill_radius) {
718  const Z avg_neighbor_value = fill_bin_from_avg_box_neighborhood(
719  x_bin, y_bin, neighborhood_null_fill_radius);
720  if (avg_neighbor_value != null_sentinel_) {
721  output_z[bin_idx] = avg_neighbor_value;
722  }
723  }
724  } else {
725  output_z[bin_idx] = z_[bin_idx];
726  }
727  }
728  }
729  });
730  return num_bins_;
731 }
732 
733 template <typename T, typename Z>
735  Column<T>& output_x,
736  Column<T>& output_y,
737  Column<Z>& output_z) const {
738  auto timer = DEBUG_TIMER(__func__);
739  mgr.set_output_row_size(num_bins_);
741  tbb::blocked_range<int64_t>(0, num_y_bins_),
742  [&](const tbb::blocked_range<int64_t>& r) {
743  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
744  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
745  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
746  output_x[bin_idx] = x_min_ + (x_bin + 0.5) * x_scale_bin_to_input_;
747  output_y[bin_idx] = y_min_ + (y_bin + 0.5) * y_scale_bin_to_input_;
748  const Z z_val = z_[bin_idx];
749  if (z_val == null_sentinel_) {
750  output_z.setNull(bin_idx);
751  } else {
752  output_z[bin_idx] = z_[bin_idx];
753  }
754  }
755  }
756  });
757  return num_bins_;
758 }
759 
760 template <typename T, typename Z>
762  mgr.set_metadata("geo_raster_num_x_bins", num_x_bins_);
763  mgr.set_metadata("geo_raster_num_y_bins", num_y_bins_);
764  mgr.set_metadata("geo_raster_x_min", static_cast<double>(x_min_));
765  mgr.set_metadata("geo_raster_y_min", static_cast<double>(y_min_));
766  mgr.set_metadata("geo_raster_x_scale_input_to_bin",
767  static_cast<double>(x_scale_input_to_bin_));
768  mgr.set_metadata("geo_raster_y_scale_input_to_bin",
769  static_cast<double>(y_scale_input_to_bin_));
770 }
771 
772 #endif // __CUDACC__
void set_output_row_size(int64_t num_rows)
#define CHECK_EQ(x, y)
Definition: Logger.h:230
constexpr double radians_to_degrees
Definition: math_consts.h:23
Z offset_source_z_from_raster_z(const int64_t source_x_bin, const int64_t source_y_bin, const Z source_z_offset) const
bool get_nxn_neighbors_if_not_null(const int64_t x_bin, const int64_t y_bin, const int64_t num_bins_radius, std::vector< Z > &neighboring_bins) const
void setMetadata(TableFunctionManager &mgr) const
std::pair< Z, Z > calculate_slope_and_aspect_of_cell(const std::vector< Z > &neighboring_cells, const bool compute_slope_in_degrees) const
NEVER_INLINE HOST std::pair< T, T > get_column_min_max(const Column< T > &col)
GeoRaster(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, const RasterAggType raster_agg_type, const double bin_dim_meters, const bool geographic_coords, const bool align_bins_to_zero_based_grid)
int64_t outputDenseColumns(TableFunctionManager &mgr, Column< T > &output_x, Column< T > &output_y, Column< Z > &output_z) const
DEVICE int64_t size() const
Definition: heavydbTypes.h:592
#define UNREACHABLE()
Definition: Logger.h:266
void fill_bins_from_neighbors(const int64_t neighborhood_fill_radius, const bool fill_only_nulls, const RasterAggType raster_agg_type=RasterAggType::GAUSS_AVG)
void computeParallelReductionAggImpl(const std::vector< std::vector< Z >> &z_inputs, std::vector< Z > &output_z, const Z agg_sentinel)
const size_t max_inputs_per_thread
#define M_PI
Definition: constants.h:25
DEVICE TextEncodingDict inline_null_value()
Definition: heavydbTypes.h:166
const size_t max_temp_output_entries
std::vector< double > generate_1d_gaussian_kernel(const int64_t fill_radius, double sigma)
int64_t x_y_bin_to_bin_index(const int64_t x_bin, const int64_t y_bin, const int64_t num_x_bins)
void fill_bins_from_gaussian_neighborhood(const int64_t neighborhood_fill_radius, const bool fill_only_nulls)
const bool geographic_coords_
void calculate_slope_and_aspect(Column< Z > &slope, Column< Z > &aspect, const bool compute_slope_in_degrees) const
void computeParallelImpl(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, std::vector< Z > &output_z, const size_t max_inputs_per_thread)
Z fill_bin_from_box_neighborhood(const int64_t x_centroid_bin, const int64_t y_centroid_bin, const int64_t bins_radius, const ComputeAgg< AggType > &compute_agg) const
void fill_bins_from_box_neighborhood(const int64_t neighborhood_fill_radius, const bool fill_only_nulls)
DEVICE void setNull(int64_t index)
Definition: heavydbTypes.h:596
void compute(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, const size_t max_inputs_per_thread)
Z fill_bin_from_avg_box_neighborhood(const int64_t x_centroid_bin, const int64_t y_centroid_bin, const int64_t bins_radius) const
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
EXTENSION_NOINLINE double distance_in_meters(const double fromlon, const double fromlat, const double tolon, const double tolat)
Computes the distance, in meters, between two WGS-84 positions.
void set_metadata(const std::string &key, const T &value)
#define CHECK(condition)
Definition: Logger.h:222
#define DEBUG_TIMER(name)
Definition: Logger.h:371
void computeSerialImpl(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, std::vector< Z > &output_z)