OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeoRaster.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 MapD Technologies, 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 #ifdef HAVE_SYSTEM_TFS
20 #ifndef __CUDACC__
21 
22 #include <vector>
24 
25 template <typename T, typename Z>
26 struct GeoRaster {
27  const T bin_dim_meters_;
28  const bool geographic_coords_;
29  const Z null_sentinel_;
30  std::vector<Z> z_;
31  T x_min_;
32  T x_max_;
33  T y_min_;
34  T y_max_;
35  T x_range_;
36  T y_range_;
37  T x_meters_per_degree_;
38  T y_meters_per_degree_;
39  int64_t num_x_bins_;
40  int64_t num_y_bins_;
41  int64_t num_bins_;
42  T x_scale_input_to_bin_;
43  T y_scale_input_to_bin_;
44  T x_scale_bin_to_input_;
45  T y_scale_bin_to_input_;
46 
47  template <typename T2, typename Z2>
48  GeoRaster(const Column<T2>& input_x,
49  const Column<T2>& input_y,
50  const Column<Z2>& input_z,
51  const double bin_dim_meters,
52  const bool geographic_coords,
53  const bool align_bins_to_zero_based_grid);
54 
55  template <typename T2, typename Z2>
56  GeoRaster(const Column<T2>& input_x,
57  const Column<T2>& input_y,
58  const Column<Z2>& input_z,
59  const double bin_dim_meters,
60  const bool geographic_coords,
61  const bool align_bins_to_zero_based_grid,
62  const T x_min,
63  const T x_max,
64  const T y_min,
65  const T y_max);
66 
67  inline T get_x_bin(const T input) const {
68  return (input - x_min_) * x_scale_input_to_bin_;
69  }
70 
71  inline T get_y_bin(const T input) const {
72  return (input - y_min_) * y_scale_input_to_bin_;
73  }
74 
75  inline bool is_null(const Z value) const { return value == null_sentinel_; }
76 
77  inline bool is_bin_out_of_bounds(const int64_t source_x_bin,
78  const int64_t source_y_bin) const {
79  return (source_x_bin < 0 || source_x_bin >= num_x_bins_ || source_y_bin < 0 ||
80  source_y_bin >= num_y_bins_);
81  }
82 
83  inline Z offset_source_z_from_raster_z(const int64_t source_x_bin,
84  const int64_t source_y_bin,
85  const Z source_z_offset) const;
86 
87  inline Z fill_bin_from_avg_neighbors(const int64_t x_centroid_bin,
88  const int64_t y_centroid_bin,
89  const int64_t bins_radius) const;
90 
91  void align_bins_max_inclusive();
92 
93  void align_bins_max_exclusive();
94 
95  void calculate_bins_and_scales();
96 
97  template <typename T2, typename Z2>
98  void compute(const Column<T2>& input_x,
99  const Column<T2>& input_y,
100  const Column<Z2>& input_z);
101 
102  int64_t outputDenseColumns(TableFunctionManager& mgr,
103  Column<T>& output_x,
104  Column<T>& output_y,
105  Column<Z>& output_z,
106  const int64_t neighborhood_null_fill_radius) const;
107 };
108 
109 // clang-format off
110 /*
111  UDTF: tf_geo_rasterize__cpu_template(TableFunctionManager, Cursor<Column<T> x, Column<T> y, Column<Z> z>, T, bool, int64_t) | filter_table_function_transpose=on -> Column<T> x, Column<T> y, Column<Z> z, T=[float, double], Z=[float, double]
112  */
113 // clang-format on
114 
115 template <typename T, typename Z>
116 TEMPLATE_NOINLINE int32_t
117 tf_geo_rasterize__cpu_template(TableFunctionManager& mgr,
118  const Column<T>& input_x,
119  const Column<T>& input_y,
120  const Column<Z>& input_z,
121  const T bin_dim_meters,
122  const bool geographic_coords,
123  const int64_t null_neighborhood_fill_radius,
124  Column<T>& output_x,
125  Column<T>& output_y,
126  Column<Z>& output_z) {
127  if (bin_dim_meters <= 0) {
128  return mgr.ERROR_MESSAGE("bin_dim_meters argument must be greater than 0");
129  }
130 
131  if (null_neighborhood_fill_radius < 0) {
132  return mgr.ERROR_MESSAGE(
133  "null_neighborhood_fill_radius argument must be greater than "
134  "or equal to 0");
135  }
136 
137  GeoRaster<T, Z> geo_raster(
138  input_x, input_y, input_z, bin_dim_meters, geographic_coords, true);
139  return geo_raster.outputDenseColumns(
140  mgr, output_x, output_y, output_z, null_neighborhood_fill_radius);
141 }
142 
143 // clang-format off
144 /*
145  UDTF: tf_geo_rasterize__cpu_template(TableFunctionManager, Cursor<Column<T>, Column<T>, Column<Z>>, T, bool, int64_t, T, T, T, T) -> Column<T> x, Column<T> y, Column<Z> z, T=[float, double], Z=[float, double]
146  */
147 // clang-format on
148 
149 template <typename T, typename Z>
150 TEMPLATE_NOINLINE int32_t
151 tf_geo_rasterize__cpu_template(TableFunctionManager& mgr,
152  const Column<T>& input_x,
153  const Column<T>& input_y,
154  const Column<Z>& input_z,
155  const T bin_dim_meters,
156  const bool geographic_coords,
157  const int64_t null_neighborhood_fill_radius,
158  const T x_min,
159  const T x_max,
160  const T y_min,
161  const T y_max,
162  Column<T>& output_x,
163  Column<T>& output_y,
164  Column<Z>& output_z) {
165  if (bin_dim_meters <= 0) {
166  return mgr.ERROR_MESSAGE("bin_dim_meters argument must be greater than 0");
167  }
168 
169  if (null_neighborhood_fill_radius < 0) {
170  return mgr.ERROR_MESSAGE(
171  "null_neighborhood_fill_radius argument must be greater than "
172  "or equal to 0");
173  }
174 
175  if (x_min >= x_max) {
176  return mgr.ERROR_MESSAGE("x_min must be less than x_max");
177  }
178 
179  if (y_min >= y_max) {
180  return mgr.ERROR_MESSAGE("y_min must be less than y_max");
181  }
182 
183  GeoRaster<T, Z> geo_raster(input_x,
184  input_y,
185  input_z,
186  bin_dim_meters,
187  geographic_coords,
188  true,
189  x_min,
190  x_max,
191  y_min,
192  y_max);
193  return geo_raster.outputDenseColumns(
194  mgr, output_x, output_y, output_z, null_neighborhood_fill_radius);
195 }
196 
197 #include "GeoRaster.cpp"
198 
199 #endif // __CUDACC__
200 #endif // HAVE_SYSTEM_TFS
#define TEMPLATE_NOINLINE
Definition: OmniSciTypes.h:30
CONSTEXPR DEVICE bool is_null(const T &value)