I have used the GDAL tools to rasterize a vector and providing the extension of my desired raster worked but if the data was very specific(i.e. lots of decimal points) I still didn't obtain the extension I needed. So I tried different things that worked in different ways:
a) I modified my rasters to the desired pixel size. If I created a raster from vector I followed this tutorial: http://www.mikemeredith.net/blog/1212_GIS_layer_for_Distance_from_in_QGIS.htm
This indicates in the editable box where to provide the pixel size. It also indicates how to provide extension but because my extension was very "long" that didn't work for extension only for pixel size.
b) If you are using QGIS and are preparing your environmental layers for Maxent I suggest using the QSDM plugin and unify all your layers. All layers need to be in the same format (.tiff),same CRS and it helps if they have the same resolution. They will be given an equal extension as new layers that have been "unified". Even if they don't seem like they are the same, checking the metadata will confirm that they have been modified to have the same extension and resolution. Ready to be used in Maxent.
c) Additionally there is a nice tutorial here for Maxent AND QGIS, check out the: "Creating new rasters with GDAL tools" section. Where they cover re-sampling and this is great if you want to change pixel size for your environmental layers.
http://clp-foss4g-workshop.readthedocs.org/en/latest/qgis_raster_resample.html
Hope it works and if there are other ways that worked for you I would be happy to know as sometimes for us beginners it is a lot of trial and error.
Ok, having learnt a lot of stuff in the months since I asked this question, here are a couple of options:
In R, very similar to Bastien's answer:
library(raster)
rootdir <- 'C:/Users/obrl_soil/Downloads'
setwd(rootdir)
tinyraster <- raster('tinyrast.tif')
tinypolygon <- readOGR('tinypolygon.shp')
# alter your mask polygon to line up with the nearest pixel edges
tpoly_aligned <- alignExtent(tinypolygon, tinyraster, snap='near')
# clip and export in one hit
tinyclip <- crop(tinyraster, tpoly_aligned, filename='tinyclip.tif')
I noticed when checking the outputs in QGIS that writeRaster introduced some spurious data in the output tif, but crop didn't - e.g. input elevation for a pixel was 108.10545, output with crop was the same, but output with writeRaster was 108.10545349121094.
Alternate workflow - get the polygon bounding box coordinates and feed them into gdal_translate using -projwin. Just be careful which version of gdal_translate you use!. 1.11 is fine, and 2.1.2 should be when its released.
You can also do this in R, like
gtrans111 <- 'C:/Program Files/GDAL/gdal_translate.exe'
tinypolygon <- readOGR('tinypolygon.shp')
tpbb <- toString(c(tinypolygon@bbox[1], tinypolygon@bbox[4],
tinypolygon@bbox[3], tinypolygon@bbox[2]))
tpbb <- gsub(', ', ' ', tpbb)
system2(gtrans111, args= c('-projwin', tpbb, 'tinyraster.tif', 'tinyclip2.tif' ))
or even bypass creating any R objects by just inputting the polygon bounding coordinates directly
system2(gtrans111, args= c('-projwin', '148.665 -20.88 148.67 -20.884',
'tinyraster.tif', 'tinyclip3.tif'))
The output extent is not identical to the raster package methods, but does align correctly. All of these methods are easy to loop across multiple datasets, which is the real advantage of R. You can do something similar in Python too.
Best Answer
I figured out that they GDAL algorithm in the toolbox worked. Although it use the same command (if you pay attention on the console call at the bottom) the arguments order results in a different output. By selecting both option: