Search code examples
rgisgeospatialr-sfr-sp

Count number of points in sfc_multipoint object


I would like to filter sfc_multipoint data by the size of the sfc_multipoints, i.e. the number of points/coordinate pairs contained in each row. Suppose I want to keep only rows with no more than 3 points from the following data set, based on mapview::breweries:

library(mapview)
library(sf)
library(tidyverse)

pts <- breweries %>% 
  group_by(zipcode) %>% 
  summarise()

In order to do this, I need to know the number of points within each sfc_multipoint. In theory I could export the coordinates via as("spatial") and count rows, but that is not only ugly, but also not really feasible for data sets of any realistic size. Any suggestions?


Solution

  • You could try dplyr::count() or summarize(n = n()) to read the number of rows within a given zipcode, but the breweries dataset seems to have some duplicates so this might be misleading.

    breweries %>% 
      count(zipcode)
    
    #----------
      zipcode n                       geometry
    1   90562 1      POINT (11.15795 49.53495)
    2   90614 1       POINT (10.85194 49.4208)
    3   90763 1      POINT (10.99625 49.44171)
    4   91054 3 MULTIPOINT ((11.00901 49.59...
    5   91097 2 MULTIPOINT ((10.76099 49.59...
    

    Or only unique points (note the change in 91054)

    breweries %>%
      distinct(zipcode, geometry) %>%
      count(zipcode)
    
    #-----
      zipcode n                       geometry
    1   90562 1      POINT (11.15795 49.53495)
    2   90614 1       POINT (10.85194 49.4208)
    3   90763 1      POINT (10.99625 49.44171)
    4   91054 2 MULTIPOINT ((11.00901 49.59...
    5   91097 2 MULTIPOINT ((10.76099 49.59...
    

    You could also try mapview::npts()

    breweries %>%
      group_by(zipcode) %>%
      summarize() %>%
      rowwise() %>%
      mutate(n = npts(geometry))
    
    #----
       zipcode                                              geometry     n
     * <chr>                                          <GEOMETRY [°]> <dbl>
     1 90562                               POINT (11.15795 49.53495)     1
     2 90614                                POINT (10.85194 49.4208)     1
     3 90763                               POINT (10.99625 49.44171)     1
     4 91054   MULTIPOINT ((11.00901 49.59511), (11.00505 49.60255))     2
     5 91097   MULTIPOINT ((10.76099 49.59044), (10.76954 49.59015))     2