8

I have a UN-Adjusted Population Count dataset, for the year 2015, stored in a TIFF file format (~450MB) downloaded from http://sedac.ciesin.columbia.edu/data/collection/gpw-v4/sets/browse

In R I am able to read the data using raster package. I am also able to plot it as follows,

#read your TIFF file

worldpopcount <- raster("gpw-v4-population-count-adjusted-to-2015-unwpp-country-totals_2015.tif")

worldpopcount

plot(worldpopcount)

Now I want to crop the West African region (covering the three countries Guinea, Liberia and Sierra Leone). My coordinates for West Africa are,

# extent format (xmin,xmax,ymin,ymax)

# WestBound     -16
# EastBound      -7.25
# SouthBound      4
# NorthBound     12.75

How do I crop the data using extent/crop? Once the region of interest has been cropped I would like to export it to an asc (ESRI ASCII) using the code

#writeRaster(r, filename="westAfrica.asc", format = "ascii", datatype='INT4S', overwrite=TRUE)
Aaron
  • 51,658
  • 28
  • 154
  • 317
Ash
  • 115
  • 1
  • 1
  • 5

3 Answers3

18

Create a box as a Spatial object and crop your raster by the box.

e <- as(extent(-16, -7.25, 4, 12.75), 'SpatialPolygons')
crs(e) <- "+proj=longlat +datum=WGS84 +no_defs"
r <- crop(worldpopcount, e)
Kazuhito
  • 30,746
  • 5
  • 69
  • 149
3

From the raster package documentation,

library(raster)

r <- raster(nrow=45, ncol=90)
r[] <- 1:ncell(r)

# crop Raster* with Spatial* object
b <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons')
crs(b) <- crs(r)
rb <- crop(r, b)
Aaron
  • 51,658
  • 28
  • 154
  • 317
1

Using a shapefile:

library(raster)

my_shape <- shapefile("../Input/area1.shp") my_raster <- raster("../Input/raster_band1.tif")

r2 <- crop(my_raster, extent(my_shape)) ### crop to the extent my_raster_clip <- mask(x= r2, mask=my_shape) ### delimitation to the shape geometry

plot(my_raster_clip)

Source: https://stackoverflow.com/a/23074167/14361772