[R-sig-Geo] problem calculating the proportion of overlap between two geographic rasters

Jonathan Greenberg jgrn at illinois.edu
Mon Mar 24 00:05:03 CET 2014


Argus:

Can't you just use the ?freq function for this purpose?

--j

On Sun, Mar 23, 2014 at 4:39 PM, Agus Camacho <agus.camacho at gmail.com> wrote:
> Hello list,
>
> Im trying to calculate the porportion of overlap among two rasters. I think
> i got it done, but when I apply the script on identical rasters, the
> proportion obtained is not 1!
> Someone could please explain me what I could be missing here?
>
> Here comes a reproducible example:
>
> library(raster)
>
> #generate 2 identical  raster layers b and d that take values 0 and 1.
>
> wgs84 <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
> d <- raster()
> extent(d) <- extent(119.4993, 141.0993, -36.65831, -29.85831)
> res(d) <- c(0.08, 0.08)
> projection(d) <- CRS(wgs84)
> values(d) <- sample(c(0, 1), ncell(d), replace=TRUE)
> b=d
>
> # calculate each layers' area
> a=raster::area(d)
> s=cellStats(a, 'sum')
>
> #sum both layers, so as to have value 2 were both layers present value 1
> soma=sum(b,d)
> overl=sum(values(area(soma))[which(values(soma)==2)])# sum cell sizes of
> cells with value 2 within soma
>
> overl/s# i think this should give 1 but it does not.
>
> # visualizing
> library(maps)
> map(regions = "australia")
> image(d, add = TRUE, col = "blue")
> image(soma, add = TRUE, col = "green")
>
>
> Thanks in advance!
> Agus
> --
> Agustín Camacho Guerrero.
> Doutor em Zoologia.
> Laboratório de Herpetologia, Departamento de Zoologia, Instituto de
> Biociências, USP.
> Rua do Matão, trav. 14, nº 321, Cidade Universitária,
> São Paulo - SP, CEP: 05508-090, Brasil.
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>



-- 
Jonathan A. Greenberg, PhD
Assistant Professor
Global Environmental Analysis and Remote Sensing (GEARS) Laboratory
Department of Geography and Geographic Information Science
University of Illinois at Urbana-Champaign
259 Computing Applications Building, MC-150
605 East Springfield Avenue
Champaign, IL  61820-6371
Phone: 217-300-1924
http://www.geog.illinois.edu/~jgrn/
AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307, Skype: jgrn3007



More information about the R-sig-Geo mailing list