OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeoRasterTableFunctions.h
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 #pragma once
18 
19 #ifndef __CUDACC__
20 
21 #include <vector>
23 
24 template <typename T, typename Z>
25 struct GeoRaster {
27  const bool geographic_coords_;
28  const Z null_sentinel_;
29  std::vector<Z> z_;
38  int64_t num_x_bins_;
39  int64_t num_y_bins_;
40  int64_t num_bins_;
45 
46  template <typename T2, typename Z2>
47  GeoRaster(const Column<T2>& input_x,
48  const Column<T2>& input_y,
49  const Column<Z2>& input_z,
50  const double bin_dim_meters,
51  const bool geographic_coords,
52  const bool align_bins_to_zero_based_grid);
53 
54  template <typename T2, typename Z2>
55  GeoRaster(const Column<T2>& input_x,
56  const Column<T2>& input_y,
57  const Column<Z2>& input_z,
58  const double bin_dim_meters,
59  const bool geographic_coords,
60  const bool align_bins_to_zero_based_grid,
61  const T x_min,
62  const T x_max,
63  const T y_min,
64  const T y_max);
65 
66  inline T get_x_bin(const T input) const {
67  return (input - x_min_) * x_scale_input_to_bin_;
68  }
69 
70  inline T get_y_bin(const T input) const {
71  return (input - y_min_) * y_scale_input_to_bin_;
72  }
73 
74  inline bool is_null(const Z value) const { return value == null_sentinel_; }
75 
76  inline bool is_bin_out_of_bounds(const int64_t source_x_bin,
77  const int64_t source_y_bin) const {
78  return (source_x_bin < 0 || source_x_bin >= num_x_bins_ || source_y_bin < 0 ||
79  source_y_bin >= num_y_bins_);
80  }
81 
82  inline Z offset_source_z_from_raster_z(const int64_t source_x_bin,
83  const int64_t source_y_bin,
84  const Z source_z_offset) const;
85 
86  inline Z fill_bin_from_avg_neighbors(const int64_t x_centroid_bin,
87  const int64_t y_centroid_bin,
88  const int64_t bins_radius) const;
89 
91 
93 
95 
96  template <typename T2, typename Z2>
97  void compute(const Column<T2>& input_x,
98  const Column<T2>& input_y,
99  const Column<Z2>& input_z);
100 
101  template <typename T2, typename Z2>
102  void computeParallel(const Column<T2>& input_x,
103  const Column<T2>& input_y,
104  const Column<Z2>& input_z,
105  const size_t max_inputs_per_thread);
106 
107  void fill_bins_from_neighbors(const int64_t neighborhood_fill_radius,
108  const bool fill_only_nulls);
109 
110  bool get_nxn_neighbors_if_not_null(const int64_t x_bin,
111  const int64_t y_bin,
112  const int64_t num_bins_radius,
113  std::vector<Z>& neighboring_bins) const;
114  inline std::pair<Z, Z> calculate_slope_and_aspect_of_cell(
115  const std::vector<Z>& neighboring_cells,
116  const bool compute_slope_in_degrees) const;
118  Column<Z>& aspect,
119  const bool compute_slope_in_degrees) const;
120 
122  Column<T>& output_x,
123  Column<T>& output_y,
124  Column<Z>& output_z) const;
125 
127  Column<T>& output_x,
128  Column<T>& output_y,
129  Column<Z>& output_z,
130  const int64_t neighborhood_null_fill_radius) const;
131 };
132 
133 // clang-format off
134 /*
135  UDTF: tf_geo_rasterize__cpu_template(TableFunctionManager,
136  Cursor<Column<T> x, Column<T> y, Column<Z> z> raster,
137  T bin_dim_meters | require="bin_dim_meters > 0", bool geographic_coords,
138  int64_t neighborhood_fill_radius | require="neighborhood_fill_radius >= 0",
139  bool fill_only_nulls) | filter_table_function_transpose=on ->
140  Column<T> x, Column<T> y, Column<Z> z, T=[float, double], Z=[float, double]
141  */
142 // clang-format on
143 
144 template <typename T, typename Z>
145 TEMPLATE_NOINLINE int32_t
147  const Column<T>& input_x,
148  const Column<T>& input_y,
149  const Column<Z>& input_z,
150  const T bin_dim_meters,
151  const bool geographic_coords,
152  const int64_t neighborhood_fill_radius,
153  const bool fill_only_nulls,
154  Column<T>& output_x,
155  Column<T>& output_y,
156  Column<Z>& output_z) {
157  GeoRaster<T, Z> geo_raster(
158  input_x, input_y, input_z, bin_dim_meters, geographic_coords, true);
159 
160  if (neighborhood_fill_radius > 0) {
161  geo_raster.fill_bins_from_neighbors(neighborhood_fill_radius, fill_only_nulls);
162  }
163 
164  return geo_raster.outputDenseColumns(mgr, output_x, output_y, output_z);
165 }
166 
167 // clang-format off
168 /*
169  UDTF: tf_geo_rasterize__cpu_template(TableFunctionManager,
170  Cursor<Column<T> x, Column<T> y, Column<Z> z> raster, T bin_dim_meters | require="bin_dim_meters > 0",
171  bool geographic_coords, int64_t neighborhood_fill_radius | require="neighborhood_fill_radius >= 0", bool fill_only_nulls,
172  T x_min, T x_max | require="x_max > x_min", T y_min, T y_max | require="y_max > y_min") ->
173  Column<T> x, Column<T> y, Column<Z> z, T=[float, double], Z=[float, double]
174  */
175 // clang-format on
176 
177 template <typename T, typename Z>
178 TEMPLATE_NOINLINE int32_t
180  const Column<T>& input_x,
181  const Column<T>& input_y,
182  const Column<Z>& input_z,
183  const T bin_dim_meters,
184  const bool geographic_coords,
185  const int64_t neighborhood_fill_radius,
186  const bool fill_only_nulls,
187  const T x_min,
188  const T x_max,
189  const T y_min,
190  const T y_max,
191  Column<T>& output_x,
192  Column<T>& output_y,
193  Column<Z>& output_z) {
194  if (bin_dim_meters <= 0) {
195  return mgr.ERROR_MESSAGE("bin_dim_meters argument must be greater than 0");
196  }
197 
198  if (neighborhood_fill_radius < 0) {
199  return mgr.ERROR_MESSAGE(
200  "neighborhood_fill_radius argument must be greater than "
201  "or equal to 0");
202  }
203 
204  if (x_min >= x_max) {
205  return mgr.ERROR_MESSAGE("x_min must be less than x_max");
206  }
207 
208  if (y_min >= y_max) {
209  return mgr.ERROR_MESSAGE("y_min must be less than y_max");
210  }
211 
212  GeoRaster<T, Z> geo_raster(input_x,
213  input_y,
214  input_z,
215  bin_dim_meters,
216  geographic_coords,
217  true,
218  x_min,
219  x_max,
220  y_min,
221  y_max);
222 
223  if (neighborhood_fill_radius > 0) {
224  geo_raster.fill_bins_from_neighbors(neighborhood_fill_radius, fill_only_nulls);
225  }
226 
227  return geo_raster.outputDenseColumns(mgr, output_x, output_y, output_z);
228 }
229 // clang-format off
230 /*
231  UDTF: tf_geo_rasterize_slope__cpu_template(TableFunctionManager,
232  Cursor<Column<T> x, Column<T> y, Column<Z> z> raster, T bin_dim_meters | require="bin_dim_meters > 0",
233  bool geographic_coords, int64_t neighborhood_fill_radius | require="neighborhood_fill_radius >= 0", bool fill_only_nulls,
234  bool compute_slope_in_degrees) | filter_table_function_transpose=on ->
235  Column<T> x, Column<T> y, Column<Z> z, Column<Z> slope, Column<Z> aspect, T=[float, double], Z=[float, double]
236  */
237 // clang-format on
238 
239 template <typename T, typename Z>
240 TEMPLATE_NOINLINE int32_t
242  const Column<T>& input_x,
243  const Column<T>& input_y,
244  const Column<Z>& input_z,
245  const T bin_dim_meters,
246  const bool geographic_coords,
247  const int64_t neighborhood_fill_radius,
248  const bool fill_only_nulls,
249  const bool compute_slope_in_degrees,
250  Column<T>& output_x,
251  Column<T>& output_y,
252  Column<Z>& output_z,
253  Column<Z>& output_slope,
254  Column<Z>& output_aspect) {
255  if (bin_dim_meters <= 0) {
256  return mgr.ERROR_MESSAGE("bin_dim_meters argument must be greater than 0");
257  }
258 
259  if (neighborhood_fill_radius < 0) {
260  return mgr.ERROR_MESSAGE(
261  "neighborhood_fill_radius argument must be greater than "
262  "or equal to 0");
263  }
264 
265  GeoRaster<T, Z> geo_raster(
266  input_x, input_y, input_z, bin_dim_meters, geographic_coords, true);
267 
268  if (neighborhood_fill_radius > 0) {
269  geo_raster.fill_bins_from_neighbors(neighborhood_fill_radius, fill_only_nulls);
270  }
271 
272  const size_t output_rows =
273  geo_raster.outputDenseColumns(mgr, output_x, output_y, output_z);
274  geo_raster.calculate_slope_and_aspect(
275  output_slope, output_aspect, compute_slope_in_degrees);
276  return output_rows;
277 }
278 
280 
281 #endif // __CUDACC__
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
int64_t outputDenseColumns(TableFunctionManager &mgr, Column< T > &output_x, Column< T > &output_y, Column< Z > &output_z) const
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
T get_y_bin(const T input) const
void computeParallel(const Column< T2 > &input_x, const Column< T2 > &input_y, const Column< Z2 > &input_z, const size_t max_inputs_per_thread)
T get_x_bin(const T input) const
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)
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
bool is_bin_out_of_bounds(const int64_t source_x_bin, const int64_t source_y_bin) const
bool is_null(const Z value) const
TEMPLATE_NOINLINE int32_t tf_geo_rasterize__cpu_template(TableFunctionManager &mgr, const Column< T > &input_x, const Column< T > &input_y, const Column< Z > &input_z, const T bin_dim_meters, const bool geographic_coords, const int64_t neighborhood_fill_radius, const bool fill_only_nulls, Column< T > &output_x, Column< T > &output_y, Column< Z > &output_z)
std::vector< Z > z_
#define TEMPLATE_NOINLINE
Definition: heavydbTypes.h:49
TEMPLATE_NOINLINE int32_t tf_geo_rasterize_slope__cpu_template(TableFunctionManager &mgr, const Column< T > &input_x, const Column< T > &input_y, const Column< Z > &input_z, const T bin_dim_meters, const bool geographic_coords, const int64_t neighborhood_fill_radius, const bool fill_only_nulls, const bool compute_slope_in_degrees, Column< T > &output_x, Column< T > &output_y, Column< Z > &output_z, Column< Z > &output_slope, Column< Z > &output_aspect)