2

I use the sf R package regularly, but I'm having trouble finding the correct process to use in sf to get a parallel result to what I would get performing a union in ArcGIS. Given the image below, where I have a set of polygon features in purple, and another polygon feature shown by the heavy black outline, what I want is what I would get from using union in ArcGIS - that is, the purple polygon features sliced on the border of the black polygon, and for the purple features within the black polygon, the attributes would now also contain attributes of my black polygon. For purple features outside the black polygon, they would have the attribute fields of the black polygon but they would have no values populated.

enter image description here

No matter how I parameterize st_union in sf, my interior purple polygons are dissolved with the bounding black polygon, and ALL features get all the attritues of the black polygon populated, even those outside the black polygon. What would be the analogous process in sf to get the result one would have using union in ArcGIS? It's simply a combination of both polygon features I need, where purple polygons are sliced by the black polygon, and features inside get attributes of black polygon feature, while those outside get the attribute fields of the black polygon but un-populated values.

2
  • 1
    If you can give us some sample data or code to synthesise it then we're more likely to have a go at answering this. It'll be something like using st_intersects to get the relationship between each set of features and then extracting attributes from the result of that.
    – Spacedman
    Jan 24, 2020 at 20:22
  • 1
    you want st_intersection() will return polygons where overlapping occurs and st_difference() %>% st_join(), thus keeping the purple polygons out of the black boundary and having the empty values for the outside, so you may rbind() both tables together (it's a two step process)
    – Elio Diaz
    Jan 24, 2020 at 20:53

1 Answer 1

0

Thanks @ElioDiaz, @Spacedman - here is code (could be simplified with chained operation) based on your suggestion that does exactly what I intended with trying to emulate result of union tool in ArcMap.

      outline <- read_sf('outline.shp') # black circle in picture
      other_polys <- read_sf('other_polys.shp') # purple polygons
      within <- st_within(other_polys, outline, sparse = FALSE)
      inside <- other_polys[within,] # interior to black polygon
      outside <- other_polys[!within,] # outside black polygon
      inside <- st_join(inside, outline) # attributes of black polygon added to interior polys
      missing_cols <- setdiff(names(inside), names(outside))
      outside[missing_cols] <- 0 # add columns of black polygon but with zeros for extiror purple polys
      full <- rbind(inside, outside) # join back together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.