Intersection of polygons in R using sf

后端 未结 1 1519
梦如初夏
梦如初夏 2021-01-25 10:30

I want to assess the degree of spatial proximity of each point to other equivalent points by looking at the number of others within 400m (5 minute walk).

I have some poi

相关标签:
1条回答
  • 2021-01-25 11:17

    How can I : asess the degree of spatial proximity of each point to other equivalent points by looking at the number of others within 400m (5 minute walk).

    Here is how to add a column to your initial sfc of pollings stations that tells you how many polling stations are within 400m of each feature in that sfc.

    Note that the minimum value is 1 because a polling station is always within 400m of itself.

    # n_neighbors shows how many polling stations are within 400m
    polls %>% 
      mutate(n_neighbors = lengths(st_is_within_distance(polls, dist = 400)))
    

    Similarly, for your sfc collection of intersecting polygons, you could add a column that counts the number of buffer polygons that contain each intersection polygon:

    polls_intersection %>% 
      mutate(n_overlaps = lengths(st_within(geometry, polls_buffer_400)))
    

    And this is the bit I'm not sure about, to get to the output I want (which will show "Hotspots" of polling stations in this case) how do I colour things?

    If you want to plot these things I highly recommend using ggplot2. It makes it very clear how you associate an attribute like colour with a specific variable.

    For example, here is an example mapping the alpha (transparency) of each polygon to a scaled version of the n_overlaps column:

    library(ggplot2)
    polls_intersection %>% 
      mutate(n_overlaps = lengths(st_covered_by(geometry, polls_buffer_400))) %>% 
      ggplot() + 
      geom_sf(aes(alpha = 0.2*n_overlaps), fill = "red") 
    

    Lastly, there should be a better way to generate your intersecting polygons that already counts overlaps. This is built in to the st_intersection function for finding intersections of sfc objects with themselves.

    However, your data in particular generates an error when you try to do this:

    st_intersection(polls_buffer_400)
    
    # > Error in CPL_nary_intersection(x) : 
    #>  Evaluation error: TopologyException: side location conflict at 315321.69159061194 199694.6971799387.
    

    I don't know what a "side location conflict" is. Maybe @edzer could help with that. However, most subsets of your data do not contain that conflict. For example:

    # this version adds an n.overlaps column automatically:
    st_intersection(polls_buffer_400[1:10,]) %>% 
      ggplot() + geom_sf(aes(alpha = 0.2*n.overlaps), fill = "red") 
    

    0 讨论(0)
提交回复
热议问题