Skip to content
Prev 20079 / 29559 Next

a projection question

First thought is to try and project a sample point and catch any error
messages, but you can't be sure that your sample point is valid in the
given coordinate system.

Second thought was to look at the proj4 and rgdal packages to see if
they implemented checking of PROJ.4 strings. Couldn't see it.

So third thought was to look at the PROJ.4 API and see what its got:

http://trac.osgeo.org/proj/wiki/pj_init_plus

which is a C function to initialise a projection. So I wrote a little wrapper:

#include <proj_api.h>

void ptest(char *proj[], int *status){
  projPJ out;
  if(!(out = pj_init_plus(proj[0]))){
    *status = 1;
  }else{
    *status = 0;
   /* need to pj_free the out object here... I think... */
  }
}

with an R wrapper: isValidProjection <-
function(s){.C("ptest",s,status=as.integer(-1))$status==0}

compiled it, linked with libproj.so and tested it:
[1] TRUE

- user typo:
[1] FALSE

- try more than +init strings:
[1] TRUE

- some bogus EPSG codes:
[1] FALSE
[1] FALSE

Looks like it works. The tricky part of all this is linking the C code
with libproj.so which I did by doing:

R CMD SHLIB projtest.c /usr/lib/libproj.so

on the command line. Might work for you, but it might be worth asking
Roger, as maintainer or rgdal, or Smon Urbanek as maintainer of
project, to add it as a top-level R function.

 Maybe I'll check out the rgdal source and push a change for Roger...

Barry
On Sat, Dec 21, 2013 at 10:19 PM, Hodgess, Erin <HodgessE at uhd.edu> wrote: