OmniSciDB  4201147b46
 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 double bin_dim_meters,
39  const bool geographic_coords,
40  const bool align_bins_to_zero_based_grid)
41  : bin_dim_meters_(bin_dim_meters)
42  , geographic_coords_(geographic_coords)
43  , null_sentinel_(std::numeric_limits<Z>::lowest()) {
44  auto timer = DEBUG_TIMER(__func__);
45  const int64_t input_size{input_z.size()};
46  if (input_size <= 0) {
47  num_bins_ = 0;
48  num_x_bins_ = 0;
49  num_y_bins_ = 0;
50  return;
51  }
52  const auto min_max_x = get_column_min_max(input_x);
53  const auto min_max_y = get_column_min_max(input_y);
54  x_min_ = min_max_x.first;
55  x_max_ = min_max_x.second;
56  y_min_ = min_max_y.first;
57  y_max_ = min_max_y.second;
58 
59  if (align_bins_to_zero_based_grid && !geographic_coords_) {
60  // For implicit, data-defined bounds, we treat the max of the x and y ranges as
61  // inclusive (closed interval), since if the max of the data in either x/y dimensions
62  // is at the first value of the next bin, values at that max will be discarded if we
63  // don't include the final bin. For exmaple, if the input data (perhaps already binned
64  // with a group by query) goes from 0.0 to 40.0 in both x and y directions, we should
65  // have the last x/y bins cover the range [40.0, 50.0), not [30.0, 40.0)
67  }
68 
70  computeParallel(input_x, input_y, input_z, max_inputs_per_thread);
71 }
72 
73 // Allow input types to GeoRaster that are different than class types/output Z type
74 // So we can move everything to the type of T and Z (which can each be either float
75 // or double)
76 template <typename T, typename Z>
77 template <typename T2, typename Z2>
79  const Column<T2>& input_y,
80  const Column<Z2>& input_z,
81  const double bin_dim_meters,
82  const bool geographic_coords,
83  const bool align_bins_to_zero_based_grid,
84  const T x_min,
85  const T x_max,
86  const T y_min,
87  const T y_max)
88  : bin_dim_meters_(bin_dim_meters)
89  , geographic_coords_(geographic_coords)
90  , null_sentinel_(std::numeric_limits<Z>::lowest())
91  , x_min_(x_min)
92  , x_max_(x_max)
93  , y_min_(y_min)
94  , y_max_(y_max) {
95  auto timer = DEBUG_TIMER(__func__);
96  if (align_bins_to_zero_based_grid && !geographic_coords_) {
97  // For explicit, user-defined bounds, we treat the max of the x and y ranges as
98  // exclusive (open interval), since if the user specifies the max x/y as the end of
99  // the bin, they do not intend to add the next full bin For example, if a user
100  // specifies a bin_dim_meters of 10.0 and an x and y range from 0 to 40.0, they almost
101  // assuredly intend for there to be 4 bins in each of the x and y dimensions, with the
102  // last bin of range [30.0, 40.0), not 5 with the final bin's range from [40.0, 50.0)
104  }
106  computeParallel(input_x, input_y, input_z, max_inputs_per_thread);
107 }
108 
109 template <typename T, typename Z>
110 inline Z GeoRaster<T, Z>::offset_source_z_from_raster_z(const int64_t source_x_bin,
111  const int64_t source_y_bin,
112  const Z source_z_offset) const {
113  if (is_bin_out_of_bounds(source_x_bin, source_y_bin)) {
114  return null_sentinel_;
115  }
116  const Z terrain_z = z_[x_y_bin_to_bin_index(source_x_bin, source_y_bin, num_x_bins_)];
117  if (terrain_z == null_sentinel_) {
118  return terrain_z;
119  }
120  return terrain_z + source_z_offset;
121 }
122 
123 template <typename T, typename Z>
124 inline Z GeoRaster<T, Z>::fill_bin_from_avg_neighbors(const int64_t x_centroid_bin,
125  const int64_t y_centroid_bin,
126  const int64_t bins_radius) const {
127  T val = 0.0;
128  int32_t count = 0;
129  for (int64_t y_bin = y_centroid_bin - bins_radius;
130  y_bin <= y_centroid_bin + bins_radius;
131  y_bin++) {
132  for (int64_t x_bin = x_centroid_bin - bins_radius;
133  x_bin <= x_centroid_bin + bins_radius;
134  x_bin++) {
135  if (x_bin >= 0 && x_bin < num_x_bins_ && y_bin >= 0 && y_bin < num_y_bins_) {
136  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
137  const Z bin_val = z_[bin_idx];
138  if (bin_val != null_sentinel_) {
139  count++;
140  val += bin_val;
141  }
142  }
143  }
144  }
145  return (count == 0) ? null_sentinel_ : val / count;
146 }
147 
148 template <typename T, typename Z>
150  x_min_ = std::floor(x_min_ / bin_dim_meters_) * bin_dim_meters_;
151  x_max_ = std::floor(x_max_ / bin_dim_meters_) * bin_dim_meters_ +
152  bin_dim_meters_; // Snap to end of bin
153  y_min_ = std::floor(y_min_ / bin_dim_meters_) * bin_dim_meters_;
154  y_max_ = std::floor(y_max_ / bin_dim_meters_) * bin_dim_meters_ +
155  bin_dim_meters_; // Snap to end of bin
156 }
157 
158 template <typename T, typename Z>
160  x_min_ = std::floor(x_min_ / bin_dim_meters_) * bin_dim_meters_;
161  x_max_ = std::ceil(x_max_ / bin_dim_meters_) * bin_dim_meters_;
162  y_min_ = std::floor(y_min_ / bin_dim_meters_) * bin_dim_meters_;
163  y_max_ = std::ceil(y_max_ / bin_dim_meters_) * bin_dim_meters_;
164 }
165 
166 template <typename T, typename Z>
168  x_range_ = x_max_ - x_min_;
169  y_range_ = y_max_ - y_min_;
170  if (geographic_coords_) {
171  const T x_centroid = (x_min_ + x_max_) * 0.5;
172  const T y_centroid = (y_min_ + y_max_) * 0.5;
173  x_meters_per_degree_ =
174  distance_in_meters(x_min_, y_centroid, x_max_, y_centroid) / x_range_;
175 
176  y_meters_per_degree_ =
177  distance_in_meters(x_centroid, y_min_, x_centroid, y_max_) / y_range_;
178 
179  num_x_bins_ = x_range_ * x_meters_per_degree_ / bin_dim_meters_;
180  num_y_bins_ = y_range_ * y_meters_per_degree_ / bin_dim_meters_;
181 
182  x_scale_input_to_bin_ = x_meters_per_degree_ / bin_dim_meters_;
183  y_scale_input_to_bin_ = y_meters_per_degree_ / bin_dim_meters_;
184  x_scale_bin_to_input_ = bin_dim_meters_ / x_meters_per_degree_;
185  y_scale_bin_to_input_ = bin_dim_meters_ / y_meters_per_degree_;
186 
187  } else {
188  num_x_bins_ = x_range_ / bin_dim_meters_;
189  num_y_bins_ = y_range_ / bin_dim_meters_;
190 
191  x_scale_input_to_bin_ = 1.0 / bin_dim_meters_;
192  y_scale_input_to_bin_ = 1.0 / bin_dim_meters_;
193  x_scale_bin_to_input_ = bin_dim_meters_;
194  y_scale_bin_to_input_ = bin_dim_meters_;
195  }
196  num_bins_ = num_x_bins_ * num_y_bins_;
197 }
198 
199 template <typename T, typename Z>
200 template <typename T2, typename Z2>
202  const Column<T2>& input_y,
203  const Column<Z2>& input_z) {
204  auto timer = DEBUG_TIMER(__func__);
205  const int64_t input_size{input_z.size()};
206  z_.resize(num_bins_, null_sentinel_);
207  for (int64_t sparse_idx = 0; sparse_idx != input_size; ++sparse_idx) {
208  const int64_t x_bin = get_x_bin(input_x[sparse_idx]);
209  const int64_t y_bin = get_y_bin(input_y[sparse_idx]);
210  if (x_bin < 0 || x_bin >= num_x_bins_ || y_bin < 0 || y_bin >= num_y_bins_) {
211  continue;
212  }
213  // Take the max height for this version, but may want to allow different metrics
214  // like average as well
215  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
216  if (!(input_z.isNull(sparse_idx)) && input_z[sparse_idx] > z_[bin_idx]) {
217  z_[bin_idx] = input_z[sparse_idx];
218  }
219  }
220 }
221 
222 template <typename T, typename Z>
223 template <typename T2, typename Z2>
225  const Column<T2>& input_y,
226  const Column<Z2>& input_z,
227  const size_t max_inputs_per_thread) {
228  const size_t input_size = input_z.size();
229  const size_t max_thread_count = std::thread::hardware_concurrency();
230  const size_t num_threads_by_input_elements =
231  std::min(max_thread_count,
232  ((input_size + max_inputs_per_thread - 1) / max_inputs_per_thread));
233  const size_t num_threads_by_output_size =
234  std::min(max_thread_count, ((max_temp_output_entries + num_bins_ - 1) / num_bins_));
235  const size_t num_threads =
236  std::min(num_threads_by_input_elements, num_threads_by_output_size);
237  if (num_threads <= 1) {
238  compute(input_x, input_y, input_z);
239  return;
240  }
241  auto timer = DEBUG_TIMER(__func__);
242 
243  std::vector<std::vector<Z>> per_thread_z_outputs(num_threads);
244 
245  tbb::parallel_for(tbb::blocked_range<size_t>(0, num_threads),
246  [&](const tbb::blocked_range<size_t>& r) {
247  for (size_t t = r.begin(); t != r.end(); ++t) {
248  per_thread_z_outputs[t].resize(num_bins_, null_sentinel_);
249  }
250  });
251 
252  tbb::task_arena limited_arena(num_threads);
253  limited_arena.execute([&] {
255  tbb::blocked_range<int64_t>(0, input_size),
256  [&](const tbb::blocked_range<int64_t>& r) {
257  const int64_t start_idx = r.begin();
258  const int64_t end_idx = r.end();
259  size_t thread_idx = tbb::this_task_arena::current_thread_index();
260  std::vector<Z>& this_thread_z_output = per_thread_z_outputs[thread_idx];
261 
262  for (int64_t sparse_idx = start_idx; sparse_idx != end_idx; ++sparse_idx) {
263  const int64_t x_bin = get_x_bin(input_x[sparse_idx]);
264  const int64_t y_bin = get_y_bin(input_y[sparse_idx]);
265  if (x_bin < 0 || x_bin >= num_x_bins_ || y_bin < 0 || y_bin >= num_y_bins_) {
266  continue;
267  }
268  // Take the max height for this version, but may want to allow different
269  // metrics like average as well
270  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
271  if (!(input_z.isNull(sparse_idx)) &&
272  input_z[sparse_idx] > this_thread_z_output[bin_idx]) {
273  this_thread_z_output[bin_idx] = input_z[sparse_idx];
274  }
275  }
276  });
277  });
278 
279  z_.resize(num_bins_, null_sentinel_);
280 
282  tbb::blocked_range<size_t>(0, num_bins_), [&](const tbb::blocked_range<size_t>& r) {
283  const size_t start_idx = r.begin();
284  const size_t end_idx = r.end();
285  for (size_t bin_idx = start_idx; bin_idx != end_idx; ++bin_idx) {
286  for (size_t thread_idx = 0; thread_idx < num_threads; ++thread_idx) {
287  const T thread_bin_z_output = per_thread_z_outputs[thread_idx][bin_idx];
288  if (thread_bin_z_output != null_sentinel_ &&
289  thread_bin_z_output > z_[bin_idx]) {
290  z_[bin_idx] = thread_bin_z_output;
291  }
292  }
293  }
294  });
295 }
296 
297 template <typename T, typename Z>
298 void GeoRaster<T, Z>::fill_bins_from_neighbors(const int64_t neighborhood_fill_radius,
299  const bool fill_only_nulls) {
300  auto timer = DEBUG_TIMER(__func__);
301  std::vector<Z> new_z(num_bins_);
302  tbb::parallel_for(tbb::blocked_range<int64_t>(0, num_y_bins_),
303  [&](const tbb::blocked_range<int64_t>& r) {
304  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
305  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
306  const int64_t bin_idx =
307  x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
308  const Z z_val = z_[bin_idx];
309  if (!fill_only_nulls || z_val == null_sentinel_) {
310  new_z[bin_idx] = fill_bin_from_avg_neighbors(
311  x_bin, y_bin, neighborhood_fill_radius);
312  } else {
313  new_z[bin_idx] = z_val;
314  }
315  }
316  }
317  });
318  z_.swap(new_z);
319 }
320 
321 template <typename T, typename Z>
323  const int64_t x_bin,
324  const int64_t y_bin,
325  const int64_t num_bins_radius,
326  std::vector<Z>& neighboring_bins) const {
327  const size_t num_bins_per_dim = num_bins_radius * 2 + 1;
328  CHECK_EQ(neighboring_bins.size(), num_bins_per_dim * num_bins_per_dim);
329  const int64_t end_y_bin_idx = y_bin + num_bins_radius;
330  const int64_t end_x_bin_idx = x_bin + num_bins_radius;
331  size_t output_bin = 0;
332  for (int64_t y_bin_idx = y_bin - num_bins_radius; y_bin_idx <= end_y_bin_idx;
333  ++y_bin_idx) {
334  for (int64_t x_bin_idx = x_bin - num_bins_radius; x_bin_idx <= end_x_bin_idx;
335  ++x_bin_idx) {
336  if (x_bin_idx < 0 || x_bin_idx >= num_x_bins_ || y_bin_idx < 0 ||
337  y_bin_idx >= num_y_bins_) {
338  return false;
339  }
340  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin_idx, y_bin_idx, num_x_bins_);
341  neighboring_bins[output_bin++] = z_[bin_idx];
342  if (z_[bin_idx] == null_sentinel_) {
343  return false;
344  }
345  }
346  }
347  return true; // not_null
348 }
349 
350 template <typename T, typename Z>
352  const std::vector<Z>& neighboring_cells,
353  const bool compute_slope_in_degrees) const {
354  const Z dz_dx =
355  ((neighboring_cells[8] + 2 * neighboring_cells[5] + neighboring_cells[2]) -
356  (neighboring_cells[6] + 2 * neighboring_cells[3] + neighboring_cells[0])) /
357  (8 * bin_dim_meters_);
358  const Z dz_dy =
359  ((neighboring_cells[6] + 2 * neighboring_cells[7] + neighboring_cells[8]) -
360  (neighboring_cells[0] + 2 * neighboring_cells[1] + neighboring_cells[2])) /
361  (8 * bin_dim_meters_);
362  const Z slope = sqrt(dz_dx * dz_dx + dz_dy * dz_dy);
363  std::pair<Z, Z> slope_and_aspect;
364  slope_and_aspect.first =
365  compute_slope_in_degrees ? atan(slope) * math_consts::radians_to_degrees : slope;
366  if (slope < 0.0001) {
367  slope_and_aspect.second = null_sentinel_;
368  } else {
369  const Z aspect_degrees =
370  math_consts::radians_to_degrees * atan2(dz_dx, dz_dy); // -180.0 to 180.0
371  slope_and_aspect.second = aspect_degrees + 180.0;
372  // aspect_degrees < 0.0 ? 180.0 + aspect_degrees : aspect_degrees;
373  }
374  return slope_and_aspect;
375 }
376 
377 template <typename T, typename Z>
379  Column<Z>& slope,
380  Column<Z>& aspect,
381  const bool compute_slope_in_degrees) const {
382  auto timer = DEBUG_TIMER(__func__);
383  CHECK_EQ(slope.size(), num_bins_);
385  tbb::blocked_range<int64_t>(0, num_y_bins_),
386  [&](const tbb::blocked_range<int64_t>& r) {
387  std::vector<Z> neighboring_z_vals(9); // 3X3 calc
388  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
389  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
390  const bool not_null =
391  get_nxn_neighbors_if_not_null(x_bin, y_bin, 1, neighboring_z_vals);
392  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
393  if (!not_null) {
394  slope.setNull(bin_idx);
395  aspect.setNull(bin_idx);
396  } else {
397  const auto slope_and_aspect = calculate_slope_and_aspect_of_cell(
398  neighboring_z_vals, compute_slope_in_degrees);
399  slope[bin_idx] = slope_and_aspect.first;
400  if (slope_and_aspect.second == null_sentinel_) {
401  aspect.setNull(bin_idx);
402  } else {
403  aspect[bin_idx] = slope_and_aspect.second;
404  }
405  }
406  }
407  }
408  });
409 }
410 
411 template <typename T, typename Z>
414  Column<T>& output_x,
415  Column<T>& output_y,
416  Column<Z>& output_z,
417  const int64_t neighborhood_null_fill_radius) const {
418  auto timer = DEBUG_TIMER(__func__);
419  mgr.set_output_row_size(num_bins_);
421  tbb::blocked_range<int64_t>(0, num_y_bins_),
422  [&](const tbb::blocked_range<int64_t>& r) {
423  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
424  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
425  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
426  output_x[bin_idx] = x_min_ + (x_bin + 0.5) * x_scale_bin_to_input_;
427  output_y[bin_idx] = y_min_ + (y_bin + 0.5) * y_scale_bin_to_input_;
428  const Z z_val = z_[bin_idx];
429  if (z_val == null_sentinel_) {
430  output_z.setNull(bin_idx);
431  if (neighborhood_null_fill_radius) {
432  const Z avg_neighbor_value = fill_bin_from_avg_neighbors(
433  x_bin, y_bin, neighborhood_null_fill_radius);
434  if (avg_neighbor_value != null_sentinel_) {
435  output_z[bin_idx] = avg_neighbor_value;
436  }
437  }
438  } else {
439  output_z[bin_idx] = z_[bin_idx];
440  }
441  }
442  }
443  });
444  return num_bins_;
445 }
446 
447 template <typename T, typename Z>
449  Column<T>& output_x,
450  Column<T>& output_y,
451  Column<Z>& output_z) const {
452  auto timer = DEBUG_TIMER(__func__);
453  mgr.set_output_row_size(num_bins_);
455  tbb::blocked_range<int64_t>(0, num_y_bins_),
456  [&](const tbb::blocked_range<int64_t>& r) {
457  for (int64_t y_bin = r.begin(); y_bin != r.end(); ++y_bin) {
458  for (int64_t x_bin = 0; x_bin < num_x_bins_; ++x_bin) {
459  const int64_t bin_idx = x_y_bin_to_bin_index(x_bin, y_bin, num_x_bins_);
460  output_x[bin_idx] = x_min_ + (x_bin + 0.5) * x_scale_bin_to_input_;
461  output_y[bin_idx] = y_min_ + (y_bin + 0.5) * y_scale_bin_to_input_;
462  const Z z_val = z_[bin_idx];
463  if (z_val == null_sentinel_) {
464  output_z.setNull(bin_idx);
465  } else {
466  output_z[bin_idx] = z_[bin_idx];
467  }
468  }
469  }
470  });
471  return num_bins_;
472 }
473 
474 #endif // __CUDACC__
void set_output_row_size(int64_t num_rows)
Definition: heavydbTypes.h:678
#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
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)
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:468
void compute(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z)
Z fill_bin_from_avg_neighbors(const int64_t x_centroid_bin, const int64_t y_centroid_bin, const int64_t bins_radius) const
const size_t max_inputs_per_thread
void computeParallel(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, const size_t max_inputs_per_thread)
GeoRaster(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, const double bin_dim_meters, const bool geographic_coords, const bool align_bins_to_zero_based_grid)
const size_t max_temp_output_entries
void fill_bins_from_neighbors(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
DEVICE bool isNull(int64_t index) const
Definition: heavydbTypes.h:470
DEVICE void setNull(int64_t index)
Definition: heavydbTypes.h:471
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.
#define DEBUG_TIMER(name)
Definition: Logger.h:369
int64_t x_y_bin_to_bin_index(const int64_t x_bin, const int64_t y_bin, const int64_t num_x_bins)