[R-pkg-devel] how to call PROJ.4 C code in a package?

Roger Bivand Roger.Bivand at nhh.no
Wed May 27 08:36:03 CEST 2015


On Tue, 26 May 2015, Daniel Kelley wrote:

> Roger,
>
> I plan to make the next version of oce use rgdal::project() to do its 
> work. The idea of using an external PROJ.4 is fine in my own work, but 
> oce has a lot of users who are unaccustomed to building and installing 
> external libraries. Also, many work at institutes that do not permit 
> this kind of system modification. So, for oce I had to make a choice of 
> including the C sources or using another package.

Dan,

(while this might seem very specialised, it's actually an example of a 
typical package development problem, that of accessing facilities in one 
package from another).

I suggest using rgdal::rawTransform() instead of rgdal::project(), because 
it calls the PROJ.4 pj_transform, rather than pj_fwd or pj_inv, and so 
permitting datum transformation. rgdal::rawTransform() was contributed to 
rgdal by Robert Hijmans to make it easier to write code in the raster 
package using rgdal facilities; it is in rgdal/R/wrappers.R. Look at how 
Robert handles the import from the rgdal NAMESPACE into raster.

Roger

PS. I'm afraid that there isn't an easy solution to the wrap-around on the 
back of the globe - most of the fixes involve breaking geometries on the 
"other" side to work around the problem.

>
> At first I felt the proj4 R package to be a natural candidate. However, 
> it produces an error when any point in a lon-lat vector is “offscale”, 
> instead of inserting NA for the projected value. Indexing over a world 
> coastline is too slow for practical work.
>
> Over the weekend I set up a test version of oce to use rgdal::project(), 
> and it’s early days but things seem to be working well.
>
> I need to study the “sp” package in more detail.  Maybe I’ll find the 
> solution to a problem “oce” has when lines in a coastal polygon “cross 
> over” the edge of the earth. This produces spurious domain-crossing 
> lines when e.g. lon_0 is set to put the Pacific at the centre (a common 
> choice of oceanographers).
>
> Thanks VERY much for your detailed and helpful answer.  And thanks for 
> your work on “rgdal” and “sp”, two very fine packages.
>
> — Dan.
>
> PS. glad to see more software going to github.
>
> Dan Kelley
> Oceanography Department
> Dalhousie University
> Halifax, NS, Canada
>
>> On May 26, 2015, at 4:14 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>>
>> Dan,
>>
>> I'm sorry to be late to the party (one is offline occasionally), but all the advice given so far is at best only partially correct.
>>
>> Use of PROJ.4 is crucially conditional on access to PROJ_LIB a shell variable setting the address of metadata files not included in the C source. PROJ.4 without this access is very limited, not least because the EPSG listing of projections is not available, not to mention a small file of default values that recently floored rgdal and other packages using its PROJ.4 facilities (PROJ.4 4.9.1 omitted the file by mistake).
>>
>> At present, oce does not ship with PROJ_LIB at all. I agree that it does not need to ship with the PROJ.4 C source, and should, like rgdal and proj4, use the system PROJ.4 (or the CRAN PROJ.4). Only rgdal checks in ./configure for settings related to PROJ_LIB. Using PROJ.4 externally makes sense because of version shifting and missed bug fixes (there were some bad bugs before the aborted 4.9.0 release), and crucially updated metadata files. Using a single PROJ.4 on a platform avoids different applications seeing possibly different metadata.
>>
>> My guess would be that anything you might need to do can be done using function stubs exported by rgdal for use in raster (the package). You are free to choose not to use sp objects, but you'll find that the high-level support for these in rgdal is robust and well-motivated.
>>
>> If you go for the proj4 rather than rgdal solution, do make sure that the CRAN Windows and OSX binaries are built with the metadata PROJ_LIB copied to the binary packages.
>>
>> Again, apologies for not jumping in at the right time.
>>
>> Roger
>>
>> PS. Please also note that PROJ.4 is moving to
>>
>> https://github.com/OSGeo/proj.4
>>
>> --
>> Roger Bivand
>> Department of Economics, Norwegian School of Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; fax +47 55 95 91 00
>> e-mail: Roger.Bivand at nhh.no
>>
>
>
>
>
>
>
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no


More information about the R-package-devel mailing list