从 .kml 文件创建 R 的 x 和 y 距离坐标

发布于 2024-12-12 08:11:48 字数 1626 浏览 5 评论 0原文

我想使用 .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)
summary(spa)
returns:

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-12-19 08:11:48

即使预编译包不可用,您仍然可以在 Mac 上从其源代码安装 rgdal,如下所示:

  1. http://www.kyngchaos.com/software/frameworks

  2. 将刚刚安装的程序的位置添加到您的 UNIX 路径中。在 Mac 终端中,执行:

    PATH=/Library/Frameworks/GDAL.framework/unix/bin:/Library/Frameworks/PROJ.framework/unix/bin$PATH
    
  3. 从 CRAN 下载 rgdal 包的源代码,网址为 http://cran.r-project.org/web/packages/rgdal/index.html

  4. 打开 R 并构建/安装 rgdal 包。请注意,我们必须指定我们刚刚安装的一些东西的位置。

    install.packages('~/Downloads/rgdal_0.7-1.tar.gz', repos=NULL, type='source', configure.args=c('--with-proj-include =/Library/Frameworks/PROJ.framework/unix/include', '--with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib'))
    

这在我的 Mac OS X 10.6 上安装得很好。祝你好运!


因此,数据的基本思想可能是:

library(rgdal)
library(maptools)

# Load KML coordinates
coords = getKMLcoordinates('data.kml')
coords = SpatialPoints(coords, CRS('+proj=longlat'))

# Load US Maps (get from www.gadm.org)
load('USA_adm1.RData') 
hawaii = gadm[gadm$NAME_1 == 'Hawaii', ]

# Transform coordinates
hawaii.proj = spTransform(hawaii, CRS=CRS('+init=epsg:2784 +units=km'))
coords.proj = spTransform(coords, CRS=CRS('+init=epsg:2784 +units=km'))

# Plot
dev.new(width=4, height=4)
plot(hawaii.proj, axes=T, xlim=c(450,550), ylim=c(0,60))
points(coords.proj, pch=16, col='red')

在此处输入图像描述

居住的好地方!

Even though a precompiled package isn't available, you can still install rgdal from its source on a mac like follows:

  1. Install the "GDAL complete" framework from http://www.kyngchaos.com/software/frameworks.

  2. Add the locations of the programs you just installed to your unix path. In mac terminal, do:

    PATH=/Library/Frameworks/GDAL.framework/unix/bin:/Library/Frameworks/PROJ.framework/unix/bin$PATH
    
  3. Download the source for the rgdal package from CRAN at http://cran.r-project.org/web/packages/rgdal/index.html.

  4. Open R and build/install the rgdal package. Note that we have to specify the locations to some of the stuff we just installed.

    install.packages('~/Downloads/rgdal_0.7-1.tar.gz', repos=NULL, type='source', configure.args=c('--with-proj-include=/Library/Frameworks/PROJ.framework/unix/include', '--with-proj-lib=/Library/Frameworks/PROJ.framework/unix/lib'))
    

This installs fine on my Mac OS X 10.6. Good luck!


So the basic idea with your data might be:

library(rgdal)
library(maptools)

# Load KML coordinates
coords = getKMLcoordinates('data.kml')
coords = SpatialPoints(coords, CRS('+proj=longlat'))

# Load US Maps (get from www.gadm.org)
load('USA_adm1.RData') 
hawaii = gadm[gadm$NAME_1 == 'Hawaii', ]

# Transform coordinates
hawaii.proj = spTransform(hawaii, CRS=CRS('+init=epsg:2784 +units=km'))
coords.proj = spTransform(coords, CRS=CRS('+init=epsg:2784 +units=km'))

# Plot
dev.new(width=4, height=4)
plot(hawaii.proj, axes=T, xlim=c(450,550), ylim=c(0,60))
points(coords.proj, pch=16, col='red')

enter image description here

Great place to live!

笔落惊风雨 2024-12-19 08:11:48

使用 rgdal 中的 readOGR 将某些内容读入“sp”类对象(此处可能是 SpatialLinesDataFrame)后,您可以使用 spTransform 函数将其从经纬度转换为笛卡尔系统。

改成哪个系统取决于它在地球的哪个地方。有一系列依赖于经度的标准变换,称为“UTM”区域(通用横轴墨卡托)。只需查找您所在的经度区域,找到 EPSG 代码,然后启动 spTransform。

对于英国,有一个名为 Ordnance Survey Grid 的标准网格系统,其 EPSG 代码为 27700。因此,要将经纬度 (EPSG:4326) 中的内容转换为 OSGB 米,我会这样做:

mapOS = spTransform(mapLL,CRS=CRS("+init=epsg:27700"))

帮助中有很多示例对于 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:

mapOS = spTransform(mapLL,CRS=CRS("+init=epsg:27700"))

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文