[R-sig-Geo] Iteratively subsample a rater and reassemble

Jonathan Greenberg jgrn at illinois.edu
Mon Mar 31 20:27:04 CEST 2014


Ok, so I think the way I described may work for what you are doing --
basically, make a raster where each pixel is the per-country
population value via ?subs linking the country code with the
population.  From that, you will have, for each pixel, an estimate for
the full country population, and a corresponding ACTUAL population for
each pixel, with which you can now do band math.

Given a raster of two countries A and B:

A A A B B B
A A A A B B
A A B B B B

And data.frame:
ID Population
A  100
B  200

A ?subs will return a raster:

100 100 100 200 200 200
100 100 100 100 200 200
100 100 200 200 200 200

A more brute-force (assuming you have enough memory) is to ?getValues
the entire raster, and convert it into a data.frame.  For small
rasters this is fine, for bigger rasters this is a more serious issue.

--j

On Mon, Mar 31, 2014 at 12:12 PM, Gordon Holtgrieve <gholt at uw.edu> wrote:
> Thanks for the reply Jonathan.
>
> I am actually trying to determine the relative population density spatially
> within each country, where the cell with the mean/median number of people
> set is to 0 (excluding cells with no population) and the variance from that
> is in units of SD.  This is the same as the scale() function.   I just want
> to do it on a per country basis rather than for the whole raster.
>
> Gordon
>
>
> On Mon, Mar 31, 2014 at 9:11 AM, Jonathan Greenberg <jgrn at illinois.edu>
> wrote:
>>
>> Gordon:
>>
>> To clarify, you are trying to determine a relative population density
>> per country, correct?  I think you should look at the ?subs function,
>> which will make it a lot easier to swap the country ID with the
>> population value -- you just need a data.frame of ID and population.
>> Then it seems if you create a new raster that has the population of
>> the country for each cell, you can simply divide the per-location
>> population by that raster.
>>
>> --j
>>
>> On Mon, Mar 31, 2014 at 12:12 AM, Gordon Holtgrieve <gholt at uw.edu> wrote:
>> > Hello,
>> >
>> > I have not worked with rasters much in R and am having a bit of trouble.
>> >
>> > I have a raster of global population and a second raster of countries
>> > identified by UN code (both at 1/2 degree resolution, but with different
>> > extents).  Ultimately I would like to end up with a raster where each
>> > country's population is rescaled to N(0,1).
>> >
>> > Can someone suggest the best way to do this?
>> >
>> > So far I have been able to mask the population raster to a given country
>> > and perform the rescale (with the plan to loop over countries) .  I'm
>> > stuck
>> > on how to "reassemble" into a complete raster with all the countries.
>> >
>> > #Code thus far -- although I'm sure there is a better way.
>> > rasterCountriesUN[rasterCountriesUN != 840] <- NA  #Mask countries not
>> > of
>> > interest
>> >
>> > rasterCountriesUN[rasterCountriesUN == 840] <- 1  #replace the UN code
>> > with
>> > 1
>> >
>> > cropPop2010 <- rasterCountriesUN*rasterPop2010  # crop population data
>> > raster
>> >
>> > cropPop2010[cropPop2010>0] <- scale(cropPop2010[cropPop2010>0]) #rescale
>> > cells > 0 (i.e., with people)
>> > # I have other calculations to do here using the scaled population data.
>> >
>> > ###??? How do I store these results and move to the next country?  Or is
>> > there  better way to go about this?
>> >
>> >
>> > Many thanks,
>> >
>> > --
>> > Gordon Holtgrieve, Ph.D.
>> > Assistant Professor
>> > University of Washington
>> > School of Aquatic & Fishery Sciences
>> > 1122 NE Boat Street
>> > Box 355020
>> > Seattle, WA 98105 USA
>> > 1-206-616-7041
>> > skype: gholtgrieve
>> >
>> >         [[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
>
>
>
>
> --
> Gordon Holtgrieve, Ph.D.
> Assistant Professor
> University of Washington
> School of Aquatic & Fishery Sciences
> 1122 NE Boat Street
> Box 355020
> Seattle, WA 98105 USA
> 1-206-616-7041
> skype: gholtgrieve



-- 
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