Skip to content

Commit

Permalink
Merge pull request #51 from anthonynorth/main
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonynorth authored Jan 20, 2024
2 parents 0941eb2 + b5f04b4 commit 2b34a16
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 63 deletions.
36 changes: 0 additions & 36 deletions src/proj-context.c

This file was deleted.

7 changes: 0 additions & 7 deletions src/proj-context.h

This file was deleted.

20 changes: 18 additions & 2 deletions src/proj-utils.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>
#include <proj.h>
#include <stdbool.h>

Expand All @@ -10,8 +13,7 @@
#define PJ_TYPE_PARAMETRIC_DATUM 27
#endif

const char *proj_type_name(PJ_TYPE type)
{
const char* proj_type_name(PJ_TYPE type) {
// clang-format off
switch (type) {
case PJ_TYPE_UNKNOWN: return "Unknown";
Expand Down Expand Up @@ -57,3 +59,17 @@ const char *proj_type_name(PJ_TYPE type)
}
// clang-format on
}

#if PROJ_VERSION_MAJOR < 8
static const char* proj_context_errno_string(PJ_CONTEXT* ctx, int err) {
// deprecated in proj 8
return proj_errno_string(err);
}
#endif

void stop_proj_error(PJ_CONTEXT* ctx) {
int err = proj_context_errno(ctx);
const char* msg = proj_context_errno_string(ctx, err);

Rf_error("%s", msg);
}
1 change: 1 addition & 0 deletions src/proj-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
#include <proj.h>

const char* proj_type_name(PJ_TYPE type);
void stop_proj_error(PJ_CONTEXT* ctx);
40 changes: 22 additions & 18 deletions src/wk-trans.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <stdio.h>
#include "wk-v1.h"

#include "proj-context.h"
#include "proj-utils.h"
#include "r-utils.h"

typedef struct {
PJ_CONTEXT* context;
PJ* transformer;
PJ* source_crs;
PJ* target_crs;
Expand Down Expand Up @@ -63,6 +63,7 @@ static void finalize(void* trans_data) {
if (data->transformer != NULL) proj_destroy(data->transformer);
if (data->source_crs != NULL) proj_destroy(data->source_crs);
if (data->target_crs != NULL) proj_destroy(data->target_crs);
if (data->context != NULL) proj_context_destroy(data->context);

free(data);
}
Expand Down Expand Up @@ -98,31 +99,32 @@ SEXP C_proj_trans_create(SEXP source_crs, SEXP target_crs, SEXP use_z, SEXP use_
SEXP trans_xptr = PROTECT(wk_trans_create_xptr(trans, R_NilValue, R_NilValue));

data->direction = PJ_FWD;
data->context = proj_context_create();

data->source_crs =
proj_create(PJ_DEFAULT_CTX, Rf_translateCharUTF8(STRING_ELT(source_crs, 0)));
proj_create(data->context, Rf_translateCharUTF8(STRING_ELT(source_crs, 0)));
if (data->source_crs == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // #nocov
stop_proj_error(data->context); // #nocov
}

data->target_crs =
proj_create(PJ_DEFAULT_CTX, Rf_translateCharUTF8(STRING_ELT(target_crs, 0)));
proj_create(data->context, Rf_translateCharUTF8(STRING_ELT(target_crs, 0)));
if (data->target_crs == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // #nocov
stop_proj_error(data->context); // #nocov
}

PJ* transformer = proj_create_crs_to_crs_from_pj(PJ_DEFAULT_CTX, data->source_crs,
PJ* transformer = proj_create_crs_to_crs_from_pj(data->context, data->source_crs,
data->target_crs, NULL, NULL);
if (transformer == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // #nocov
stop_proj_error(data->context); // #nocov
}

// always lon,lat
data->transformer = proj_normalize_for_visualization(PJ_DEFAULT_CTX, transformer);
data->transformer = proj_normalize_for_visualization(data->context, transformer);
proj_destroy(transformer);

if (data->transformer == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // # nocov
stop_proj_error(data->context); // # nocov
}

// xptrs
Expand Down Expand Up @@ -151,19 +153,21 @@ SEXP C_proj_trans_inverse(SEXP trans_xptr) {
// reverse
data_inv->direction = -data_fwd->direction;

data_inv->source_crs = proj_clone(PJ_DEFAULT_CTX, data_fwd->source_crs);
data_inv->context = proj_context_create();

data_inv->source_crs = proj_clone(data_inv->context, data_fwd->source_crs);
if (data_inv->source_crs == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // # nocov
stop_proj_error(data_inv->context); // # nocov
}

data_inv->target_crs = proj_clone(PJ_DEFAULT_CTX, data_fwd->target_crs);
data_inv->target_crs = proj_clone(data_inv->context, data_fwd->target_crs);
if (data_inv->target_crs == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // # nocov
stop_proj_error(data_inv->context); // # nocov
}

data_inv->transformer = proj_clone(PJ_DEFAULT_CTX, data_fwd->transformer);
data_inv->transformer = proj_clone(data_inv->context, data_fwd->transformer);
if (data_inv->transformer == NULL) {
stop_proj_error(PJ_DEFAULT_CTX); // # nocov
stop_proj_error(data_inv->context); // # nocov
}

// xptr
Expand Down Expand Up @@ -232,9 +236,9 @@ SEXP C_proj_trans_info(SEXP trans_xptr) {
SET_VECTOR_ELT(res, 1, r_scalar_string(info.id));
SET_VECTOR_ELT(res, 2, r_scalar_string(info.description));
SET_VECTOR_ELT(res, 3, r_scalar_string(info.definition));
SET_VECTOR_ELT(res, 4, proj_area_of_use_info(PJ_DEFAULT_CTX, data->transformer));
SET_VECTOR_ELT(res, 5, proj_crs_info(PJ_DEFAULT_CTX, source_crs));
SET_VECTOR_ELT(res, 6, proj_crs_info(PJ_DEFAULT_CTX, target_crs));
SET_VECTOR_ELT(res, 4, proj_area_of_use_info(data->context, data->transformer));
SET_VECTOR_ELT(res, 5, proj_crs_info(data->context, source_crs));
SET_VECTOR_ELT(res, 6, proj_crs_info(data->context, target_crs));

UNPROTECT(1);
return res;
Expand Down

0 comments on commit 2b34a16

Please sign in to comment.