Same mask for two rasters, but obtaining different extents in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Same mask for two rasters, but obtaining different extents in R

R-sig-geo mailing list
I loaded a shapefile in R, and cropped and masked it with the same mask,
but I get different extents. Any suggestions?

Code below:

    library(raster)
    library(rgdal)
    library(GISTools)
    library(sp)
    library(maptools)

    ##Loading the first mask
    Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate
Stability\\SPV_Mask\\mask.shp")
    proj4string(Mask) <- CRS("+init=epsg:4326")

    #Loading the Rasters For Present and LGM and clipping them to the right
extent
    bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
    proj4string(bio5) <- CRS("+init=epsg:4326")
    lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
    proj4string(lg5) <- CRS("+init=epsg:4326")

    ##Cropping by using the Crop and Mask functions
    cr<- crop(bio5, extent(Mask))
    bio5 <- mask(cr, Mask)

    cr2<- crop(lg5, extent(Mask))
    lg5<- mask(cr2, Mask)

    > bio5
     class       : RasterLayer
     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
     resolution  : 0.04166667, 0.04166667  (x, y)
     extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax,
ymin, ymax)
     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
+ellps=WGS84 +towgs84=0,0,0
     data source : in memory
     names       : bio_5
     values      : 207, 432  (min, max)
     attributes  :
        ID COUNT
     from: -86     1
     to  : 489    38

     > lg5
     class       : RasterLayer
     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
     resolution  : 0.04166667, 0.04166667  (x, y)
     extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax,
ymin, ymax)
     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
+ellps=WGS84 +towgs84=0,0,0
     data source : in memory
     names       : cclgmbi5
     values      : 187, 392  (min, max)


--
Data Manager,
Barbara Han Lab,
Cary Institute of Ecosystem Studies,
2801 Sharon Turnpike, Millbrook, NY 12545
Lab Site : http://www.hanlab.science/
Personal Site : http://evolecol.weebly.com/
Phone : (845)-677-7600 Ext: 241

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Same mask for two rasters, but obtaining different extents in R

Ben Tupper
Hi,

Might you show us what bio5 and lg5 look like before you do the masking?

Cheers,
Ben


> On Mar 14, 2017, at 5:31 PM, Vijay Ramesh via R-sig-Geo <[hidden email]> wrote:
>
> I loaded a shapefile in R, and cropped and masked it with the same mask,
> but I get different extents. Any suggestions?
>
> Code below:
>
>    library(raster)
>    library(rgdal)
>    library(GISTools)
>    library(sp)
>    library(maptools)
>
>    ##Loading the first mask
>    Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\SPV_Mask\\mask.shp")
>    proj4string(Mask) <- CRS("+init=epsg:4326")
>
>    #Loading the Rasters For Present and LGM and clipping them to the right
> extent
>    bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
>    proj4string(bio5) <- CRS("+init=epsg:4326")
>    lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
>    proj4string(lg5) <- CRS("+init=epsg:4326")
>
>    ##Cropping by using the Crop and Mask functions
>    cr<- crop(bio5, extent(Mask))
>    bio5 <- mask(cr, Mask)
>
>    cr2<- crop(lg5, extent(Mask))
>    lg5<- mask(cr2, Mask)
>
>> bio5
>     class       : RasterLayer
>     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>     resolution  : 0.04166667, 0.04166667  (x, y)
>     extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax,
> ymin, ymax)
>     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> +ellps=WGS84 +towgs84=0,0,0
>     data source : in memory
>     names       : bio_5
>     values      : 207, 432  (min, max)
>     attributes  :
>        ID COUNT
>     from: -86     1
>     to  : 489    38
>
>> lg5
>     class       : RasterLayer
>     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>     resolution  : 0.04166667, 0.04166667  (x, y)
>     extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax,
> ymin, ymax)
>     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> +ellps=WGS84 +towgs84=0,0,0
>     data source : in memory
>     names       : cclgmbi5
>     values      : 187, 392  (min, max)
>
>
> --
> Data Manager,
> Barbara Han Lab,
> Cary Institute of Ecosystem Studies,
> 2801 Sharon Turnpike, Millbrook, NY 12545
> Lab Site : http://www.hanlab.science/
> Personal Site : http://evolecol.weebly.com/
> Phone : (845)-677-7600 Ext: 241
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Same mask for two rasters, but obtaining different extents in R

Sarah Goslee
In reply to this post by R-sig-geo mailing list
The most likely explanation seems to me that the rasters were not
aligned before cropping, so they are not aligned after cropping.

Have you checked the original extent and resolution?

Sarah

On Tue, Mar 14, 2017 at 5:31 PM, Vijay Ramesh via R-sig-Geo
<[hidden email]> wrote:

> I loaded a shapefile in R, and cropped and masked it with the same mask,
> but I get different extents. Any suggestions?
>
> Code below:
>
>     library(raster)
>     library(rgdal)
>     library(GISTools)
>     library(sp)
>     library(maptools)
>
>     ##Loading the first mask
>     Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\SPV_Mask\\mask.shp")
>     proj4string(Mask) <- CRS("+init=epsg:4326")
>
>     #Loading the Rasters For Present and LGM and clipping them to the right
> extent
>     bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
>     proj4string(bio5) <- CRS("+init=epsg:4326")
>     lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
>     proj4string(lg5) <- CRS("+init=epsg:4326")
>
>     ##Cropping by using the Crop and Mask functions
>     cr<- crop(bio5, extent(Mask))
>     bio5 <- mask(cr, Mask)
>
>     cr2<- crop(lg5, extent(Mask))
>     lg5<- mask(cr2, Mask)
>
>     > bio5
>      class       : RasterLayer
>      dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>      resolution  : 0.04166667, 0.04166667  (x, y)
>      extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax,
> ymin, ymax)
>      coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> +ellps=WGS84 +towgs84=0,0,0
>      data source : in memory
>      names       : bio_5
>      values      : 207, 432  (min, max)
>      attributes  :
>         ID COUNT
>      from: -86     1
>      to  : 489    38
>
>      > lg5
>      class       : RasterLayer
>      dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>      resolution  : 0.04166667, 0.04166667  (x, y)
>      extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax,
> ymin, ymax)
>      coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> +ellps=WGS84 +towgs84=0,0,0
>      data source : in memory
>      names       : cclgmbi5
>      values      : 187, 392  (min, max)
>
>

--
Sarah Goslee
http://www.functionaldiversity.org

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Same mask for two rasters, but obtaining different extents in R

R-sig-geo mailing list
I re-checked the original extent and here it is: This is data obtained from
WorldClim.

> bio5
class       : RasterLayer
dimensions  : 3600, 8640, 31104000  (nrow, ncol, ncell)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : -180, 180, -60, *90.00001 * (xmin, xmax, ymin, ymax) #This
varies at the 6th decimal place.
coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
data source : C:\Users\rameshv\Downloads\Climate
Stability\Data_LGM_Present\Present\1_RawData\bio_5
names       : bio_5
values      : -86, 489  (min, max)
attributes  :
        ID COUNT
 from: -86     1
 to  : 489    38


> lg5
class       : RasterLayer
dimensions  : 3600, 8640, 31104000  (nrow, ncol, ncell)
resolution  : 0.04166667, 0.04166667  (x, y)
extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
data source : C:\Users\rameshv\Downloads\Climate
Stability\Data_LGM_Present\LGM\1_RawData\cclgmbi5.tif
names       : cclgmbi5
values      : -204, 519  (min, max)

What do you suggest?

Also, any suggestions on aligning them before I crop them? I wouldn't want
to projectRaster using one of the rasters, as that creates extra number of
rows (when I coerce the raster to a Spatial Pixels Data Frame).



On Wed, Mar 15, 2017 at 1:13 PM, Sarah Goslee <[hidden email]>
wrote:

> The most likely explanation seems to me that the rasters were not
> aligned before cropping, so they are not aligned after cropping.
>
> Have you checked the original extent and resolution?
>
> Sarah
>
> On Tue, Mar 14, 2017 at 5:31 PM, Vijay Ramesh via R-sig-Geo
> <[hidden email]> wrote:
> > I loaded a shapefile in R, and cropped and masked it with the same mask,
> > but I get different extents. Any suggestions?
> >
> > Code below:
> >
> >     library(raster)
> >     library(rgdal)
> >     library(GISTools)
> >     library(sp)
> >     library(maptools)
> >
> >     ##Loading the first mask
> >     Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate
> > Stability\\SPV_Mask\\mask.shp")
> >     proj4string(Mask) <- CRS("+init=epsg:4326")
> >
> >     #Loading the Rasters For Present and LGM and clipping them to the
> right
> > extent
> >     bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> > Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
> >     proj4string(bio5) <- CRS("+init=epsg:4326")
> >     lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
> > Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
> >     proj4string(lg5) <- CRS("+init=epsg:4326")
> >
> >     ##Cropping by using the Crop and Mask functions
> >     cr<- crop(bio5, extent(Mask))
> >     bio5 <- mask(cr, Mask)
> >
> >     cr2<- crop(lg5, extent(Mask))
> >     lg5<- mask(cr2, Mask)
> >
> >     > bio5
> >      class       : RasterLayer
> >      dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
> >      resolution  : 0.04166667, 0.04166667  (x, y)
> >      extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax,
> > ymin, ymax)
> >      coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> > +ellps=WGS84 +towgs84=0,0,0
> >      data source : in memory
> >      names       : bio_5
> >      values      : 207, 432  (min, max)
> >      attributes  :
> >         ID COUNT
> >      from: -86     1
> >      to  : 489    38
> >
> >      > lg5
> >      class       : RasterLayer
> >      dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
> >      resolution  : 0.04166667, 0.04166667  (x, y)
> >      extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax,
> > ymin, ymax)
> >      coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
> > +ellps=WGS84 +towgs84=0,0,0
> >      data source : in memory
> >      names       : cclgmbi5
> >      values      : 187, 392  (min, max)
> >
> >
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>



--
Data Manager,
Barbara Han Lab,
Cary Institute of Ecosystem Studies,
2801 Sharon Turnpike, Millbrook, NY 12545
Lab Site : http://www.hanlab.science/
Personal Site : http://evolecol.weebly.com/
Phone : (845)-677-7600 Ext: 241

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Same mask for two rasters, but obtaining different extents in R

Ben Tupper
Hi,

In the short term you could use raster::alignExtent() to get align bio5 to the the extent of lg5.  

I would also inspect the source of your data and compare it to your locally stored copy of it.  If they are different then it would be worthwhile to review the steps you used to download, manipulate and store it.

Cheers,
Ben

 

> On Mar 15, 2017, at 1:21 PM, Vijay Ramesh via R-sig-Geo <[hidden email]> wrote:
>
> I re-checked the original extent and here it is: This is data obtained from
> WorldClim.
>
>> bio5
> class       : RasterLayer
> dimensions  : 3600, 8640, 31104000  (nrow, ncol, ncell)
> resolution  : 0.04166667, 0.04166667  (x, y)
> extent      : -180, 180, -60, *90.00001 * (xmin, xmax, ymin, ymax) #This
> varies at the 6th decimal place.
> coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
> data source : C:\Users\rameshv\Downloads\Climate
> Stability\Data_LGM_Present\Present\1_RawData\bio_5
> names       : bio_5
> values      : -86, 489  (min, max)
> attributes  :
>        ID COUNT
> from: -86     1
> to  : 489    38
>
>
>> lg5
> class       : RasterLayer
> dimensions  : 3600, 8640, 31104000  (nrow, ncol, ncell)
> resolution  : 0.04166667, 0.04166667  (x, y)
> extent      : -180, 180, -60, 90  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs
> data source : C:\Users\rameshv\Downloads\Climate
> Stability\Data_LGM_Present\LGM\1_RawData\cclgmbi5.tif
> names       : cclgmbi5
> values      : -204, 519  (min, max)
>
> What do you suggest?
>
> Also, any suggestions on aligning them before I crop them? I wouldn't want
> to projectRaster using one of the rasters, as that creates extra number of
> rows (when I coerce the raster to a Spatial Pixels Data Frame).
>
>
>
> On Wed, Mar 15, 2017 at 1:13 PM, Sarah Goslee <[hidden email]>
> wrote:
>
>> The most likely explanation seems to me that the rasters were not
>> aligned before cropping, so they are not aligned after cropping.
>>
>> Have you checked the original extent and resolution?
>>
>> Sarah
>>
>> On Tue, Mar 14, 2017 at 5:31 PM, Vijay Ramesh via R-sig-Geo
>> <[hidden email]> wrote:
>>> I loaded a shapefile in R, and cropped and masked it with the same mask,
>>> but I get different extents. Any suggestions?
>>>
>>> Code below:
>>>
>>>    library(raster)
>>>    library(rgdal)
>>>    library(GISTools)
>>>    library(sp)
>>>    library(maptools)
>>>
>>>    ##Loading the first mask
>>>    Mask <- readOGR("C:\\Users\\rameshv\\Downloads\\Climate
>>> Stability\\SPV_Mask\\mask.shp")
>>>    proj4string(Mask) <- CRS("+init=epsg:4326")
>>>
>>>    #Loading the Rasters For Present and LGM and clipping them to the
>> right
>>> extent
>>>    bio5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
>>> Stability\\Data_LGM_Present\\Present\\1_RawData\\bio_5")
>>>    proj4string(bio5) <- CRS("+init=epsg:4326")
>>>    lg5 <- raster("C:\\Users\\rameshv\\Downloads\\Climate
>>> Stability\\Data_LGM_Present\\LGM\\1_RawData\\cclgmbi5.tif")
>>>    proj4string(lg5) <- CRS("+init=epsg:4326")
>>>
>>>    ##Cropping by using the Crop and Mask functions
>>>    cr<- crop(bio5, extent(Mask))
>>>    bio5 <- mask(cr, Mask)
>>>
>>>    cr2<- crop(lg5, extent(Mask))
>>>    lg5<- mask(cr2, Mask)
>>>
>>>> bio5
>>>     class       : RasterLayer
>>>     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>>>     resolution  : 0.04166667, 0.04166667  (x, y)
>>>     extent      : 72.45835, 82.70835, 8.083337, 22.20834  (xmin, xmax,
>>> ymin, ymax)
>>>     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
>>> +ellps=WGS84 +towgs84=0,0,0
>>>     data source : in memory
>>>     names       : bio_5
>>>     values      : 207, 432  (min, max)
>>>     attributes  :
>>>        ID COUNT
>>>     from: -86     1
>>>     to  : 489    38
>>>
>>>> lg5
>>>     class       : RasterLayer
>>>     dimensions  : 339, 246, 83394  (nrow, ncol, ncell)
>>>     resolution  : 0.04166667, 0.04166667  (x, y)
>>>     extent      : 72.45833, 82.70833, 8.083333, 22.20833  (xmin, xmax,
>>> ymin, ymax)
>>>     coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs
>>> +ellps=WGS84 +towgs84=0,0,0
>>>     data source : in memory
>>>     names       : cclgmbi5
>>>     values      : 187, 392  (min, max)
>>>
>>>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>>
>
>
>
> --
> Data Manager,
> Barbara Han Lab,
> Cary Institute of Ecosystem Studies,
> 2801 Sharon Turnpike, Millbrook, NY 12545
> Lab Site : http://www.hanlab.science/
> Personal Site : http://evolecol.weebly.com/
> Phone : (845)-677-7600 Ext: 241
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Loading...