[R-sig-Geo] spsample() ok with type= 'stratified', but gives error with 'random'
Roger Bivand
Roger.Bivand at nhh.no
Thu Jun 7 21:53:27 CEST 2007
On Thu, 7 Jun 2007, Don MacQueen wrote:
> I don't know enough to assess whether I have a misunderstanding or a mistake.
>
> spsample() with type='random' is giving an error, but succeeding with
> type='stratified'.
>
> Maybe random sampling requires projection information and stratified doesn't?
> I may have constructed my SpatialPolygonsDataFrame incorrectly?
The problem is that the hole well-nigh fills the polygon, and after iter=4
tries, it tries to finish, and fails, because the output object is NULL
rather than some points. Increasing iter= doesn't help. It shouldn't do
this, but it does - I'll try to fix it but not soon.
A work-around is:
plot(SP)
r1 <- spsample(as(SP, "Spatial"), type="random", n=300)
points(r1)
r2 <- overlay(SP, r1)
points(r1[!is.na(r2)], col="red")
length(which(!is.na(r2))) >= 50
r3 <- sample(which(!is.na(r2)), 50)
length(r3)
points(r1[r3], pch=19, col="red")
Most likely spsurvey and/or making an appropriate window in spatstat will
do this better.
Thanks for a clear report that was easy to reproduce.
Roger
>
> Thanks
> -Don
>
> Details:
>
> spgd consists of two rectangles, one inside the other, and the inside
> one is a hole.
> The outer one has vertices in counter-clockwise order; the inner one
> in clockwise order.
>
> > class(tst)
> [1] "SpatialPolygonsDataFrame"
> attr(,"package")
> [1] "sp"
>
> > summary(tst)
> Object of class SpatialPolygonsDataFrame
> Coordinates:
> min max
> r1 990396.8 990762.3
> r2 213369.4 213702.6
> Is projected: NA
> proj4string : [NA]
> Data attributes:
> ID
> zone:1
>
>
> > stst <- spsample(tst,type='stratified',n=50)
> > plot(tst)
> > points(stst)
> > stst <- spsample(tst,type='random',n=50)
> Error in `proj4string<-`(`*tmp*`, value = <S4 object of class "CRS">) :
> proj4string only works for class(es extending) Spatial
>
>
> ##########
> > sessionInfo()
> R version 2.5.0 (2007-04-23)
> powerpc-apple-darwin8.9.1
>
> locale:
> C
>
> attached base packages:
> [1] "stats" "graphics" "grDevices" "utils" "datasets"
> "methods" "base"
>
> other attached packages:
> maptools sp foreign rmacq
> "0.6-12" "0.9-14" "0.8-20" "1.0"
>
> ##########
> ## since tst is fairly small, here it is:
> ##########
> > tst
> An object of class "SpatialPolygonsDataFrame"
> Slot "data":
> ID
> zone zone
>
> Slot "polygons":
> [[1]]
> An object of class "Polygons"
> Slot "Polygons":
> $zn
> An object of class "Polygon"
> Slot "labpt":
> [1] 990579.4 213536.2
>
> Slot "area":
> [1] 64601.13
>
> Slot "hole":
> [1] FALSE
>
> Slot "ringDir":
> [1] 1
>
> Slot "coords":
> x y
> [1,] 990499.3 213702.6
> [2,] 990762.3 213559.8
> [3,] 990658.2 213369.4
> [4,] 990396.8 213513.2
> [5,] 990499.3 213702.6
>
>
> $zn2
> An object of class "Polygon"
> Slot "labpt":
> [1] 990584.4 213537.3
>
> Slot "area":
> [1] 30367.20
>
> Slot "hole":
> [1] TRUE
>
> Slot "ringDir":
> [1] -1
>
> Slot "coords":
> x y
> [1,] 990492.6 213650.8
> [2,] 990441.8 213549.4
> [3,] 990673.3 213423.7
> [4,] 990728.1 213525.6
> [5,] 990492.6 213650.8
>
>
>
> Slot "plotOrder":
> [1] 1 2
>
> Slot "labpt":
> [1] 990579.4 213536.2
>
> Slot "ID":
> [1] "zone"
>
> Slot "area":
> [1] 94968.33
>
>
>
> Slot "plotOrder":
> [1] 1
>
> Slot "bbox":
> min max
> r1 990396.8 990762.3
> r2 213369.4 213702.6
>
> Slot "proj4string":
> CRS arguments: NA
>
>
--
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