I have a shapefile of Mexico composed of Admin 2 polygons.

I am trying to create a buffer along the Mexico-US border, in R considering this is a shapefile of Mexico only and I want to retain the admin 2 boundaries after the buffer is created.

So I am trying to find a way of selecting all the bordering admin 2 polygons. The admin 2 polygons are of varying size with variable numbers of neighbours.

@Spacedman provided feedback -- I found this resource, which was helpful, and my code now looks as follows:

#subset to mexico and us only
mexus <- adm0all[adm0all$ADM0_NAME %in% c('Mexico', 'United States of 

#identify shared border
sf_bord <- mexus %>% 
            ms_innerlines() %>% 
            as_tibble() %>% 

#plot & see if worked
  geom_sf(data=mexus) +
  geom_sf(data=sf_bord, color = "red", size = 1) +
  coord_sf(datum = NA) +

enter image description here

This results in the border being isolated(see image above), however when I calculate the border using the following code, my buffer is fragmented/non-continuous, see code and image below:

#transform to project CRS to get units in meters
sf_bord_utm <- st_transform(sf_bord, 6368)

#calculate 5km buffer
mexus_border <- st_buffer(sf_bord_utm, dist = 5000)

enter image description here

Does anyone understand why this is not a continuous buffer?

  • Are you allowed to get a shapefile of the US border? I can do it fairly easily if I have one. But just using the Mexico-2 shapes is tricky because its not clear where the border ends and the Gulf begins. If this is a one-off I'd do it with QGIS - load the shape, zoom along the border selecting polygons according to an underlying basemap or your own knowledge.
    – Spacedman
    Commented Sep 6, 2018 at 7:08
  • Yes, I can. Exactly. And unfortunately this is not a one-off, so will need a reproducible method to use. Thank you for your feedback, much appreciated
    – e.stearns
    Commented Sep 6, 2018 at 16:55
  • With a US boundary shapefile (or even just the states you know form the border) you can use sf::st_intersects to get which regions intersect - you might need to do a small (1km?) buffer of the USA polygon to make sure it encroaches over the border. What have you tried already?
    – Spacedman
    Commented Sep 6, 2018 at 17:02
  • Thank you, @Spacedman. I have updated my original question with more information.
    – e.stearns
    Commented Sep 12, 2018 at 17:22

1 Answer 1


I adopted the following strategy to solve this issue:

  • Found a polylines shapefile representing the Mexico-US border from the U.S. Geological Survey website (found here for direct download)
  • Brought this into my R script and calculated the 5 km buffer
  • Set the CRS to match that of my Mexico Admin0 shapefile
  • Used st_intersection to clip buffer to Mexico side only


#calculate 5km buffer
mexus_border <- st_buffer(mex_us_bord, dist = 5000)

#plot buffer under border over mexico to check worked
ggplot() +
  geom_sf(data=mex) +
  geom_sf(data=mexus_border, color = "red", size = 1) +
  geom_sf(data=mex_us_bord, color = "blue", size = 1) +
  coord_sf(datum = NA) +

#set buffer area crs to same as mexico
mexus_bord_proj <- st_transform(mexus_border, 4326)

#plot mexico adm0 on top and clip buffer to just mexico side
mex_buff <- st_intersection(mex, mexus_bord_proj)

Thank you to @Spacedman for your help.

Your Answer

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

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