代码之家  ›  专栏  ›  技术社区  ›  Xavier Prudent

这一点位于多边形内吗?

  •  3
  • Xavier Prudent  · 技术社区  · 8 年前

    我问了很多这样的问题,并尝试了各种功能和软件包,如sp,但无法解释为什么它失败了。

    我尝试了这个非常简单的函数: https://www.rdocumentation.org/packages/SDMTools/versions/1.1-221/topics/pnt.in.poly

    install.packages("SDMTools v1.1-221")
    library(SDMTools v1.1-221)
    
    ## Coordinates of the polygon corners
    lat <- c(48.43119, 48.43119, 48.42647, 48.400031, 48.39775, 48.40624, 48.42060, 48.42544, 48.42943 ) 
    lon <- c(-71.06970, -71.04180, -71.03889, -71.04944, -71.05991, -71.06764, -71.06223, -71.06987, -71.07004)
    pol = cbind(lat=lat,lng=lon)
    
    ## Point to be tested
    x <- data.frame(lng=-71.05609, lat=48.40909)
    
    ## Visualization, this point clearly stands in the middle of the polygon
    plot(rbind(pol, x))
    polygon(pol,col='#99999990')
    
    ## Is that point in the polygon? 
    out = pnt.in.poly(x,poly)
    
    ## Well, no (pip=0)
    print(out)
    

    2 回复  |  直到 8 年前
        1
  •  5
  •   sconfluentus    8 年前

    我没有使用你正在使用的方法,但我有一个来自内部的方法 sp

    我选择了你的代码并留下了 lat lon 作为向量,点坐标作为值,以满足功能要求。

    其要点如下:

    require(sp)
    ## Your polygon
    lat <- c(48.43119, 48.43119, 48.42647, 48.400031, 48.39775, 48.40624, 48.42060, 48.42544, 48.42943 ) 
    lon <- c(-71.06970, -71.04180, -71.03889, -71.04944, -71.05991, -71.06764, -71.06223, -71.06987, -71.07004)
    
    
    ## Your Point
    lng=-71.05609
    lt=48.40909
    
    # sp function which tests for points in polygons
    
    point.in.polygon(lt, lng, lat, lon, mode.checked=FALSE)
    

    这是输出:

    [1] 1  
    

    文件对此的解释:

    • 0点严格位于多边形外部
    • 2点位于多边形边的相对内部
    • 3点是多边形的顶点

    df df$lat df$lon test 具有 test$lat test$lon

    point.in.polygon(df$lat, df$lon, test$lat, test$lon, mode.checked=FALSE)
    

    它将返回一个0,1,2,3的向量

    只需确保你首先得到正确的格式! Here is a link to the function page:

        2
  •  2
  •   SymbolixAU Adam Erickson    8 年前

    ?pnt.in.poly ,但似乎 lng lat 列很重要。您需要在 pol 这是可行的。

    pol = cbind(lat=lat, lng=lon)
    pnt.in.poly(x, pol)
    #          lng      lat pip
    # 1 -71.05609 48.40909   0
    
    pol = cbind(lng=lon, lat=lat)
    pnt.in.poly(x, pol)
    #          lng      lat pip
    # 1 -71.05609 48.40909   1
    

    液化天然气 通常被认为是 x-axis 这个 y-axis ,您将看到在 plot()