Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce crs input as necessary #55

Merged
merged 2 commits into from
Mar 19, 2024
Merged

Conversation

anthonynorth
Copy link
Collaborator

Reverts strict crs type requirement for proj strings.

Fixes #52

@anthonynorth anthonynorth requested a review from mdsumner March 9, 2024 07:45
@anthonynorth
Copy link
Collaborator Author

anthonynorth commented Mar 9, 2024

Should we notify when crs input is changed? I.e. if crs is a proj string, but +type=crs is absent, should we message() / warning()?

Should I move this logic to c?

Edit: a working c implementation below. If we're certain proj strings are always small, we could stack allocate the new_crs buffer.

static bool str_starts(const char* str, const char* pre) {
  return strncmp(pre, str, strlen(pre)) == 0;
}

static bool str_contains(const char* str, const char* test) {
  return strstr(str, test) != NULL;
}

PJ* proj_create_crs(PJ_CONTEXT* ctx, const char* crs) {
  if ((str_starts(crs, "proj=") || str_starts(crs, "+proj=") ||
       str_starts(crs, "+init=") || str_starts(crs, "+title=")) &&
      !str_contains(crs, "type=crs")) {

    const char* type = " +type=crs";
    size_t sz = strlen(crs) + strlen(type) + 1;
    char* new_crs = malloc(sz);

    snprintf(new_crs, sz, "%s%s", crs, type);
    PJ* crs_obj = proj_create(ctx, new_crs);
    free(new_crs);

    return crs_obj;
  }

  return proj_create(ctx, crs);
}

// with usage : proj_create_crs(ctx, crs_str);

Copy link
Member

@mdsumner mdsumner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! fixes the problem I had

@anthonynorth anthonynorth merged commit 7f4c4e7 into hypertidy:main Mar 19, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Breaking change: proj_trans_create() doesn't accept bad input CRS
2 participants