【问题标题】:r - Grid of points from polygon inputr - 来自多边形输入的点网格
【发布时间】:2018-07-21 12:05:14
【问题描述】:

我正在编写一个脚本,该脚本采用在 google earth 中创建的输入 KML 文件,并在多边形内绘制坐标点网格。

到目前为止,我有多边形输入和多边形边界框的点网格,但我只想在多边形内部有点。

我尝试使用over() 函数执行此操作,但它不起作用。有什么建议吗?

你可以下载我的测试KML文件HERE

library(rgdal)
library(sp)
library(maptools)

# ogrInfo() to find layer name... not as labelled in Google Earth?!
my.poly = readOGR(ds = "PolyNYC.kml", layer = "PolyNYC") 
proj4string(my.poly) <- "+proj=longlat +datum=WGS84 +no_defs"

# Creating grid of points
grdpts <- makegrid(my.poly)

# Converting from df to spdf
coords = cbind(grdpts$x1, grdpts$x2)
sp = SpatialPoints(coords)
spdf = SpatialPointsDataFrame(coords, grdpts, proj4string = CRS(proj4string(my.poly)))

# Using over() to select only those points in the polygon
inPoly = over(spdf, my.poly)
# This is not working

# Plotting the polygon with the points overlaid.
plot(my.poly)
points(spdf, pch = 3, col = "red")

#kmlPoints(obj = spdf, kmlfile = "BBoxFromPoly.kml", kmlname = "Testing123")

【问题讨论】:

    标签: r geospatial polygon spatial


    【解决方案1】:

    我将展示一个使用library(sf) 的解决方案,它是library(sp) 的继承者

    加载数据

    library(sf)
    
    ## read the kml
    my.poly <- sf::st_read("~/Downloads/PolyNYC.kml")
    
    ## create a grid of points
    grdpts <- sf::st_make_grid(my.poly, what = "centers")
    
    ## convert it to an `sf` object, as opposed to an `sfc`
    my.points <- sf::st_sf(grdpts)
    

    查看数据

    要查看地图上的对象,我正在使用我的 googleway 包将其绘制在 Google 地图上(因此您需要 API 密钥),但您可以使用 leaflet 或任何您想要的地图

    library(googleway)
    
    set_key("your_api_key_here")
    
    google_map() %>%
      add_polygons(my.poly) %>%
      add_markers(my.points)
    

    多边形中的点

    您可以使用函数sf::st_join() 加入几何图形

    pointsInside <- sf::st_join(x = my.points, y = my.poly, left = FALSE)
    
    # Simple feature collection with 59 features and 2 fields
    # geometry type:  POINT
    # dimension:      XY
    # bbox:           xmin: -74.1754 ymin: 40.63513 xmax: -73.75675 ymax: 40.8514
    # epsg (SRID):    4326
    # proj4string:    +proj=longlat +datum=WGS84 +no_defs
    # First 10 features:
    #   Name Description                   geometry
    # 1  TestLayerNYC             POINT (-74.08237 40.63513)
    # 2  TestLayerNYC             POINT (-74.03585 40.63513)
    # 3  TestLayerNYC              POINT (-74.1754 40.65916)
    # 4  TestLayerNYC             POINT (-74.12889 40.65916)
    # 5  TestLayerNYC             POINT (-74.08237 40.65916)
    # 6  TestLayerNYC             POINT (-74.03585 40.65916)
    # 7  TestLayerNYC             POINT (-73.80326 40.65916)
    # 8  TestLayerNYC              POINT (-74.1754 40.68319)
    # 9  TestLayerNYC             POINT (-74.12889 40.68319)
    # 10 TestLayerNYC             POINT (-74.08237 40.68319)
    

    这里,pointsInside 是多边形内的所有点

    查看结果

    google_map() %>%
      add_polygons(my.poly) %>%
      add_markers(pointsInside)
    

    【讨论】:

    • 这太完美了,谢谢!您如何将像元大小指定为 20 公里网格?
    • @Heliornis - 我认为你会想看看cellsize参数;我不完全确定您需要的确切参数
    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多