Skip to content

Commit

Permalink
handle axis order; #1033
Browse files Browse the repository at this point in the history
This is all exploration; I'm not sure this, or anything, is needed at all.
  • Loading branch information
edzer committed Apr 22, 2019
1 parent 8ee3aae commit 8c022d5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
#include "wkb.h"
#include "gdal_sf_pkg.h"

#if GDAL_VERSION_MAJOR == 2
# if GDAL_VERSION_MINOR >= 5
# define HAVE250
# endif
#else
# if GDAL_VERSION_MAJOR > 2
# define HAVE250
# endif
#endif

//
// Returns errors to R
// Note only case 4 actually returns immediately
Expand Down Expand Up @@ -111,6 +121,7 @@ Rcpp::List CPL_crs_parameters(std::string p4s) {
Rcpp::List out(7);
OGRErr Err;
OGRSpatialReference *srs = new OGRSpatialReference;
srs = handle_axis_order(srs);
handle_error(srs->importFromProj4(p4s.c_str()));
out(0) = Rcpp::NumericVector::create(srs->GetSemiMajor());
out(1) = Rcpp::NumericVector::create(srs->GetSemiMinor());
Expand Down Expand Up @@ -138,8 +149,10 @@ Rcpp::List CPL_crs_parameters(std::string p4s) {
Rcpp::LogicalVector CPL_crs_equivalent(std::string crs1, std::string crs2) {
Rcpp::LogicalVector out(1);
OGRSpatialReference *srs1 = new OGRSpatialReference;
srs1 = handle_axis_order(srs1);
handle_error(srs1->importFromProj4(crs1.c_str()));
OGRSpatialReference *srs2 = new OGRSpatialReference;
srs2 = handle_axis_order(srs2);
handle_error(srs2->importFromProj4(crs2.c_str()));
out(0) = (bool) srs1->IsSame(srs2);
delete srs1;
Expand All @@ -158,6 +171,7 @@ std::vector<OGRGeometry *> ogr_from_sfc(Rcpp::List sfc, OGRSpatialReference **sr
if (p4s != NA_STRING) {
Rcpp::CharacterVector cv = crs["proj4string"];
local_srs = new OGRSpatialReference;
local_srs = handle_axis_order(local_srs);
OGRErr err = local_srs->importFromProj4(cv[0]);
if (err != OGRERR_NONE) {
local_srs->Release(); // #nocov
Expand Down Expand Up @@ -272,6 +286,7 @@ Rcpp::List sfc_from_ogr(std::vector<OGRGeometry *> g, bool destroy = false) {
// [[Rcpp::export]]
Rcpp::List CPL_crs_from_epsg(int epsg) {
OGRSpatialReference ref;
handle_axis_order(&ref);
if (ref.importFromEPSG(epsg) == OGRERR_NONE)
return get_crs(&ref);
else
Expand All @@ -282,6 +297,7 @@ Rcpp::List CPL_crs_from_epsg(int epsg) {
Rcpp::List CPL_crs_from_wkt(Rcpp::CharacterVector wkt) {
char *cp = wkt[0];
OGRSpatialReference ref;
handle_axis_order(&ref);
#if GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR <= 2
handle_error(ref.importFromWkt(&cp));
#else
Expand Down Expand Up @@ -359,6 +375,7 @@ Rcpp::List CPL_transform(Rcpp::List sfc, Rcpp::CharacterVector proj4) {

// import proj4string:
OGRSpatialReference *dest = new OGRSpatialReference;
dest = handle_axis_order(dest);
handle_error(dest->importFromProj4((const char *) (proj4[0])));

// transform geometries:
Expand Down Expand Up @@ -410,6 +427,7 @@ Rcpp::List CPL_wrap_dateline(Rcpp::List sfc, Rcpp::CharacterVector opt, bool qui
// [[Rcpp::export]]
Rcpp::List CPL_crs_from_proj4string(Rcpp::CharacterVector p4s) {
OGRSpatialReference ref;
handle_axis_order(&ref);
if (ref.importFromProj4(p4s[0]) == OGRERR_NONE)
return get_crs(&ref);
else {
Expand Down Expand Up @@ -470,3 +488,15 @@ Rcpp::LogicalVector CPL_gdal_with_geos() {
bool withGEOS = OGRGeometryFactory::haveGEOS();
return Rcpp::LogicalVector::create(withGEOS);
}

OGRSpatialReference *handle_axis_order(OGRSpatialReference *sr, bool traditional) {
#ifdef HAVE250
if (sr != NULL) {
if (traditional)
sr->SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
else
sr->SetAxisMappingStrategy(OAMS_AUTHORITY_COMPLIANT);
}
#endif
return sr;
}
1 change: 1 addition & 0 deletions src/gdal.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
void set_error_handler(void);
void unset_error_handler(void);
OGRSpatialReference *handle_axis_order(OGRSpatialReference *sr, bool traditional = true);
1 change: 1 addition & 0 deletions src/gdal_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ int CPL_write_ogr(Rcpp::List obj, Rcpp::CharacterVector dsn, Rcpp::CharacterVect
// read geometries:
OGRSpatialReference *sref = NULL;
std::vector<OGRGeometry *> geomv = ogr_from_sfc(geom, &sref);
sref = handle_axis_order(sref);

// create layer:
options = create_options(lco, quiet);
Expand Down
1 change: 1 addition & 0 deletions src/polygonize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Rcpp::List CPL_polygonize(Rcpp::CharacterVector raster, Rcpp::CharacterVector ma
Rcpp::stop("Creation failed.\n"); // #nocov
}
OGRSpatialReference *sr = new OGRSpatialReference;
sr = handle_axis_order(sr);
char **ppt = (char **) &wkt;
#if GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR <= 2
sr->importFromWkt(ppt);
Expand Down
2 changes: 2 additions & 0 deletions src/stars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <Rcpp.h>

#include "gdal.h"
#include "gdal_read.h"
#include "gdal_sf_pkg.h"

Expand Down Expand Up @@ -229,6 +230,7 @@ List CPL_read_gdal(CharacterVector fname, CharacterVector options, CharacterVect
proj = CharacterVector::create(wkt);
// proj4string:
OGRSpatialReference *sr = new OGRSpatialReference;
sr = handle_axis_order(sr);
char **ppt = (char **) &wkt;
#if GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR <= 2
sr->importFromWkt(ppt);
Expand Down

0 comments on commit 8c022d5

Please sign in to comment.