How to calculate geo-distance from a polygon?

[亡魂溺海] 提交于 2019-12-05 07:35:12

问题


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.

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
}   

回答1:


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.

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.

If your data is over a large chunk of globe then... tricky... 42...

Barry



来源:https://stackoverflow.com/questions/8579913/how-to-calculate-geo-distance-from-a-polygon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!