从 .kml 文件创建 R 的 x 和 y 距离坐标
我想使用 .kml 轨道文件来制作一组 x,y 坐标以在 R 中使用。
我现在拥有的是 GoogleEarth 轨道,我相信它是一个 LineString。我听说人们通常使用 rgdal 包,但它不适用于 Mac 版本的 R。如果可能的话,我想在 Mac 上执行此操作,并在其中进行其余的分析。如果有必要,我可以在 Windows 上的 R64 上进行转换,然后将坐标带到我的 Mac,但这似乎......笨重。
.kml 代码的开头如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Perimeter_Track.kml</name>
<Placemark>
<name>ACTIVE LOG</name>
<LineString>
<coordinates>
-157.80736808,21.4323436,20.324951171875
我想将其转换为距地图中某个点的 x , y 坐标(以公里为单位)。成品将是水体的线条轮廓,其上覆盖有物种丰度数据。
我已经尝试过几种方法:
1. 将.kml文件转换为.csv并使用read.csv
将其导入到r中;
2. 使用maptools
包中的getKMLcooperatives
导入坐标。
(1) 的问题是,当我尝试将 .kml 坐标转换为 csv 时,转换器程序 (kmlcsv) 中出现错误,表示无法读取文件(我不确定为什么 - 错误日志不可用)。
当我尝试(2)时,我得到的坐标排列得很奇怪。spa<-getKML坐标(“Perimeter_Track.kml”,ignoreAltitude = TRUE)
摘要(spa)返回:
长度类别模式
[1,] 128 -无-数字
[2,] 242 -非数字
[3,] 34 -非数字
[4,] 126 -非数字
我相信这是因为 .kml 文件实际上是四个单独的轨道,由小间隙隔开(即,他们将 GPS 关闭一小段时间,然后再次启动)。我是否需要单独导入这些才能获得整个形状?如果是这样,我该怎么做?
我最终希望在 x x y km 的网格上获得这个形状,其中坐标以 km 为单位,而不是 GPS 坐标。如果有人对如何做到这一点有任何见解,我很乐意听取您的意见!
预先非常感谢。
I would like to use a .kml track file to make a set of x, y coordinates for use in R.
What I have right now is a GoogleEarth track, which I believe is a LineString. I have heard that the rgdal package is usually what people use, but it doesn't work on Mac versions of R. If possible, I'd like to do this on a Mac, where I do the rest of my analyses. If necessary, I can do the conversion on R64 with Windows, and then bring the coordinates to my Mac, but that seems...clunky.
The beginning of the .kml code looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Perimeter_Track.kml</name>
<Placemark>
<name>ACTIVE LOG</name>
<LineString>
<coordinates>
-157.80736808,21.4323436,20.324951171875
I want to convert it into x , y coordinates in kilometers from a point in my map. The finished product will be a line outline of a body of water, with species abundance data overlaid on it.
I have tried a couple of methods already:
1. Converting the .kml file into a .csv and importing it to r using read.csv
;
2. Importing coordinates using getKMLcoordinates
in the maptools
package.
The problem with (1) is that when I try to convert the .kml coords into csv, I get an error in the converter program (kmlcsv) that says it can't read the file (I'm not sure why- the error logs aren't available).
When I try (2), I get coordinates that are arranged weirdly.spa<-getKMLcoordinates("Perimeter_Track.kml", ignoreAltitude=TRUE)
returns:
summary(spa)
Length Class Mode
[1,] 128 -none- numeric
[2,] 242 -none- numeric
[3,] 34 -none- numeric
[4,] 126 -none- numeric
I believe this is because the .kml file is actually four separate tracks, separated by small gaps (i.e., where they turned the GPS off for a short time, then started again). Do I need to import these all separately in order to get the whole shape? If so, how do I do this?
I would like, eventually, to get this shape on a grid that is x by y km, where the coordinates are in km instead of GPS coords. If anyone has any insight into how to do this, I would love to hear from you!
Thanks very much in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使预编译包不可用,您仍然可以在 Mac 上从其源代码安装
rgdal
,如下所示:从 http://www.kyngchaos.com/software/frameworks。
将刚刚安装的程序的位置添加到您的 UNIX 路径中。在 Mac 终端中,执行:
从 CRAN 下载
rgdal
包的源代码,网址为 http://cran.r-project.org/web/packages/rgdal/index.html。打开 R 并构建/安装
rgdal
包。请注意,我们必须指定我们刚刚安装的一些东西的位置。这在我的 Mac OS X 10.6 上安装得很好。祝你好运!
因此,数据的基本思想可能是:
居住的好地方!
Even though a precompiled package isn't available, you can still install
rgdal
from its source on a mac like follows:Install the "GDAL complete" framework from http://www.kyngchaos.com/software/frameworks.
Add the locations of the programs you just installed to your unix path. In mac terminal, do:
Download the source for the
rgdal
package from CRAN at http://cran.r-project.org/web/packages/rgdal/index.html.Open R and build/install the
rgdal
package. Note that we have to specify the locations to some of the stuff we just installed.This installs fine on my Mac OS X 10.6. Good luck!
So the basic idea with your data might be:
Great place to live!
使用 rgdal 中的 readOGR 将某些内容读入“sp”类对象(此处可能是 SpatialLinesDataFrame)后,您可以使用 spTransform 函数将其从经纬度转换为笛卡尔系统。
改成哪个系统取决于它在地球的哪个地方。有一系列依赖于经度的标准变换,称为“UTM”区域(通用横轴墨卡托)。只需查找您所在的经度区域,找到 EPSG 代码,然后启动 spTransform。
对于英国,有一个名为 Ordnance Survey Grid 的标准网格系统,其 EPSG 代码为 27700。因此,要将经纬度 (EPSG:4326) 中的内容转换为 OSGB 米,我会这样做:
帮助中有很多示例对于 sp 变换。
请注意,仅当您的数据位于地球的一小部分时,这一切都很棒......
Once you've read something into an "sp" class object (probably a SpatialLinesDataFrame here) using readOGR from rgdal, you can transform it to a cartesian system from lat-long with the spTransform function.
Which system to transform it into depends on where on the earth it is. There's a bunch of standard transforms that depend on longitude called 'UTM' zones (Universal Transverse Mercator). Simply look up the zone for your longitude, find the EPSG code, and fire up spTransform.
For the UK, there's a standard grid system called the Ordnance Survey Grid, which has an EPSG code of 27700. So to transform something in lat-long (EPSG:4326) to OSGB metres, I do:
There's lots of examples in the help for spTransform.
Note this is all great only if your data are on a small part of the earth...