[英]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.但是,这不起作用 - 它找不到文件
nzgd2kgrid0005.gsb
,而且我无法从文档中找出它的意思。 Putting it in my current (project) directory does not work, and nor does the proj
folder of my sf installation.将它放在我当前的(项目)目录中不起作用,我的 sf 安装的
proj
文件夹也不起作用。
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:如果我将文件复制到
c:\
目录并像这样引用它,我可以引用该文件:
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:\
.这对于可移植性来说并不是一个很好的结果,许多人需要管理员权限才能将文件放在
c:\
中。
I tried using here()
to force it to look in the local directory - no joy.我尝试使用
here()
强制它在本地目录中查找 - 不高兴。
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:我通过在 Windows 命令提示符中使用 dir
/X
来确认这一点,以查找并替换 OneDrive 文件夹的短(无空格)名称,即ONEDRI~1
,并且生成的投影字符串有效:
Context for short path: https://superuser.com/questions/348079/how-can-i-find-the-short-path-of-a-windows-directory-file短路径的上下文: 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:
任何一个:
here()
or something similar to apply the short (no spaces) folder names on Windows, from R, in a neat way.here()
或类似方法的方法,以一种简洁的方式在 Windows 上应用短(无空格)文件夹名称,来自 R。 Or maybe I am missing something even easier?或者,也许我错过了一些更容易的东西?
library(tidyverse)
library(sf)
library(here)
library(utils)
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.答案是,将此项目文件夹中的 nzgd2kgrid0005.gsb 文件复制到“rgdal”文件夹中的“proj”文件夹,即保存 R 包的位置。 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
Note here::here()
gives current location in the project, and wrapping that in utils::shortPathName()
takes out the spaces so the projection works注意
here::here()
给出项目中的当前位置,并将其包装在utils::shortPathName()
中取出空格,以便投影工作
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.