0

I'm making this loop, it's part of a script I'm making to illustrate a percentage in a polygon (let's say instead of a pie) but then I realized it looks good as an animation; when I start plotting, the polygon goes black every now and then, I want to be able to see the line between the two sections and with the black polygon I can't

library(units)
library(sf)
library(dplyr)
library(osmdata)
library(spatial.tools)

xalver <- osmdata::getbb("xalapa veracruz", format_out = "sf_polygon")
ea <- sp::bbox(as(xalver, "Spatial"))
ea2 <- ea
for(i in seq(.001, .015, by = 0.001)) { 
  ea[4]  <-  ea[4] -i
  ea2[2] <- ea[4]
  oik <- bbox_to_SpatialPolygons(ea) %>% sf::st_as_sf() %>% st_set_crs(4326) 
  oik2 <- bbox_to_SpatialPolygons(ea2) %>% sf::st_as_sf() %>% st_set_crs(4326)
  anka <- rbind(oik, oik2)
  st_intersection(xalver, anka) %>% plot()
#st_area() -> osh

#  if( drop_units(osh[1])/drop_units(osh[2]) < 0.5) break
  Sys.sleep(.3)
  }
1

What is happening is that at certain points you are dividing the region into a single ring POLYGON and a multiple-ring MULTIPOLYGON object, because the slicing line is chopping two separate parts off the top of the region.

Since this vector now has different types of object in it, sf treats it as a generic GEOMETRY object - these objects can be mixes of lines, points, and polygons.

Because the col argument controls the line colour of lines and the fill colour of polygons, the choice was made to use black if plotting a GEOMETRY object. Hence if your intersection creates a mix of POLYGON and MULTIPOLYGON objects, everything is drawn with col="black".

This is fixed in sf version 0.7.3 which is on github (but not yet on CRAN I think), where the code now tests the dimension of all elements in a GEOMETRY vector, and uses col=NA if the element is two dimensional (like a polygon).

  • plotting does work as i expected with geom_sf and plot.sf when there are two categories, so i'll try installing the dev version – Elio Diaz Feb 8 at 16:41
1

I can't 100% understand what you're going for, but I think your seq() might be producing strange bboxes. Is this closer? Dividing the extent of the y axis seems safer.

bb = st_bbox(xalver)
top <- bb['ymax']
bot <- bb['ymin']
steps <- seq(bot, top, by = (top - bot) / 10)

test <- lapply(steps[2:length(steps)], function(frame) {
  bf <- bb
  bf['ymax'] <- frame
  st_crop(xalver, st_as_sfc(bf, 4326))

})

for(i in seq_along(test)) { 
  plot(test[[i]])
  Sys.sleep(0.3)
  }

Either way, set plot(x , col = NA) and see what happens. Ref - https://github.com/r-spatial/sf/issues/945

  • Its nothing to do with the structure of the code, its purely the bug you mention - which is fixed in 0.7.3 on github. – Spacedman Feb 8 at 10:00
  • changed the col to white and it looks good; plotting does work well with geom_sf and plot.sf; i'll have to decide if it's going to be better to keep the two bounding boxes or only one in the final version of the code – Elio Diaz Feb 8 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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