Skip to content

Commit 02d7d7d

Browse files
authored
Merge pull request #1629 from joto/fix-proj6-projection
Fix reprojection when using Proj version 6 and above
2 parents b443520 + 5b53259 commit 02d7d7d

2 files changed

Lines changed: 24 additions & 23 deletions

File tree

src/reprojection-generic-proj6.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,10 @@ class generic_reprojection_t : public reprojection
1515
{
1616
public:
1717
explicit generic_reprojection_t(int srs)
18-
: m_target_srs(srs), m_context(proj_context_create())
19-
{
20-
assert(m_context);
21-
22-
m_transformation = create_transformation(PROJ_LATLONG, srs);
23-
24-
m_transformation.reset(proj_normalize_for_visualization(
25-
m_context.get(), m_transformation.get()));
26-
27-
if (!m_transformation) {
28-
throw std::runtime_error{
29-
"Invalid projection '{}': {}"_format(srs, errormsg())};
30-
}
31-
32-
m_transformation_tile = create_transformation(PROJ_SPHERE_MERC, srs);
33-
}
18+
: m_target_srs(srs), m_context(proj_context_create()),
19+
m_transformation(create_transformation(PROJ_LATLONG, srs)),
20+
m_transformation_tile(create_transformation(srs, PROJ_SPHERE_MERC))
21+
{}
3422

3523
osmium::geom::Coordinates reproject(osmium::Location loc) const override
3624
{
@@ -71,6 +59,8 @@ class generic_reprojection_t : public reprojection
7159
std::unique_ptr<PJ, pj_deleter_t> create_transformation(int from,
7260
int to) const
7361
{
62+
assert(m_context);
63+
7464
std::string const source = "epsg:{}"_format(from);
7565
std::string const target = "epsg:{}"_format(to);
7666

@@ -82,7 +72,17 @@ class generic_reprojection_t : public reprojection
8272
"Invalid projection from {} to {}: {}"_format(from, to,
8373
errormsg())};
8474
}
85-
return trans;
75+
76+
std::unique_ptr<PJ, pj_deleter_t> trans_vis{
77+
proj_normalize_for_visualization(m_context.get(), trans.get())};
78+
79+
if (!trans_vis) {
80+
throw std::runtime_error{
81+
"Invalid projection from {} to {}: {}"_format(from, to,
82+
errormsg())};
83+
}
84+
85+
return trans_vis;
8686
}
8787

8888
osmium::geom::Coordinates transform(PJ *transformation,
@@ -92,8 +92,8 @@ class generic_reprojection_t : public reprojection
9292
PJ_COORD c_in;
9393
c_in.lpzt.z = 0.0;
9494
c_in.lpzt.t = HUGE_VAL;
95-
c_in.lpzt.lam = osmium::geom::deg_to_rad(coords.x);
96-
c_in.lpzt.phi = osmium::geom::deg_to_rad(coords.y);
95+
c_in.lpzt.lam = coords.x;
96+
c_in.lpzt.phi = coords.y;
9797

9898
auto const c_out = proj_trans(transformation, PJ_FWD, c_in);
9999

tests/test-reprojection.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <catch.hpp>
1111

12+
#include "config.h"
1213
#include "reprojection.hpp"
1314

1415
TEST_CASE("projection 4326", "[NoDB]")
@@ -48,18 +49,18 @@ TEST_CASE("projection 3857", "[NoDB]")
4849
}
4950

5051
#ifdef HAVE_GENERIC_PROJ
51-
TEST_CASE("projection 5520", "[NoDB]")
52+
TEST_CASE("projection 5651", "[NoDB]")
5253
{
5354
osmium::Location const loc{10.0, 53.0};
54-
int const srs = 5520; // DHDN / 3-degree Gauss-Kruger zone 1
55+
int const srs = 5651; // ETRS89 / UTM zone 31N (N-zE)
5556

5657
auto const reprojection = reprojection::create_projection(srs);
5758
REQUIRE(reprojection->target_srs() == srs);
5859
REQUIRE_FALSE(reprojection->target_latlon());
5960

6061
auto const c = reprojection->reproject(loc);
61-
REQUIRE(c.x == Approx(1969644.93));
62-
REQUIRE(c.y == Approx(5897146.04));
62+
REQUIRE(c.x == Approx(31969448.78));
63+
REQUIRE(c.y == Approx(5895222.39));
6364

6465
auto const ct = reprojection->target_to_tile(c);
6566
REQUIRE(ct.x == Approx(1113194.91));

0 commit comments

Comments
 (0)