简体   繁体   English

R-计算点和线之间的最小距离

[英]R- Calculating the minimum distance between points and lines

I have a large set of polygon shapefiles of parcels (for 50+ counties) and I would like to loop through each of them and calculate the distance from the center of each parcel to the nearest railroad, stored nationally in a line shapefile.我有一大组地块的多边形形状文件(用于 50 多个县),我想遍历它们中的每一个并计算从每个地块的中心到最近的铁路的距离,在全国范围内存储在一个线形文件中。

I had come up with two different measures to do this: point2Line from the geospheres package and gDistance from rgeos package.我想出了两种不同的方法来做到这一点:来自 geospheres 包的 point2Line 和来自 rgeos 包的 gDistance。 Each had different problems for me.每个人对我来说都有不同的问题。

For the point2line, my code looked something like this:对于 point2line,我的代码如下所示:

rail_projection <-spTransform(rail_file,CRS(proj4string(parcels)) #project to same CRS
rail_crop<-crop(rail_projection,extent(parcels)) #crop to extent of parcels
centroids<-gCentroid(parcels,byid=T) # take centroid of parcel polygons.
m<-gDistance(rail_crop,centroids,byid=T)

but this spits out the following warning:但这会发出以下警告:

Warning messages:
1: In RGEOSDistanceFunc(rail_crop, centroids, byid=T, "rgeos_distance") :
Spatial object 1 is not projected; GEOS expects planar coordinates
2: In RGEOSDistanceFunc(rail_crop, centroids, byid=T, "rgeos_distance") :
Spatial object 2 is not projected; GEOS expects planar coordinates

It also gives me a nparcels X (I think number of line matrix) of numbers for which I'm not quite sure of the measurement.它还给了我一个 nparcels X(我认为行矩阵的数量)的数字,我不太确定其测量值。 I'd like the measurement in KM or miles or something.我想要以公里或英里为单位的测量值。

The second method, using dist2line looks like this: dist<-dist2Line(centroids,rail_crop) It works fine, but the only problem is it takes very long!第二种方法,使用 dist2line 看起来像这样: dist<-dist2Line(centroids,rail_crop) 它工作正常,但唯一的问题是它需要很长时间! For looping through 60 or so counties, it may take a couple days.如果要遍历 60 个左右的县,可能需要几天时间。 Additionally, I'd rather not have to crop the rail shapefile in case the closest railroad is in another county.此外,如果最近的铁路在另一个县,我宁愿不必裁剪铁路形状文件。 Running the code with the entire shapefile takes wayyy longer.使用整个 shapefile 运行代码需要更长的时间。

So what I'm essentially look for is a way to get nearest distance from points to a line shapefile in a fairly efficient way that spits out the distance in km or miles, or something similarly interpretable.所以我基本上要寻找的是一种以相当有效的方式获得从点到线形状文件的最近距离的方法,以公里或英里或类似的可解释的方式输出距离。 If this can be done with gDistance in a way that doesn't have these errors then great!如果这可以通过 gDistance 以没有这些错误的方式完成,那就太好了! If there's a way to speed it up from dist2Line, or some other method, that'd also be great.如果有办法从 dist2Line 或其他方法加快速度,那也很棒。 If you also have any thoughts on how to crop the rail shapefile with some buffer, I'd also appreciate it.如果您对如何使用一些缓冲区裁剪轨道 shapefile 也有任何想法,我也将不胜感激。 (I can't post code since the parcel data is proprietary) (我无法发布代码,因为包裹数据是专有的)

I'm still a beginner with spatial stuff so sorry if I have trouble following or if this has been answered before.我仍然是空间内容的初学者,如果我在跟随时遇到问题或者之前已经回答过,我很抱歉。 I've looked around and haven't found solutions I'm able to make work here.我环顾四周,没有找到我可以在这里工作的解决方案。

Thanks!谢谢!

Edit with Data使用数据编辑

So I guess I figured out where my warning message was coming from using this .所以我想我想出了我的警告信息来自使用this 的地方 I was using a 3d coordinate system rather than a planar one.我使用的是 3d 坐标系而不是平面坐标系。 I've run some code with a shapefile I downloaded from the internet for a county in Wisconsin, and gDistance spits out a matrix with the number of parcels x the number of lines with distances (I presume) from each parcel to each rail line.我用我从互联网上为威斯康星州的一个县下载的 shapefile 运行了一些代码,gDistance 输出了一个矩阵,其中包含包裹数量 x 线数以及从每个包裹到每条铁路线的距离(我假设)。 Does anyone know in what measurement these distances come out?有谁知道这些距离的测量结果是什么? How can I convert them to KM or miles?我如何将它们转换为公里或英里? Also is there a "preferred" planar projection I should be using.还有我应该使用的“首选”平面投影。 Furthermore, any thoughts on how to crop the shapefile with some buffer would be greatly appreciated.此外,任何关于如何使用一些缓冲区裁剪 shapefile 的想法都将不胜感激。 Files/code can be found here文件/代码可以在这里找到

I would recommend looking into the Universal Transverse Mercator system .我建议查看通用横轴墨卡托系统 I don't know if it's the best projected coordinate system, but it's the one I see used most frequently in my field (as long as the study area is not too large).我不知道它是否是最好的投影坐标系,但它是我在我的领域中看到最常用的一个(只要研究区域不是太大)。

Once you find the UTM zone specific to your region, you can use sp::spTransform() to reproject your shapefiles such that gDistance() can return your distances in either kilometers or meters.找到特定于您所在地区的 UTM 区域后,您可以使用sp::spTransform()重新投影您的 shapefile,以便gDistance()可以以公里或米为单位返回您的距离。

Here's the proj4string for a UTM zone that covers Kenya: +proj=utm +zone=37 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs .这是覆盖肯尼亚的 UTM 区域的 proj4string: +proj=utm +zone=37 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs As you can see, the units here are in meters ( +units=m ).如您所见,此处的单位以米为单位( +units=m )。

It might be worth your time to look into the relatively new sf package, which simplifies and standardizes functions and classes for spatial data.可能值得您花时间研究一下相对较新的sf包,它简化和标准化了空间数据的函数和类。 Here's a brief intro you can check out: http://strimas.com/r/tidy-sf/ .这是您可以查看的简短介绍: http : //strimas.com/r/tidy-sf/ The analogous functions from that package are st_transform() and st_distance() .该包中的类似函数是st_transform()st_distance()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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