[R-sig-Geo] Subscripting by coordinates

Roger Bivand Roger.Bivand at nhh.no
Fri Jun 15 22:57:08 CEST 2007


On Fri, 15 Jun 2007, Agustin Lobo wrote:

> Hi r-sig-geos,
>
> I'm starting to use package sp
> and can't figure how to subscript an sp grid
> object by coordinates. This is what I do:
>
> First, I read the raster image of the example:
>
> x <- GDAL.open(system.file("pictures/erdas_spnad83.tif", package =
> "rgdal")[1])
> erdas_spnad83 <- asSGDF_GROD(x, output.dim=c(100,100))
> GDAL.close(x)
>
> Then I simplify to only 4 levels
> test <- erdas_spnad83
> test2 <- erdas_spnad83 at data
> test2[test2<50]<- 1
> test2[test2>1 & test2 <100]<- 2
> test2[test2>2 & test2 <150]<- 3
> test2[test2>=150]<- 4
> test at data <- test2
>
> and make an small example
> test3 <- test[1:10,11:20]
>
> and then perform an stratified sampling:
> test3.stsamp <- spsample(test3, n = 10, "stratified")
>
> Now, I would like to get the values of test3 at positions
> test3.stsamp at coords,
> but
> test3[test3.stsamp at coords]
>
> does not work. How should I do it?

o <- overlay(test3, test3.stsamp)

works for me, returning a SpatialPointsDataFrame. Note that test2 is a 
data frame with column band1, so some of your code is potentially 
misleading:

test <- erdas_spnad83
names(test)
test$cut <- as.ordered(cut(test$band1, c(0,50,100,150,255),
   include.lowest=TRUE))
test3 <- test[1:10,11:20]
test3.stsamp <- spsample(test3, n = 10, "stratified")
o <- overlay(test3, test3.stsamp)
summary(o)

feels safer - the test2 assignments are only working because the data 
frame has one column.

Hope this helps,

Roger

>
> Thanks
>
> Agus
>
>
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list