简体   繁体   English

如何使用投影字符串来使用 R (在 Windows 上)引用其中包含空格的当前目录?

[英]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:任何一个:

  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.找到一种使用here()或类似方法的方法,以一种简洁的方式在 Windows 上应用短(无空格)文件夹名称,来自 R。

Or maybe I am missing something even easier?或者,也许我错过了一些更容易的东西?

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

Option 1, put the .gsb file in the right place选项1,将.gsb文件放在正确的位置

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

Option 2, use short path name to point to location in this project选项 2,使用短路径名指向此项目中的位置

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM