Intersecting Points and Polygons in R

前端 未结 3 654
感动是毒
感动是毒 2020-12-10 03:30

I am working with shapefiles in R, one is point.shp the other is a polygon.shp. Now, I would like to intersect the points with the polygon, meaning that all

3条回答
  •  温柔的废话
    2020-12-10 03:47

    With the new sf package this is now fast and easy:

    library(sf)
    out <- st_intersection(points, poly)
    

    Additional options

    If you do not want all fields from the polygon added to the point feature, just call dplyr::select() on the polygon feature before:

    library(magrittr)
    library(dplyr)
    library(sf)
    
    poly %>% 
      select(column-name1, column-name2, etc.) -> poly
    
    out <- st_intersection(points, poly)
    

    If you encounter issues, make sure that your polygon is valid:

    st_is_valid(poly)
    

    If you see some FALSE outputs here, try to make it valid:

    poly <- st_make_valid(poly) 
    

    Note that these 'valid' functions depend on a sf installation compiled with liblwgeom.

提交回复
热议问题