[R-sig-Geo] clip a SpatialPolygonsDataFrame

Roger Bivand Roger.Bivand at nhh.no
Fri Aug 6 13:13:43 CEST 2010


On Fri, 6 Aug 2010, Laetitia Blanc wrote:

>
>
>
> Can the KDE be run with the correct coordinates in the specified 
> projection on the input data, so that thw window matches the outlines? Can 
> you re-write the code making res to generate a SpatialGridDataFrame? The 
> input points must have a projection. Without a shared projection (CRS, 
> coordinate reference system) this is not possible.
>
> Roger
>
>> I have taken into account your advice.
>> I have loaded my input data with the correct CRS (lambert II) in order to
> match with the map :
>
> d <- read.csv('total.csv', header = TRUE) #, sep = ",", quote="\"",
> dec=".",fill = TRUE, comment.char="", ...)
>
> d <- d[-(1:10),]
>
> d2 <- data.frame (d)
> spd2 <- d2
>
> #transformation into SpatialPointDataFrame
> coordinates ( spd2 ) <- ~ x + y
> proj4string ( spd2 ) <- CRS("+init=epsg:27572")
>
>> Then, i have realised my KDE :
>
> source("stkde2.txt")
>
> res <- array(0,c(20,20,length(unique(spd2$an)))) # créer 30
> (length(unique(d$an))) tableaux de 20x20.
> for (i in 1:length(unique(spd2$an))) { 
> spd3 <- subset(spd2,spd2$an == 1979+i) # résumé en conservant que 1973+i où
> i=30 donc en gardant que 2009
> lynxkde<-stkde(xlong=spd3$x, ylat=spd3$y, ztime=spd3$an,
> xgrids=20,ygrids=20,bwmethod="cv.ml") 
> res[,,i] <- lynxkde$dens
> } 
>
>> At that moment, the window where the KDE is running has te correct unit
> (meters) (xrange : >from 823 000 to 981 000 ; yrange : from 2 079 500 to 2
> 305 500)

No, this is a misunderstanding. Until you get this right, please forget 
the simulation, make sure everything is correct for a single period 
first. I am pretty sure that lynxkde$dens is a matrix, and if you 
take the trouble to read help(image), you'll see that if the first 
argument is a matrix, then the window is set to 0,1. You need to pass the 
cell centres of the xgrids and ygrids to the image call. Beware that if 
the spd3$x and spd3$y change by i, the grid will be different each time, 
evenj though it is 20x20, it will start somewhere else, and have a 
different cell dimension.

I think that you are using a function from the stam package - you need to 
adapt it to pass the x.seq and y.seq values now generated internally in as 
arguments, which will permit you to make sure that you have full control 
of the location of the output grids. If you then pass the same x.seq and 
y.seq to image() as the first two arguments, and the matrix as the third 
argument, it will use them to specify its axes, giving you the correct 
window. You need to be sure that your x.seq and y.seq contain all of the 
xlong and xlat for all periods. If stkde took a GridTopology object as a 
grid definition, the output could easily be used to create a 
SpatialGridDataFrame object, which would be very easy to plot in register 
with your boundaries, but this is something for the package maintainer.

Hope this helps,

Roger

PS. CC-ing the stam maintainer.

>
>> But then, when i create my animation, the window has the x and y ranges :
> 0:1. I think the >probleme comes from my animation. Moreover, when i try to
> insert my map in my animation, i still >have an alternation between the map
> and the kernel >rather than the kernel ON the map (like
>> overlay/superposition...) :
>
>
> library(animation)
> library(MASS)
>
> # create a HTML page from my animation :
> pres.ani <- function() 
> { 
> for (i in 1:length(unique(spd2$an))) { 
> image(res[,,i],xlab='',ylab='',main=unique(spd2$an)[i]) 
> contour(res[,,i],add=T) 
> plot(france,xlim=range(823000:981000),ylim=range(2079500:2305500),add=TRUE) 
> Sys.sleep(.5) 
> } 
>
> } 
> ani.start(interval=.5,title = "movie presence lynx", outdir = getwd()) 
>
> pres.ani() 
> ani.stop() 
>
>> I think i must solve the problem by changing my animation but i don't know
> how. Do you have an >idea ? Do you think that it can be because i have
> spatial objects ?
> -- 
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/clip-a-SpatialPolygonsDataFrame-tp5376909p5379892.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

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