Calculating the distance between polygon and point in R

前端 未结 4 981
北海茫月
北海茫月 2020-12-17 04:48

I have a, not necessarily convex, polygon without intersections and a point outside this polygon. I\'m wondering how calculate the Euclidian distance most efficiently in a 2

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-17 05:27

    You could use the rgeos package and the gDistance method. This will require you to prepare your geometries, creating spgeom objects from the data you have (I assume it is a data.frame or something similar). The rgeos documentation is very detailed (see the PDF manual of the package from the CRAN page), this is one relevant example from the gDistance documentation:

    pt1 = readWKT("POINT(0.5 0.5)")
    pt2 = readWKT("POINT(2 2)")
    p1 = readWKT("POLYGON((0 0,1 0,1 1,0 1,0 0))")
    p2 = readWKT("POLYGON((2 0,3 1,4 0,2 0))")
    gDistance(pt1,pt2)
    gDistance(p1,pt1)
    gDistance(p1,pt2)
    gDistance(p1,p2)
    

    readWKT is included in rgeos as well.

    Rgeos is based on the GEOS library, one of the de facto standards in geometric computing. If you don't feel like reinventing the wheel, this is a good way to go.

提交回复
热议问题