Problems with raster clipping

1,820 views
Skip to first unread message

Bert Harris

unread,
Mar 30, 2012, 4:58:17 PM3/30/12
to max...@googlegroups.com
Hi all,

I am having trouble getting the input rasters to line up exactly and of course Maxent will not run unless they are perfect.

I have layers of elevation, rainfall, and land cover, all of different cell sizes.

In ArcGIS 10, I clipped the layers to the general study area. Then I resampled the land cover and rainfall layer to have the same cell size as the fine scale elevation layer. This was successful (as shown in the exported asc file header).

I then clipped the rainfall and land cover layer to be the same size as the elevation layer by specifying "output extent". The other layers end up being slightly larger than the elevation layer. I have tried various permutations of this (e.g. resampling rainfall while specifying output extent to be equal to elevation layer) with no luck.

So I am wondering:

(1) does anyone have a work around for this? In Arc, Diva, or R?
(2) why don't rasters clip to exact extent the user specifies in ArcGIS?
(3) Just for interest, why does Maxent have to be so rigid about having exactly the same extent? It seems a line of code could be added to the program to simlply choose the smallest input variable when estimating the relationships.

Thanks!!
Bert
--
J. Berton C. Harris
PhD student
Environment Institute & ACEBB
School of Earth and Environmental Sciences
The University of Adelaide, SA 5005, Australia
E-mails: bert....@adelaide.edu.au, arami...@gmail.com
Homepage: https://www.adelaide.edu.au/directory/bert.harris
Phone (USA): 1-256-533-0529, Mobile (USA): 1-615-440-0666, Skype: ravenfork

John Baumgartner

unread,
Mar 30, 2012, 7:05:31 PM3/30/12
to max...@googlegroups.com
In Arc, you need to specify a snap raster in order to align the processed grid correctly with the other grids. You can specify this on a per-tool-use basis in the tool's Environments settings (under Raster Processing, perhaps... I don't recall the exact section), or on an application basis via the Geoprocessing menu. You can also specify the output cell size and extent there. 

Hope that helps. 
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.

John Baumgartner

unread,
Mar 30, 2012, 7:39:43 PM3/30/12
to max...@googlegroups.com
Since you asked, Robert Hijman's excellent raster package makes this very straightforward in R, as well...

library(raster)

e <- extent(xmin, xmax, ymin, ymax) # desired final extent

# read in rasters
elev <- raster('path/to/elevation/raster')
rain <- raster('path/to/rainfall/raster')
lc <- raster('path/to/landcover/raster')

elev <- crop(elev, e) # clip elev grid to study area
# resample rain and lc grids to have same res and extent as elev grid
# You can specify method='ngb' for nearest-neighbour (default is bilinear)
# Provide a filename arg to. write the result directly to a file
rain <- resample(rain, elev)
lc <- resample(lc, elev)

# Look at ?writeRaster


On 31/03/2012, at 7:58 AM, Bert Harris <arami...@gmail.com> wrote:

--

Francisco Rodriguez Sanchez

unread,
Apr 2, 2012, 5:59:30 AM4/2/12
to max...@googlegroups.com
Hi,

you can also try the all-in-one "spatial_sync_raster" function in package climstats: http://r-forge.r-project.org/projects/climstats/. It will match the projection, extent and resolution of any reference raster

Cheers

Paco


-- 
Dr Francisco Rodriguez-Sanchez
Forest Ecology and Conservation Group
Department of Plant Sciences
University of Cambridge
Downing Street 
Cambridge CB2 3EA
United Kingdom
http://sites.google.com/site/rodriguezsanchezf

marc fernandez

unread,
Apr 3, 2012, 1:07:57 PM4/3/12
to max...@googlegroups.com
Hi, 

If you want to do it graphically you can use the Interactive Cut tool implemented on SAGA-GIS, very easy and intuitive. I used to clip all my rasters for Maxent and always worked fine!

Cheers,

Marc

2012/4/2 Francisco Rodriguez Sanchez <f.rodrig...@gmail.com>

Bert Harris

unread,
Apr 4, 2012, 9:49:17 PM4/4/12
to max...@googlegroups.com
Hi all,

I received many replies with diverse approaches.

I tried various permutations of snap raster with no success. I was finally able to make it work using Logan's method which is described (briefly) here: http://blogs.esri.com/esri/arcgis/2009/05/06/better-raster-clipping-options-in-arcgis/

Thank you all so much for your help!
Bert

Marc


--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.

--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.

Mike Treglia

unread,
Jul 3, 2012, 2:33:43 AM7/3/12
to max...@googlegroups.com
Hi Paco,

I was trying out the "spatial_sync_raster" function that you suggested, but received a message that R could not find the function... yet there was a Help topic for it. Any suggestions?

Thanks!
Mike
To unsubscribe from this group, send email to maxent+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/maxent?hl=en.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+unsubscribe@googlegroups.com.

John B

unread,
Jul 3, 2012, 3:36:05 AM7/3/12
to max...@googlegroups.com
This is happening for me too. Not sure why. In fact the problem exists for all functions in the climstats package.

You can, however, access the function with:

climstats:::spatial_sync_raster()

(that's a triple colon)

John B

unread,
Jul 3, 2012, 3:59:55 AM7/3/12
to max...@googlegroups.com
Turns out that a NAMESPACE exists in the package but doesn't export any of the functions.

The simple fix is to: 
  1. download the package source (http://download.r-forge.r-project.org/src/contrib/climstats_1.0.tar.gz)
  2. keep extracting the nested archives (e.g. with 7zip) until you have a 'climstats' folder (typically 2 unzips required, except on OS X, which I think extracts the tar.gz to a tar on download)
  3. within the climstats folder, there is a NAMESPACE file. Delete it. (the problem with this file is that if present, it should include exports of all functions etc. that should be available, whereas the existing NAMESPACE is blank. Deleting it will cause a default NAMESPACE - with all functions exported - to be created on installation.)
  4. fire up R, and install the package with: install.packages('c:/full/path/to/climstats/', repos=NULL, type='source')
Should work now.

Michael Treglia

unread,
Jul 3, 2012, 4:12:53 AM7/3/12
to max...@googlegroups.com
Thanks John! I look forward to taking care of that when I'm back in the office in the AM. Glad it wasn't just me with the problem.
On a side-note, is there any documentation for 'climstats'? (other than the help files?) I would like to get a better idea of what other commands are in it.
Cheers,
Mike

To view this discussion on the web visit https://groups.google.com/d/msg/maxent/-/sGcvMCQuwowJ.

To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.

Francisco Rodriguez Sanchez

unread,
Jul 3, 2012, 6:09:56 AM7/3/12
to max...@googlegroups.com
Thanks John. It used to work for me, maybe the problem arose in the latest build.

Mike, I haven't found extended documentation for this package, but you can have a look at the available functions by typing: help(package="climstats")

Cheers

Paco
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To view this discussion on the web visit https://groups.google.com/d/msg/maxent/-/sGcvMCQuwowJ.

To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.
--
You received this message because you are subscribed to the Google Groups "Maxent" group.
To post to this group, send email to max...@googlegroups.com.
To unsubscribe from this group, send email to maxent+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/maxent?hl=en.

Francisco Rodriguez Sanchez

unread,
Sep 25, 2012, 10:57:42 AM9/25/12
to max...@googlegroups.com
Hi,

I paste the code of the spatial_sync_raster function from climstats
package below. If you run this code then you will be able to use the
function even if you're not able to install the package.

Hope this helps

Paco

spatial_sync_raster <- function (unsynced, reference, method = "ngb",
verbose = FALSE)
{
new_projection = projection(reference)
old_projection = projection(unsynced)
new_res = res(reference)
old_res = res(unsynced)
if (new_projection != old_projection | new_res[1] != old_res[1] |
new_res[2] != old_res[2]) {
pr_extent = projectExtent(unsynced, new_projection)
pr_extent <- setExtent(pr_extent, extent(reference))
res(pr_extent) = res(reference)
if (new_projection != old_projection) {
pr <- projectRaster(unsynced, pr_extent, method = method)
}
else {
pr <- raster::resample(unsynced, pr_extent, method = method)
}
}
else {
pr = unsynced
}
expanded_raster = expand(pr, reference)
synced_raster = crop(expanded_raster, reference)
extent(synced_raster) = extent(reference)
return(synced_raster)
}





El 21/09/2012 15:38, Doni Schwalm escribió:
> Hi All:
>
> I, too, am trying to use the spatial_sync_raster function to align a
> series of rasters. When I completed the initial install (from the zip
> file), I subsequently received the error message indicating R couldn't
> find the spatial_sync_raster function. I then followed John's suggestion
> and deleted the NAMESPACE file. Using the command require(climstats), I
> receive the following error message: Failed with error: ‘package
> ‘climstats’ does not have a NAMESPACE and should be re-installed’.
>
> I then tried the install format suggested by John; my command line
> reads:
> install.packages('C:/Users/schwalmd/Documents/R/win-library/2.15/climstats',
> repos=NULL, type='source')
>
> This generated the following error messages:
>
> Installing package(s) into ‘C:/Users/schwalmd/Documents/R/win-library/2.15’
> (as ‘lib’ is unspecified)
> ERROR: cannot install to srcdir for package 'climstats'
> * removing 'C:/Users/schwalmd/Documents/R/win-library/2.15/climstats'
> Warning messages:
> 1: running command 'C:/PROGRA~1/R/R-215~1.1/bin/i386/R CMD INSTALL -l
> "C:/Users/schwalmd/Documents/R/win-library/2.15"
> "C:/Users/schwalmd/Documents/R/win-library/2.15/climstats"' had status 1
> 2: In
> install.packages("C:/Users/schwalmd/Documents/R/win-library/2.15/climstats",
> :
> installation of package
> ‘C:/Users/schwalmd/Documents/R/win-library/2.15/climstats’ had non-zero
> exit status
>
> I also tried a simpler path (c:/WorkSpace/climstats) but received the
> same error messages.
>
> Any suggestions? I am running R v 2.15.1, 32 bit version, on Windows 7.
>
> Thank you!
> Doni
>
>
>
> On Tuesday, July 3, 2012 3:09:56 AM UTC-7, Francisco Rodriguez-Sanchez
> wrote:
>
> Thanks John. It used to work for me, maybe the problem arose in the
> latest build.
>
> Mike, I haven't found extended documentation for this package, but
> you can have a look at the available functions by typing:
> help(package="climstats")
>
> Cheers
>
> Paco
>
>
> El 03/07/2012 09:12, Michael Treglia escribió:
>> Thanks John! I look forward to taking care of that when I'm back
>> in the office in the AM. Glad it wasn't just me with the problem.
>> On a side-note, is there any documentation for 'climstats'? (other
>> than the help files?) I would like to get a better idea of what
>> other commands are in it.
>> Cheers,
>> Mike
>>
>> On Tue, Jul 3, 2012 at 2:59 AM, John B
>> <j.baum...@pgrad.unimelb.edu.au <javascript:>> wrote:
>>
>> Turns out that a NAMESPACE exists in the package but doesn't
>> export any of the functions.
>>
>> The simple fix is to:
>>
>> 1. download the package source
>> (http://download.r-forge.r-project.org/src/contrib/climstats_1.0.tar.gz
>> <http://download.r-forge.r-project.org/src/contrib/climstats_1.0.tar.gz>)
>> 2. keep extracting the nested archives (e.g. with 7zip
>> <http://www.7-zip.org/>) until you have a 'climstats'
>> folder (typically 2 unzips required, except on OS X, which
>> I think extracts the tar.gz to a tar on download)
>> 3. within the climstats folder, there is a NAMESPACE file.
>> Delete it. (the problem with this file is that if present,
>> it should include exports of all functions etc. that
>> should be available, whereas the existing NAMESPACE is
>> blank. Deleting it will cause a default NAMESPACE - with
>> all functions exported - to be created on installation.)
>> 4. fire up R, and install the package with:
>> install.packages('c:/full/path/to/climstats/', repos=NULL,
>> type='source')
>>
>> Should work now.
>>
>>
>> On Tuesday, 3 July 2012 17:36:05 UTC+10, John B wrote:
>>
>> This is happening for me too. Not sure why. In fact the
>> problem exists for all functions in the climstats package.
>>
>> You can, however, access the function with:
>>
>> climstats:::spatial_sync_raster()
>>
>> (that's a triple colon)
>>
>>
>>
>> On Tuesday, 3 July 2012 16:33:43 UTC+10, Mike Treglia wrote:
>>
>> Hi Paco,
>>
>> I was trying out the "spatial_sync_raster" function
>> that you suggested, but received a message that R
>> could not find the function... yet there was a Help
>> topic for it. Any suggestions?
>>
>> Thanks!
>> Mike
>>
>> On Monday, April 2, 2012 2:59:30 AM UTC-7, Francisco
>> Rodriguez-Sanchez wrote:
>>
>> Hi,
>>
>> you can also try the all-in-one
>> "spatial_sync_raster" function in package
>> climstats:
>> http://r-forge.r-project.org/projects/climstats/
>> <http://r-forge.r-project.org/projects/climstats/>. It
>> will match the projection, extent and resolution
>> of any reference raster
>>
>> Cheers
>>
>> Paco
>>
>>
>> --
>> Dr Francisco Rodriguez-Sanchez
>> Forest Ecology and Conservation Group
>> Department of Plant Sciences
>> University of Cambridge
>> Downing Street
>> Cambridge CB2 3EA
>> United Kingdom
>> http://sites.google.com/site/rodriguezsanchezf <http://sites.google.com/site/rodriguezsanchezf>
>>
>>
>> El 31/03/2012 00:39, John Baumgartner escribió:
>>> Since you asked, Robert Hijman's excellent raster
>>> package makes this very straightforward in R, as
>>> well...
>>>
>>> library(raster)
>>>
>>> e <- extent(xmin, xmax, ymin, ymax) # desired
>>> final extent
>>>
>>> # read in rasters
>>> elev <- raster('path/to/elevation/raster')
>>> rain <- raster('path/to/rainfall/raster')
>>> lc <- raster('path/to/landcover/raster')
>>>
>>> elev <- crop(elev, e) # clip elev grid to study area
>>> # resample rain and lc grids to have same res and
>>> extent as elev grid
>>> # You can specify method='ngb' for
>>> nearest-neighbour (default is bilinear)
>>> # Provide a filename arg to. write the result
>>> directly to a file
>>> rain <- resample(rain, elev)
>>> lc <- resample(lc, elev)
>>>
>>> # Look at ?writeRaster
>>>
>>>
>>> On 31/03/2012, at 7:58 AM, Bert Harris
>>>> E-mails: bert....@adelaide.edu.au <javascript:>,
>>>> arami...@gmail.com <javascript:>
>>>> Homepage:
>>>> https://www.adelaide.edu.au/directory/bert.harris <https://www.adelaide.edu.au/directory/bert.harris>
>>>> Phone (USA): 1-256-533-0529, Mobile (USA):
>>>> 1-615-440-0666, Skype: ravenfork
>>>> --
>>>> You received this message because you are
>>>> subscribed to the Google Groups "Maxent" group.
>>>> To post to this group, send email to
>>>> max...@googlegroups.com <javascript:>.
>>>> To unsubscribe from this group, send email to
>>>> maxent+un...@googlegroups.com <javascript:>.
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/maxent?hl=en
>>>> <http://groups.google.com/group/maxent?hl=en>.
>>> --
>>> You received this message because you are
>>> subscribed to the Google Groups "Maxent" group.
>>> To post to this group, send email to
>>> max...@googlegroups.com <javascript:>.
>>> To unsubscribe from this group, send email to
>>> maxent+un...@googlegroups.com <javascript:>.
>>> For more options, visit this group at
>>> http://groups.google.com/group/maxent?hl=en
>>> <http://groups.google.com/group/maxent?hl=en>.
>>
>> --
>> You received this message because you are subscribed to the
>> Google Groups "Maxent" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/maxent/-/sGcvMCQuwowJ
>> <https://groups.google.com/d/msg/maxent/-/sGcvMCQuwowJ>.
>>
>> To post to this group, send email to max...@googlegroups.com
>> <javascript:>.
>> To unsubscribe from this group, send email to
>> maxent+un...@googlegroups.com <javascript:>.
>> For more options, visit this group at
>> http://groups.google.com/group/maxent?hl=en
>> <http://groups.google.com/group/maxent?hl=en>.
>>
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Maxent" group.
>> To post to this group, send email to max...@googlegroups.com
>> <javascript:>.
>> To unsubscribe from this group, send email to
>> maxent+un...@googlegroups.com <javascript:>.
>> For more options, visit this group at
>> http://groups.google.com/group/maxent?hl=en
>> <http://groups.google.com/group/maxent?hl=en>.
>
> --
> Dr Francisco Rodriguez-Sanchez
> Forest Ecology and Conservation Group
> Department of Plant Sciences
> University of Cambridge
> Downing Street
> Cambridge CB2 3EA
> United Kingdom
> http://sites.google.com/site/rodriguezsanchezf <http://sites.google.com/site/rodriguezsanchezf>
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Maxent" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/maxent/-/TfMNekxnSMIJ.
Reply all
Reply to author
Forward
0 new messages