Friday, 1 September 2017

geotiff tiff - Crop a raster file in R


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)

Answer



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)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...