如何计算多边形的地理距离? [英] How to calculate geo-distance from a polygon?
问题描述
我有一个shapefile,其中有50多种不同的多边形(代表50多种不同的区域)和10,000多种数据点,这些数据点应该存在于其中一个区域中.事实是,已经对10,000+点的区域进行了编码,我想算出它们在地理空间距离上与该编码区域相距多远.
I have a shapefile with 50+ different polygonal shapes (representing 50+ different regions) and 10,000+ data points that are supposed to be present in one of the regions. The thing is, the 10,000+ points are already coded with a region they are supposed to be in, and I want to figure out how far they are from this coded region in geo-spatial distance.
我当前的方法(下面的代码)包括将shapefile从sp
库转换为owin
对象,并使用distfun
可以在拉长的欧几里得空间中获得距离.但是我想获得地理空间距离(最终转换为公里).我下一步应该去哪里?
My current approach (code below), which involves converting shapefiles to owin
objects from the sp
library and using distfun
gets me distances in lat,long euclidean space. But I would like to get geo-spatial distances (eventually to convert to km). Where should I go next?
#basically cribbed from http://cran.r-project.org/web/packages/spatstat/vignettes/shapefiles.pdf (page 9)
shp <- readShapeSpatial("myShapeFile.shp", proj4string=CRS("+proj=longlat +datum=WGS84"))
regions <- lapply(slot(shp, "polygons"), function(x) SpatialPolygons(list(x)))
windows <- lapply(regions, as.owin)
# need to convert this to geo distance
distance_from_region <- function(regionData, regionName) {
w <- windows[[regionName]]
regionData$dists <- distfun(w)(regionData$lat, regionData$long)
regionData
}
推荐答案
我将数据投影到一个欧几里得(或接近欧几里得)坐标系-除非您跨越地球的大部分,否则这是可行的.使用来自maptools的spTransform或sp或rgdal(我忘记了),然后转换为数据附近的UTM区域.
I'd project the data to a euclidean (or near euclidean) coordinate system - unless you are spanning a large chunk of the globe then this is feasible. Use spTransform from maptools or sp or rgdal (I forget which) and convert to a UTM zone near your data.
您还可以使用rgeos软件包和gDistance函数来做得更好:
You also might do better with package rgeos and the gDistance function:
gDistance by default returns the cartesian minimum distance
between the two geometries in the units of the current projection.
如果您的数据遍及全球,那么...棘手... 42 ...
If your data is over a large chunk of globe then... tricky... 42...
巴里
这篇关于如何计算多边形的地理距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!