|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Jaeyoung Lim, ASL, ETH Zurich, Switzerland |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in |
| 13 | + * the documentation and/or other materials provided with the |
| 14 | + * distribution. |
| 15 | + * 3. Neither the name terrain-navigation nor the names of its contributors may be |
| 16 | + * used to endorse or promote products derived from this software |
| 17 | + * without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 26 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + * POSSIBILITY OF SUCH DAMAGE. |
| 31 | + * |
| 32 | + ****************************************************************************/ |
| 33 | + |
| 34 | +#include "grid_map_geo/transform.hpp" |
| 35 | + |
| 36 | +#if __APPLE__ |
| 37 | +#include <gdal.h> |
| 38 | +#include <gdal_priv.h> |
| 39 | +#include <ogr_p.h> |
| 40 | +#include <ogr_spatialref.h> |
| 41 | +#else |
| 42 | +#include <gdal/gdal.h> |
| 43 | +#include <gdal/gdal_priv.h> |
| 44 | +#include <gdal/ogr_p.h> |
| 45 | +#include <gdal/ogr_spatialref.h> |
| 46 | +#endif |
| 47 | + |
| 48 | +Eigen::Vector3d transformCoordinates(ESPG src_coord, ESPG tgt_coord, const Eigen::Vector3d source_coordinates) { |
| 49 | + OGRSpatialReference source, target; |
| 50 | + source.importFromEPSG(static_cast<int>(src_coord)); |
| 51 | + target.importFromEPSG(static_cast<int>(tgt_coord)); |
| 52 | + |
| 53 | + OGRPoint p; |
| 54 | + p.setX(source_coordinates(0)); |
| 55 | + p.setY(source_coordinates(1)); |
| 56 | + p.setZ(source_coordinates(2)); |
| 57 | + p.assignSpatialReference(&source); |
| 58 | + |
| 59 | + p.transformTo(&target); |
| 60 | + Eigen::Vector3d target_coordinates(p.getX(), p.getY(), p.getZ()); |
| 61 | + return target_coordinates; |
| 62 | +} |
| 63 | + |
| 64 | +Eigen::Vector3d transformCoordinates(ESPG src_coord, const std::string wkt, const Eigen::Vector3d source_coordinates) { |
| 65 | + OGRSpatialReference source, target; |
| 66 | + char* wkt_string = const_cast<char*>(wkt.c_str()); |
| 67 | + source.importFromEPSG(static_cast<int>(src_coord)); |
| 68 | + target.importFromWkt(&wkt_string); |
| 69 | + |
| 70 | + OGRPoint p; |
| 71 | + p.setX(source_coordinates(0)); |
| 72 | + p.setY(source_coordinates(1)); |
| 73 | + p.setZ(source_coordinates(2)); |
| 74 | + p.assignSpatialReference(&source); |
| 75 | + |
| 76 | + p.transformTo(&target); |
| 77 | + Eigen::Vector3d target_coordinates(p.getX(), p.getY(), p.getZ()); |
| 78 | + return target_coordinates; |
| 79 | +} |
0 commit comments