1

I would like to get set of Boxes that 'surrounds' the country.

I have never worked with geo(spatial) data so I don't know where to start. I know there is a st_as_sfc function form sf package that can produce set of polygons from existing polygons, but I ma not sure how can I get polygon for the country and if this is the right approach.

So there should be set of boxes with coordinates lie '6538128.30144595,4918255.70658098,6540571.54819537,4920698.9533303995'

The country is Bosnia

Mislav Sagovac
  • 185
  • 1
  • 8

1 Answers1

1

For a reproducible example of drawing a bounding box for BiH consider this code; it downloads the Bosnia shapefile from gadm.org - so do check their terms & conditions (it is a popular repository and their T&C are not onerous, but...)

The code will return object box which will be in sfc format, understandable by the package {sf}.

You may find yourself needing the coordinates of the box; you can get them via printing sf::st_coordinates() of the box object. There are 5 points, that is OK as the first and last point are the same (the square like rectangle is closed = it both starts and ends in the same point / all four sides have to be defined by two points each).

library(dplyr)
library(sf)

# GADM.org data for Bosna i Hercegovina
bosnia <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_BIH_0_sf.rds"))

# get the bounding box
box <- st_bbox(bosnia) %>% 
  st_as_sfc() # makes the box a polygon - much easier to plot

# a visual check
plot(st_geometry(bosnia), col = "blue")
plot(box, border = "red", add = T)

BiH bounding box

# print coordinates
st_coordinates(box)
            X        Y L1 L2
[1,] 15.72739 42.56531  1  1
[2,] 19.61471 42.56531  1  1
[3,] 19.61471 45.27468  1  1
[4,] 15.72739 45.27468  1  1
[5,] 15.72739 42.56531  1  1
Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Thanks for the code. Now, I would like to divide Bosnia country to many little boxes. I have tried to use `g = st_make_grid(bosnia$geometry, cellsize = 1000, flat_topped = TRUE)`, but than plot(g) doesn't give me boxes. – Mislav Sagovac Apr 26 '21 at 20:56
  • to get a grid covering Bosnia consider this earlier answer: https://stackoverflow.com/questions/53789313/creating-an-equal-distance-spatial-grid-in-r/53801517#53801517 - and to get little bits of Bosnia over the grid run st_intersection on 1) the grid and 2) the Bosnia polygon – Jindra Lacko Apr 26 '21 at 21:45
  • I tried `# GADM.org data for Bosna i Hercegovina bosnia <- readRDS(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_BIH_0_sf.rds")) bosnia_st <- st_transform(bosnia, 5514) g = st_make_grid(bosnia_st$geometry, cellsize = c(10000, 10000), square = TRUE) plot(g) ` but this return boxes inside box, but not boxes inside country. – Mislav Sagovac Apr 27 '21 at 16:06
  • And it returns negative values for coordinates? – Mislav Sagovac Apr 27 '21 at 16:09