1\name{as.ppp} 2\alias{as.ppp.SpatialPoints} 3\alias{coerce,SpatialPoints,ppp-method} 4\alias{as.ppp.SpatialPointsDataFrame} 5\alias{coerce,SpatialPointsDataFrame,ppp-method} 6 7\alias{as.SpatialPoints.ppp} 8\alias{coerce,ppp,SpatialPoints-method} 9\alias{as.SpatialPointsDataFrame.ppp} 10\alias{coerce,ppp,SpatialPointsDataFrame-method} 11\alias{as.SpatialGridDataFrame.ppp} 12\alias{coerce,ppp,SpatialGridDataFrame-method} 13 14\title{Coercion between sp objects and spatstat ppp objects} 15\description{ 16Functions to convert between \pkg{spatstat}s planar point pattern (\code{ppp}) 17format and \pkg{sp}s \code{SpatialPoints} and \code{SpatialPointsDataFrame} as 18well as one-way conversion from \code{SpatialGridDataFrame} to \code{ppp}. 19S4-style \code{as()} coercion can be used as well. 20} 21\section{Methods}{ 22 \describe{ 23\item{coerce}{\code{signature(from = "SpatialPoints", to = "ppp")}} 24\item{coerce}{\code{signature(from = "SpatialPointsDataFrame", to = "ppp")}} 25\item{coerce}{\code{signature(from = "ppp", to = "SpatialGridDataFrame")}} 26\item{coerce}{\code{signature(from = "ppp", to = "SpatialPointsDataFrame")}} 27\item{coerce}{\code{signature(from = "ppp", to = "SpatialPoints")}} 28} 29} 30 31\usage{ 32as.ppp.SpatialPoints(X) 33as.ppp.SpatialPointsDataFrame(X) 34as.SpatialPoints.ppp(from) 35as.SpatialPointsDataFrame.ppp(from) 36as.SpatialGridDataFrame.ppp(from) 37} 38\arguments{ 39 \item{from, X}{object to coerce from} 40} 41\details{The main conversion is between \pkg{sp}s 42\code{SpatialPoints}/\code{SpatialPointsDataFrame} and \pkg{spatstat}s 43\code{ppp}. Conversion between \code{SpatialGridDataFrame} and \code{ppp} should 44rarely be used; using \code{\link{as.owin.SpatialGridDataFrame}} is more transparent.} 45 46\note{The \code{ppp} format requires an observation window which is the sampling 47region. The \pkg{sp} formats contain no such information and by default the 48bounding box of the points is simply used. This is almost never the correct 49thing to do! Rather, information about the sampling region should be converted 50into \pkg{spatstat}s \code{\link[spatstat.geom]{owin}} format and assigned as the 51observation window. Usually conversion from \code{ppp} to \pkg{sp} format simply 52discards the \code{owin}. However, \code{as.SpatialGridDataFrame.ppp} actually 53first discards the points(!), second checks that the corresponding \code{owin} 54is in a grid format (matrix of TRUE/FALSE for inside/outside sampling region), 55and finally converts the TRUE/FALSE grid to a \code{SpatialGridDataFrame}.} 56 57\section{Warning}{ 58 In \pkg{spatstat} all spatial objects are assumed to be planar. This means 59 that \pkg{spatstat} is not designed to work directly with geographic 60 (longitude and latitude) coordinates. If a \pkg{sp} object is declared to 61 have geographic (unprojected) coordinates \pkg{maptools} refuses to convert 62 directly to \pkg{spatstat} format. Rather, these should be projected first 63 using e.g. \code{\link[sp]{spTransform}}. If you know what you are doing, and 64 really want to force coercion, you can overwrite the \code{proj4string} of the 65 \pkg{sp} object with \code{NA}, \code{proj4string(x) <- CRS(NA)}, which 66 will fool the system to think that the data is in local planar coordinates. 67 This is probably not a good idea! 68} 69 70\author{Edzer Pebesma \email{edzer.pebesma@uni-muenster.de}, Roger Bivand} 71\examples{ 72run <- FALSE 73if (require("spatstat.geom", quietly=TRUE)) run <- TRUE 74if (run) { 75## Convert SpatialPointsDataFrame into a marked ppp 76data(meuse) 77coordinates(meuse) = ~x+y 78meuse_ppp <- as(meuse, "ppp") 79meuse_ppp # Window is the bounding rectangle 80} 81if (run) { 82plot(meuse_ppp, which.marks = "zinc") 83} 84if (run) { 85## Convert SpatialPoints into an unmarked ppp 86meuse2 <- as(meuse, "SpatialPoints") 87as(meuse2, "ppp") 88} 89if (run) { 90## Get sampling region in grid format and assign it as observation window 91data(meuse.grid) 92gridded(meuse.grid) <- ~x+y 93mg_owin <- as(meuse.grid, "owin") 94Window(meuse_ppp) <- mg_owin 95meuse_ppp # Window is now a binary image mask (TRUE/FALSE grid) 96} 97if (run) { 98plot(meuse_ppp, which.marks = "zinc") 99} 100if (run) { 101## Convert marked ppp back to SpatialPointsDataFrame 102rev_ppp_SPDF <- as.SpatialPointsDataFrame.ppp(meuse_ppp) 103summary(rev_ppp_SPDF) 104} 105if (run) { 106## Convert marked ppp back to SpatialPoints (discarding marks) 107rev_ppp_SP <- as.SpatialPoints.ppp(meuse_ppp) 108summary(rev_ppp_SP) 109} 110if (run) { 111## Convert marked ppp back to SpatialGridDataFrame (extracting the window grid) 112rev_ppp_SGDF <- as.SpatialGridDataFrame.ppp(meuse_ppp) 113summary(rev_ppp_SGDF) 114} 115} 116\keyword{spatial} 117