/cpp/src/render/utils/render_utils.cpp

https://github.com/zilliztech/arctern · C++ · 146 lines · 116 code · 15 blank · 15 comment · 22 complexity · 1a948045ae209bf4b12f1e93bd40aa10 MD5 · raw file

  1. /*
  2. * Copyright (C) 2019-2020 Zilliz. All rights reserved.
  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. #include <ogr_api.h>
  17. #include <ogrsf_frmts.h>
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include <arrow/util/string_view.h>
  22. #include "arrow/render_api.h"
  23. #include "render/utils/render_utils.h"
  24. #include "utils/check_status.h"
  25. namespace arctern {
  26. namespace render {
  27. OGRGeometryUniquePtr GeometryExtraction(nonstd::string_view wkb) {
  28. OGRGeometry* geo = nullptr;
  29. OGRGeometryFactory::createFromWkb(wkb.data(), nullptr, &geo);
  30. return OGRGeometryUniquePtr(geo);
  31. }
  32. std::vector<OGRGeometryUniquePtr> GeometryExtraction(
  33. const std::vector<std::shared_ptr<arrow::Array>>& arrs) {
  34. int total_size = 0;
  35. for (const auto& arr : arrs) {
  36. total_size += arr->length();
  37. }
  38. std::vector<OGRGeometryUniquePtr> geos_res(total_size);
  39. int index = 0;
  40. for (const auto& arr : arrs) {
  41. assert(arr->type_id() == arrow::Type::BINARY);
  42. auto wkb_geometries = std::static_pointer_cast<arrow::BinaryArray>(arr);
  43. for (int i = 0; i < wkb_geometries->length(); i++) {
  44. if (arr->IsNull(i)) {
  45. geos_res[index++] = nullptr;
  46. continue;
  47. }
  48. OGRGeometry* geo = nullptr;
  49. auto err_code = OGRGeometryFactory::createFromWkb(
  50. wkb_geometries->GetString(i).c_str(), nullptr, &geo);
  51. if (err_code || geo == nullptr) {
  52. geos_res[index] = nullptr;
  53. } else {
  54. geos_res[index] = OGRGeometryUniquePtr(geo);
  55. }
  56. index++;
  57. }
  58. }
  59. return geos_res;
  60. }
  61. std::vector<std::string> WkbExtraction(
  62. const std::vector<std::shared_ptr<arrow::Array>>& arrs) {
  63. int total_size = 0;
  64. for (const auto& arr : arrs) {
  65. total_size += arr->length();
  66. }
  67. std::vector<std::string> wkb_res(total_size);
  68. int index = 0;
  69. for (const auto& arr : arrs) {
  70. assert(arr->type_id() == arrow::Type::BINARY);
  71. auto wkb_geometries = std::static_pointer_cast<arrow::BinaryArray>(arr);
  72. for (int i = 0; i < wkb_geometries->length(); i++) {
  73. if (arr->IsNull(i)) {
  74. wkb_res[index++] = "";
  75. continue;
  76. }
  77. wkb_res[index] = wkb_geometries->GetString(i);
  78. index++;
  79. }
  80. }
  81. return wkb_res;
  82. }
  83. std::vector<std::shared_ptr<arrow::Array>> GeometryExport(
  84. const std::vector<OGRGeometryUniquePtr>& geos, int arrays_size) {
  85. int size_per_array = geos.size() / arrays_size;
  86. arrays_size = geos.size() % arrays_size == 0 ? arrays_size : arrays_size + 1;
  87. std::vector<std::shared_ptr<arrow::Array>> arrays(arrays_size);
  88. for (int i = 0; i < arrays_size; i++) {
  89. arrow::BinaryBuilder builder;
  90. for (int j = i * size_per_array; j < geos.size() && j < (i + 1) * size_per_array;
  91. j++) {
  92. if (geos[j] == nullptr) {
  93. CHECK_ARROW(builder.AppendNull());
  94. continue;
  95. }
  96. auto sz = geos[j]->WkbSize();
  97. std::vector<char> str(sz);
  98. auto err_code = geos[j]->exportToWkb(OGRwkbByteOrder::wkbNDR, (uint8_t*)str.data());
  99. if (err_code != OGRERR_NONE) {
  100. std::string err_msg =
  101. "failed to export to wkt, error code = " + std::to_string(err_code);
  102. throw std::runtime_error(err_msg);
  103. }
  104. CHECK_ARROW(builder.Append(str.data(), str.size()));
  105. }
  106. std::shared_ptr<arrow::Array> array;
  107. CHECK_ARROW(builder.Finish(&array));
  108. arrays[i] = array;
  109. }
  110. return arrays;
  111. }
  112. void pointXY_from_wkt_with_transform(const std::string& wkt, double& x, double& y,
  113. void* poCT) {
  114. OGRGeometry* res_geo = nullptr;
  115. CHECK_GDAL(OGRGeometryFactory::createFromWkt(wkt.c_str(), nullptr, &res_geo));
  116. CHECK_GDAL(OGR_G_Transform(res_geo, (OGRCoordinateTransformation*)poCT));
  117. auto rst_pointer = reinterpret_cast<OGRPoint*>(res_geo);
  118. x = rst_pointer->getX();
  119. y = rst_pointer->getY();
  120. OGRGeometryFactory::destroyGeometry(res_geo);
  121. }
  122. void pointXY_from_wkt(const std::string& wkt, double& x, double& y) {
  123. OGRGeometry* res_geo = nullptr;
  124. CHECK_GDAL(OGRGeometryFactory::createFromWkt(wkt.c_str(), nullptr, &res_geo));
  125. auto rst_pointer = reinterpret_cast<OGRPoint*>(res_geo);
  126. x = rst_pointer->getX();
  127. y = rst_pointer->getY();
  128. OGRGeometryFactory::destroyGeometry(res_geo);
  129. }
  130. } // namespace render
  131. } // namespace arctern