Loading...
Searching...
No Matches
convert_coordinates.hpp
1/*
2 * Copyright (c) 2023, NVIDIA CORPORATION.
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#include <proj.h>
20
21#include <algorithm>
22#include <type_traits>
23
24namespace cuproj_test {
25
26// Convert coordinates from a x-y struct to a PJ_COORD struct or vice versa
27template <typename InVector, typename OutVector>
28void convert_coordinates(InVector const& in, OutVector& out)
29{
30 using in_coord_type = typename InVector::value_type;
31 using out_coord_type = typename OutVector::value_type;
32
33 static_assert(
34 (std::is_same_v<out_coord_type, PJ_COORD> != std::is_same_v<in_coord_type, PJ_COORD>),
35 "Invalid coordinate vector conversion");
36
37 if constexpr (std::is_same_v<in_coord_type, PJ_COORD>) {
38 using T = typename out_coord_type::value_type;
39 auto proj_coord_to_coordinate = [](auto const& c) {
40 return out_coord_type{static_cast<T>(c.xy.x), static_cast<T>(c.xy.y)};
41 };
42 std::transform(in.begin(), in.end(), out.begin(), proj_coord_to_coordinate);
43 } else if constexpr (std::is_same_v<out_coord_type, PJ_COORD>) {
44 auto coordinate_to_proj_coord = [](auto const& c) { return PJ_COORD{c.x, c.y, 0, 0}; };
45 std::transform(in.begin(), in.end(), out.begin(), coordinate_to_proj_coord);
46 }
47}
48
49} // namespace cuproj_test