OmniSciDB  17c254d2f8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
geo_compression.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 #include "Shared/geo_compression.h"
18 
19 namespace geospatial {
20 
21 uint64_t compress_coord(double coord, const SQLTypeInfo& ti, bool x) {
22  if (ti.get_compression() == kENCODING_GEOINT && ti.get_comp_param() == 32) {
25  }
26  return *reinterpret_cast<uint64_t*>(may_alias_ptr(&coord));
27 }
28 
29 uint64_t compress_null_point(const SQLTypeInfo& ti, bool x) {
30  if (ti.get_compression() == kENCODING_GEOINT && ti.get_comp_param() == 32) {
33  }
34  double n = x ? NULL_ARRAY_DOUBLE : NULL_DOUBLE;
35  auto u = *reinterpret_cast<uint64_t*>(may_alias_ptr(&n));
36  return u;
37 }
38 
39 // Compress non-NULL geo coords; and also NULL POINT coords (special case)
40 std::vector<uint8_t> compress_coords(std::vector<double>& coords, const SQLTypeInfo& ti) {
41  CHECK(!coords.empty()) << "Coord compression received no data";
42  bool is_null_point = false;
43  if (!ti.get_notnull()) {
44  is_null_point = (ti.get_type() == kPOINT && coords[0] == NULL_ARRAY_DOUBLE);
45  }
46  std::vector<uint8_t> compressed_coords;
47  bool x = true;
48  bool is_geoint32 =
49  (ti.get_compression() == kENCODING_GEOINT && ti.get_comp_param() == 32);
50  size_t coord_data_size = (is_geoint32) ? (ti.get_comp_param() / 8) : sizeof(double);
51  for (auto coord : coords) {
52  uint64_t coord_data;
53  if (is_null_point) {
54  coord_data = compress_null_point(ti, x);
55  } else {
56  if (ti.get_output_srid() == 4326) {
57  if (x) {
58  if (coord < -180.0 || coord > 180.0) {
59  throw std::runtime_error("WGS84 longitude " + std::to_string(coord) +
60  " is out of bounds");
61  }
62  } else {
63  if (coord < -90.0 || coord > 90.0) {
64  throw std::runtime_error("WGS84 latitude " + std::to_string(coord) +
65  " is out of bounds");
66  }
67  }
68  }
69  if (is_geoint32) {
70  coord_data = compress_coord(coord, ti, x);
71  } else {
72  auto coord_data_ptr = reinterpret_cast<uint64_t*>(&coord);
73  coord_data = *coord_data_ptr;
74  }
75  }
76  for (size_t i = 0; i < coord_data_size; i++) {
77  compressed_coords.push_back(coord_data & 0xFF);
78  coord_data >>= 8;
79  }
80  x = !x;
81  }
82  return compressed_coords;
83 }
84 
85 } // namespace geospatial
#define NULL_DOUBLE
Definition: sqltypes.h:185
uint64_t compress_null_point(const SQLTypeInfo &ti, bool x)
#define NULL_ARRAY_DOUBLE
Definition: sqltypes.h:193
DEVICE constexpr uint64_t compress_null_point_lattitude_geoint32()
bool is_null_point(const SQLTypeInfo &geo_ti, const int8_t *coords, const size_t coords_sz)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:258
std::string to_string(char const *&&v)
CHECK(cgen_state)
DEVICE uint64_t compress_longitude_coord_geoint32(const double coord)
DEVICE constexpr uint64_t compress_null_point_longitude_geoint32()
DEVICE uint64_t compress_lattitude_coord_geoint32(const double coord)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:266
uint64_t compress_coord(double coord, const SQLTypeInfo &ti, bool x)
std::vector< uint8_t > compress_coords(std::vector< double > &coords, const SQLTypeInfo &ti)
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:267
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:265
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:264