Skip to content

Commit

Permalink
fix: break out projection wrapper into new component
Browse files Browse the repository at this point in the history
  • Loading branch information
program-- committed Nov 9, 2023
1 parent dacd5ba commit bad989e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 54 deletions.
31 changes: 31 additions & 0 deletions include/geopackage/proj.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef NGEN_GEOPACKAGE_PROJ_HPP
#define NGEN_GEOPACKAGE_PROJ_HPP

#include <boost/geometry/srs/projection.hpp>

namespace ngen {
namespace srs {

namespace bg = boost::geometry;

struct epsg
{
using srs_type = bg::srs::dpar::parameters<double>;

enum {
wgs84 = 4326,
conus_albers = 5070,
mercator = 3857
};

static srs_type get(uint32_t srid);

private:
static const srs_type epsg5070_;
static const srs_type epsg3857_;
};

} // namespace srs
} // namespace ngen

#endif // NGEN_GEOPACKAGE_PROJ_HPP
2 changes: 0 additions & 2 deletions include/geopackage/wkb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ struct wkb {
*/
static geometry read(const boost::span<const uint8_t> buffer);

static bg::srs::dpar::parameters<> get_prj(uint32_t srid);

private:
/**
* Read a WKB point into a cartesian model.
Expand Down
3 changes: 2 additions & 1 deletion src/geopackage/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ if (NOT _cmp)
set_source_files_properties(wkb.cpp PROPERTIES COMPILE_OPTIONS "-O0;-fno-sanitize=all")
endif()

add_library(geopackage geometry.cpp
add_library(geopackage proj.cpp
geometry.cpp
properties.cpp
feature.cpp
read.cpp
Expand Down
7 changes: 4 additions & 3 deletions src/geopackage/geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "geopackage.hpp"
#include "EndianCopy.hpp"
#include "wkb.hpp"
#include "proj.hpp"

geojson::geometry ngen::geopackage::build_geometry(
const ngen::sqlite::database::iterator& row,
Expand All @@ -26,8 +27,8 @@ geojson::geometry ngen::geopackage::build_geometry(
uint32_t srs_id = 0;
utils::copy_from(geometry_blob, index, srs_id, endian);

const auto epsg = wkb::get_prj(srs_id);
const bg::srs::transformation<> prj{epsg, wkb::get_prj(4326)};
const auto epsg = ngen::srs::epsg::get(srs_id);
const bg::srs::transformation<> prj{epsg, ngen::srs::epsg::get(ngen::srs::epsg::wgs84)};
wkb::wgs84 pvisitor{srs_id, prj};

if (indicator > 0 & indicator < 5) {
Expand All @@ -50,7 +51,7 @@ geojson::geometry ngen::geopackage::build_geometry(
geojson::coordinate_t min_prj{};

// project the raw bounding box
if (srs_id == 4326) {
if (srs_id == srs::epsg::wgs84) {
max_prj = geojson::coordinate_t{max.get<0>(), max.get<1>()};
min_prj = geojson::coordinate_t{min.get<0>(), min.get<1>()};
} else {
Expand Down
43 changes: 43 additions & 0 deletions src/geopackage/proj.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "proj.hpp"
#include <boost/geometry/srs/projections/epsg.hpp>

namespace ngen {
namespace srs {

const epsg::srs_type epsg::epsg5070_ =
epsg::srs_type(bg::srs::dpar::proj_aea)
(bg::srs::dpar::ellps_grs80)
(bg::srs::dpar::towgs84, {0,0,0,0,0,0,0})
(bg::srs::dpar::lat_0, 23)
(bg::srs::dpar::lon_0, -96)
(bg::srs::dpar::lat_1, 29.5)
(bg::srs::dpar::lat_2, 45.5)
(bg::srs::dpar::x_0, 0)
(bg::srs::dpar::y_0, 0);

const epsg::srs_type epsg::epsg3857_ =
epsg::srs_type(bg::srs::dpar::proj_merc)
(bg::srs::dpar::units_m)
(bg::srs::dpar::no_defs)
(bg::srs::dpar::a, 6378137)
(bg::srs::dpar::b, 6378137)
(bg::srs::dpar::lat_ts, 0)
(bg::srs::dpar::lon_0, 0)
(bg::srs::dpar::x_0, 0)
(bg::srs::dpar::y_0, 0)
(bg::srs::dpar::k, 1);

auto epsg::get(uint32_t srid) -> srs_type
{
switch (srid) {
case 5070:
return epsg5070_;
case 3857:
return epsg3857_;
default:
return bg::projections::detail::epsg_to_parameters(srid);
}
}

} // namespace srs
} // namespace ngen
55 changes: 7 additions & 48 deletions src/geopackage/wkb.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "wkb.hpp"
#include "proj.hpp"

namespace ngen {
namespace geopackage {
Expand Down Expand Up @@ -197,51 +198,9 @@ typename wkb::geometry wkb::read(const boost::span<const uint8_t> buffer)
// WKB Projection Visitor
// ----------------------------------------------------------------------------

bg::srs::dpar::parameters<> wkb::get_prj(uint32_t srid) {
/**
* EPSG 5070 and 3857 projection definitions for use with boost::geometry.
*
* @note these are required because boost 1.72.0 does not
* have an EPSG definition for 5070 or 3857 in boost::srs::epsg.
*/
const static auto epsg5070 = bg::srs::dpar::parameters<>
(bg::srs::dpar::proj_aea)
(bg::srs::dpar::ellps_grs80)
(bg::srs::dpar::towgs84, {0,0,0,0,0,0,0})
(bg::srs::dpar::lat_0, 23)
(bg::srs::dpar::lon_0, -96)
(bg::srs::dpar::lat_1, 29.5)
(bg::srs::dpar::lat_2, 45.5)
(bg::srs::dpar::x_0, 0)
(bg::srs::dpar::y_0, 0);

const static auto epsg3857 = bg::srs::dpar::parameters<>
(bg::srs::dpar::proj_merc)
(bg::srs::dpar::units_m)
(bg::srs::dpar::no_defs)
(bg::srs::dpar::a, 6378137)
(bg::srs::dpar::b, 6378137)
(bg::srs::dpar::lat_ts, 0)
(bg::srs::dpar::lon_0, 0)
(bg::srs::dpar::x_0, 0)
(bg::srs::dpar::y_0, 0)
(bg::srs::dpar::k, 1);

switch(srid) {
case 5070:
return epsg5070;
case 3857:
return epsg3857;
default:
return bg::projections::detail::epsg_to_parameters(srid);
}
}

// ----------------------------------------------------------------------------

geojson::geometry wkb::wgs84::operator()(point_t& g)
{
if (this->srs == 4326) {
if (this->srs == ngen::srs::epsg::wgs84) {
return geojson::coordinate_t(g.get<0>(), g.get<1>());
}

Expand All @@ -256,7 +215,7 @@ geojson::geometry wkb::wgs84::operator()(linestring_t& g)
{
geojson::linestring_t h;

if (this->srs == 4326) {
if (this->srs == ngen::srs::epsg::wgs84) {
h.reserve(g.size());
for (auto&& gg : g) {
h.emplace_back(
Expand All @@ -276,7 +235,7 @@ geojson::geometry wkb::wgs84::operator()(polygon_t& g)
{
geojson::polygon_t h;

if(this->srs == 4326) {
if(this->srs == ngen::srs::epsg::wgs84) {
h.outer().reserve(g.outer().size());
for (auto&& gg : g.outer()) {
h.outer().emplace_back(
Expand Down Expand Up @@ -309,7 +268,7 @@ geojson::geometry wkb::wgs84::operator()(multipoint_t& g)
{
geojson::multipoint_t h;

if (this->srs == 4326) {
if (this->srs == ngen::srs::epsg::wgs84) {
h.reserve(g.size());
for (auto&& gg : g) {
h.emplace_back(
Expand All @@ -330,7 +289,7 @@ geojson::geometry wkb::wgs84::operator()(multilinestring_t& g)
{
geojson::multilinestring_t h;

if (this->srs == 4326) {
if (this->srs == ngen::srs::epsg::wgs84) {
h.resize(g.size());
auto&& line_g = g.begin();
auto&& line_h = h.begin();
Expand All @@ -352,7 +311,7 @@ geojson::geometry wkb::wgs84::operator()(multipolygon_t& g)
{
geojson::multipolygon_t h;

if (this->srs == 4326) {
if (this->srs == ngen::srs::epsg::wgs84) {
h.resize(g.size());
auto&& polygon_g = g.begin();
auto&& polygon_h = h.begin();
Expand Down

0 comments on commit bad989e

Please sign in to comment.