rwindowspathr-sfproj

How can I use a projection string to refer to a current directory that has spaces in it with R (on windows)?


I am doing some spatial work, and I am using a custom projection string. However, this doesn't work - it can't find the file nzgd2kgrid0005.gsb, and I can't work out from the documentation where it is meant to be. Putting it in my current (project) directory does not work, and nor does the proj folder of my sf installation.

NZMG <- "+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=nzgd2kgrid0005.gsb +no_defs"

I can refer to the file if I copy the file the c:\ directory and refer to it like so:

NZMG <- paste0("+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=C:/nzgd2kgrid0005.gsb +no_defs")

This is not a great result for portability, many people need admin access to put files in c:\.

I tried using here() to force it to look in the local directory - no joy.

NZMG <- paste0("+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=", here(), "/nzgd2kgrid0005.gsb +no_defs")

> NZMG #note spaces in path that is returned
[1] "+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=C:/Users/nealm/OneDrive - DairyNZ Limited/Desktop/dairynz/NZMS1_conversion/nzgd2kgrid0005.gsb +no_defs"

I think it is due to the spaces in the path.

I confirmed this by using dir /X in the windows command prompt to find and substitute in the short (no spaces) name for the OneDrive folder, which is ONEDRI~1, and the resulting projection string works:

Context for short path: https://superuser.com/questions/348079/how-can-i-find-the-short-path-of-a-windows-directory-file

NZMG <- "+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=C:/Users/nealm/ONEDRI~1/Desktop/dairynz/NZMS1_conversion/nzgd2kgrid0005.gsb +no_defs"

So, I can make it work on my computer, but it is not very portable. Either:

  1. I need to find the place where the file will be found (and instruct users to put it there, if they can), OR,
  2. Find a way of using here() or something similar to apply the short (no spaces) folder names on Windows, from R, in a neat way.

Or maybe I am missing something even easier?


Solution

  • library(tidyverse)
    library(sf)
    library(here)
    library(utils)
    

    Option 1, put the .gsb file in the right place

    The answer is, copy the file nzgd2kgrid0005.gsb from this project folder, to the "proj" folder, in your "rgdal" folder, wherever your R packages are saved. Then it should work.

    NZMG <- "+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=nzgd2kgrid0005.gsb +no_defs"
    NZMG
    # n.b. it looks like NZMG needs this file - nzgd2kgrid0005.gsb
    # Available at LINZ link below (and now copied into this repo, see file list).
    # https://www.linz.govt.nz/data/geodetic-system/download-geodetic-software/gd2000it-download
    

    Option 2, use short path name to point to location in this project

    Note here::here() gives current location in the project, and wrapping that in utils::shortPathName() takes out the spaces so the projection works

    NZMG <- paste0("+proj=nzmg  +lat_0=-41 +lon_0=173 +x_0=2510000 +y_0=6023150  +ellps=intl +datum=nzgd49 +units=m +towgs84=59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993 +nadgrids=", utils::shortPathName(here::here()), "/nzgd2kgrid0005.gsb +no_defs")
    NZMG