| Title: | Extension to 'spatstat' for Converting Data Formats |
|---|---|
| Description: | Extension to the 'spatstat' package, enabling the user to convert spatial data between formats defined in `spatstat' and formats defined in 'sp', 'sf' and 'terra'. |
| Authors: | Adrian Baddeley [aut, cre], Ege Rubak [aut], Rolf Turner [aut], Edzer Pebesma [aut], Roger Bivand [aut], Michael Sumner [ctb], Matthew Lewis [ctb], Mathieu Rajerison [ctb], Mehdi Moradi [ctb] |
| Maintainer: | Adrian Baddeley <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.0-1 |
| Built: | 2026-10-18 11:08:37 UTC |
| Source: | https://github.com/cran/spatstat.convert |
Contains functions for converting spatial data between formats in the packages spatstat, sf, sp and terra.
This package provides functions for converting spatial data objects between different formats supported in the packages spatstat, sf, sp and terra.
An object X can be converted to a new class "foo" using the
S4 style syntax as(X, "foo"). If "foo" is a class in the
spatstat package, then the conversion can also be written in the
S3 style as.foo(X).
This package depends on the packages spatstat and sp, but only Suggests the packages sf and terra. In order to convert data to or from a format defined in sf, the user must load sf. To convert data to or from a format defined in terra, the user must load terra.
Functions for converting to spatstat data types are:
Functions for converting from spatstat data types are:
Adrian Baddeley [email protected], Ege Rubak [email protected], Rolf Turner [email protected], Edzer Pebesma [email protected], Roger Bivand, Michael Sumner, Matthew Lewis, Mathieu Rajerison and Mehdi Moradi.
## Create a SpatialPolygons representing a region with a hole holey <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0), c(0,0,1,1,0))), Polygon(cbind(c(0.6,0.4,0.4,0.6,0.6), c(0.2,0.2,0.4,0.4,0.2)), hole=TRUE)), ID="ho"))) plot(holey, col="red", pbg="pink") ## convert SpatialPolygons -> owin hoy <- as(holey, "owin") plot(hoy) #### Create a SpatialPolygons representing a region with a hole holey <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0), c(0,0,1,1,0))), Polygon(cbind(c(0.6,0.4,0.4,0.6,0.6), c(0.2,0.2,0.4,0.4,0.2)), hole=TRUE)), ID="ho"))) plot(holey, col="red", pbg="pink") ## convert SpatialPolygons -> owin hoy <- as(holey, "owin") plot(hoy) ##
Functions to convert between raster image formats in the spatstat and sp packages.
## S3 method for class 'SpatialGridDataFrame' as.im(X, ...) as.SpatialGridDataFrame.im(from)## S3 method for class 'SpatialGridDataFrame' as.im(X, ...) as.SpatialGridDataFrame.im(from)
X, from
|
object to coerce from |
... |
Ignored. |
A SpatialGridDataFrame object may contain several columns of
data such that several values are associated with each grid cell.
In contrast an
im object can only contain a single variable value for each cell. In
as.im.SpatialGridDataFrame() the first data column is used. To convert
another column to im format simply extract this column first
as shown in
the example below.
Either a pixel image
(object of class "im" as defined in the spatstat package)
or an object of class "SpatialGridDataFrame" as defined in the
package sp.
signature(from = "SpatialGridDataFrame", to = "im")
signature(from = "im", to = "SpatialGridDataFrame")
In spatstat all spatial objects are assumed to be planar. This means
that spatstat is not designed to work directly with geographic
(longitude and latitude) coordinates. If a sp object is declared to
have geographic (unprojected) coordinates maptools refuses to convert
directly to spatstat format. Rather, these should be projected first
using e.g. spTransform. If you know what you are doing, and
really want to force coercion, you can overwrite the proj4string of the
sp object with NA, proj4string(x) <- CRS(NA), which
will fool the system to think that the data is in local planar coordinates.
This is probably not a good idea!
Edzer Pebesma [email protected] and Roger Bivand. Adapted by Adrian Baddeley [email protected].
Other converters between sp and spatstat:
as.ppp.SpatialPoints,
as.psp.SpatialLines,
as.owin.SpatialPolygons,
as.SpatialPolygons.tess.
run <- TRUE qt <- TRUE plo <- interactive() if (run) { ## Extract an example SpatialGridDataFrame and plot it data(meuse.grid) # A data.frame gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame fullgrid(meuse.grid) <- TRUE # Finally a SpatialGridDataFrame mg_dist <- meuse.grid["dist"] # A SpatialGridDataFrame with a single column image(mg_dist, axes=TRUE) ## Convert to im format and plot it mg_im <- as(mg_dist, "im") if(plo) { plot(mg_im) } ## Convert back to SpatialGridDataFrame and plot it again mg2 <- as.SpatialGridDataFrame.im(mg_im) if(plo) { image(mg2, axes=TRUE) } } run <- run && require(terra, quietly=qt) if (run) { ## Convert SpatialGridDataFrame -> SpatRaster -> im and plot it r <- as(mg2, "SpatRaster") rim <- as.im(r) if(plo) plot(rim) ## image with categorical values rr <- rast(nrows=2, ncols=3) values(rr) <- factor(letters[11:16]) ii <- as.im(rr) if(plo) plot(ii) }run <- TRUE qt <- TRUE plo <- interactive() if (run) { ## Extract an example SpatialGridDataFrame and plot it data(meuse.grid) # A data.frame gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame fullgrid(meuse.grid) <- TRUE # Finally a SpatialGridDataFrame mg_dist <- meuse.grid["dist"] # A SpatialGridDataFrame with a single column image(mg_dist, axes=TRUE) ## Convert to im format and plot it mg_im <- as(mg_dist, "im") if(plo) { plot(mg_im) } ## Convert back to SpatialGridDataFrame and plot it again mg2 <- as.SpatialGridDataFrame.im(mg_im) if(plo) { image(mg2, axes=TRUE) } } run <- run && require(terra, quietly=qt) if (run) { ## Convert SpatialGridDataFrame -> SpatRaster -> im and plot it r <- as(mg2, "SpatRaster") rim <- as.im(r) if(plo) plot(rim) ## image with categorical values rr <- rast(nrows=2, ncols=3) values(rr) <- factor(letters[11:16]) ii <- as.im(rr) if(plo) plot(ii) }
Given a raster image in the SpatRaster format of the terra
package, convert it to a pixel image of class im in the
spatstat package.
## S3 method for class 'SpatRaster' as.im(X, ...)## S3 method for class 'SpatRaster' as.im(X, ...)
X |
A raster image object of class |
... |
Ignored. |
The function as.im is generic.
This file describes the method for as.im
for objects of class "SpatRaster" in the terra package.
The object X is converted to a pixel image of class "im"
recognised by the spatstat family of packages.
A pixel image object of class "im" recognised by the
spatstat family of packages.
Robert Hijmans, Ege Rubak [email protected] and Adrian Baddeley [email protected].
The reverse operation is rast.
qt <- TRUE if(require(terra, quietly=qt)) { f <- system.file("ex/elev.tif", package="terra") x <- rast(f) plot(x) z <- as.im(x) plot(z) }qt <- TRUE if(require(terra, quietly=qt)) { f <- system.file("ex/elev.tif", package="terra") x <- rast(f) plot(x) z <- as.im(x) plot(z) }
Convert an object of class sf or
sfc (from package sf),
representing a collection of polygonal lines,
into an object of class linnet (from package spatstat),
representing a network.
## S3 method for class 'sf' as.linnet(X, ..., fuse=TRUE) ## S3 method for class 'sfc' as.linnet(X, ..., fuse=TRUE)## S3 method for class 'sf' as.linnet(X, ..., fuse=TRUE) ## S3 method for class 'sfc' as.linnet(X, ..., fuse=TRUE)
X |
Object of class |
... |
Ignored. |
fuse |
Logical value indicating whether to join different curves which have a common vertex. |
These functions convert an object of class sf
or sfc into an object of class linnet.
They can be used both as S3 methods for the spatstat generic function
as.linnet which can be called in the
syntax as.linnet(X), and as S4 coercion methods which can be
called in the syntax as(X, "linnet").
An object of class sf
or sfc (from package sp) represents
a list of lists of the coordinates of lines, such as a list of all roads in
a city. An object of class linnet in the spatstat package
represents a linear network, such as a road network.
If fuse=FALSE, each line in X
will be treated as if it were disconnected from the others.
The result is a network that consists of many disconnected sub-networks,
equivalent to the list of lines.
If fuse=TRUE (the default), the code will
search for identical pairs of vertices occurring in different
lines, and will treat them as identical vertices,
effectively joining the two lines objects at this common
vertex.
A linear network (object of class linnet
as defined in the spatstat package).
signature(from = "sf", to = "linnet")
signature(from = "sfc", to = "linnet")
Adrian Baddeley [email protected].
qt <- TRUE if(require(sf, quietly=qt)) { nc <- st_read(system.file("shape/nc.shp", package="sf")) b <- st_sfc(st_cast(st_geometry(nc)[[4]], "MULTILINESTRING")) ## convert to 'spatstat' network format z <- as.linnet(b) }qt <- TRUE if(require(sf, quietly=qt)) { nc <- st_read(system.file("shape/nc.shp", package="sf")) b <- st_sfc(st_cast(st_geometry(nc)[[4]], "MULTILINESTRING")) ## convert to 'spatstat' network format z <- as.linnet(b) }
Convert an object of class SpatialLines or
SpatialLinesDataFrame (from package sp),
representing a collection of polygonal lines,
into an object of class linnet (from package spatstat),
representing a network.
## S3 method for class 'SpatialLines' as.linnet(X, ..., fuse = TRUE) ## S4 method for signature 'SpatialLines,linnet' coerce(from, to = "linnet", strict = TRUE) ## S4 method for signature 'SpatialLinesDataFrame,linnet' coerce(from, to = "linnet", strict = TRUE)## S3 method for class 'SpatialLines' as.linnet(X, ..., fuse = TRUE) ## S4 method for signature 'SpatialLines,linnet' coerce(from, to = "linnet", strict = TRUE) ## S4 method for signature 'SpatialLinesDataFrame,linnet' coerce(from, to = "linnet", strict = TRUE)
X, from
|
Object of class |
to |
output object of class |
strict |
logical flag. If TRUE, the returned object must be strictly from the target class. |
... |
Ignored. |
fuse |
Logical value indicating whether to join different curves which have a common vertex. |
This function converts an object of class SpatialLines
or SpatialLinesDataFrame into an object of class linnet.
It is not a method for the spatstat generic function
as.linnet, but like other S4 coercion functions for
sp classes to spatstat classes, it may be called directly as a function.
An object of class SpatialLines
or SpatialLinesDataFrame (from package sp) represents
a list of lists of the coordinates of lines, such as a list of all roads in
a city. An object of class linnet in the spatstat package
represents a linear network, such as a road network.
If fuse=FALSE, each “Line” object in X
will be treated as if it were disconnected from the others.
The result is a network that consists of many disconnected sub-networks,
equivalent to the list of “Line” objects.
If fuse=TRUE (the default), the code will
search for identical pairs of vertices occurring in different
“Line” objects, and will treat them as identical vertices,
effectively joining the two “Line” objects at this common
vertex.
If X belongs to class SpatialLinesDataFrame,
the associated columns of data in the auxiliary data frame
slot(X, "data") will be copied to the output as the marks
attached to the line segments of the network. See the Examples.
A linear network (object of class linnet
as defined in the spatstat package).
Adrian Baddeley [email protected].
s1 <- cbind(c(1,2,3,4),c(3,2,2,1)) s2 <- s1 + 0.05 S <- SpatialLines(list(Lines(list(Line(s1), Line(s2)), ID="a"))) L <- as.linnet(S, fuse=FALSE) print(max(vertexdegree(L))) ## extract data associated with each network segment head(marks(as.psp(L)))s1 <- cbind(c(1,2,3,4),c(3,2,2,1)) s2 <- s1 + 0.05 S <- SpatialLines(list(Lines(list(Line(s1), Line(s2)), ID="a"))) L <- as.linnet(S, fuse=FALSE) print(max(vertexdegree(L))) ## extract data associated with each network segment head(marks(as.psp(L)))
Functions to convert between observation window (owin) objects
in the spatstat package and various formats in the sp package.
Coercion can use the S3 or S4 syntax.
## S3 method for class 'SpatialPolygons' as.owin(W, ..., fatal=TRUE) ## S3 method for class 'SpatialGridDataFrame' as.owin(W, ..., fatal=TRUE) ## S3 method for class 'SpatialPixelsDataFrame' as.owin(W, ..., fatal=TRUE) as.SpatialPolygons.owin(from) as.SpatialGridDataFrame.owin(from)## S3 method for class 'SpatialPolygons' as.owin(W, ..., fatal=TRUE) ## S3 method for class 'SpatialGridDataFrame' as.owin(W, ..., fatal=TRUE) ## S3 method for class 'SpatialPixelsDataFrame' as.owin(W, ..., fatal=TRUE) as.SpatialPolygons.owin(from) as.SpatialGridDataFrame.owin(from)
W |
Object of class |
from |
Object of class |
... |
ignored |
fatal |
Logical value specifying whether conversion errors should cause a fatal error. |
These functions convert an object of class owin in the
spatstat package into an object of class SpatialPolygons,
SpatialGridDataFrame or SpatialPixelsDataFrame
in the sp package, and vice versa.
An observation window object of class owin in the
spatstat packages defines a planar region. It is
typically used to represent a sampling region where data were
observed. It comes in three different
formats: a simple rectangle, a polygon (vector format) or a binary
mask (TRUE/FALSE grid; raster format).
These can all be coerced to polygonal form internally in
spatstat and then converted to SpatialPolygons, which is what
as.SpatialPolygons.owin() does. For objects of class
SpatialPolygons (and SpatialPolygonsDataFrame) the sp
polygons are simply extracted and cast into spatstat's polygon format
inside the owin object. For SpatialPixelsDataFrame and
SpatialGridDataFrame the grid is extracted and cast into
spatstat's mask format inside the owin object.
In all cases, any data apart from the
spatial region itself are discarded.
A spatial window
(object of class "owin" as defined in the spatstat
package)
or a collection of polygons
(object of class "SpatialPolygons"
as defined in the package sp)
or a spatial grid (object of class "SpatialGridDataFrame"
as defined in the package sp).
signature(from = "SpatialPolygons", to = "owin")
signature(from = "SpatialPixelsDataFrame", to = "owin")
signature(from = "SpatialGridDataFrame", to = "owin")
signature(from = "owin", to = "SpatialPolygons")
In spatstat all spatial objects are assumed to be planar. This means
that spatstat is not designed to work directly with geographic
(longitude and latitude) coordinates. If a sp object is declared to
have geographic (unprojected) coordinates maptools refuses to convert
directly to spatstat format. Rather, these should be projected first
using e.g. spTransform. If you know what you are doing, and
really want to force coercion, you can overwrite the proj4string of the
sp object with an empty string, proj4string(x) <- "", which
will fool the system to think that the data is in local planar coordinates.
This is probably not a good idea!
When coercing a SpatialPolygons object to an owin object, full
topology checking is enabled by default. To avoid checking, set
spatstat.options(checkpolygons=FALSE).
To perform the checking later, owinpolycheck(W, verbose=TRUE).
Edzer Pebesma [email protected] and Roger Bivand. Adapted by Adrian Baddeley [email protected].
## SpatialPixelsDataFrame -> owin data(meuse.grid) # A data.frame gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame mg_owin <- as(meuse.grid, "owin") mg_owin ## SpatialGridDataFrame -> owin fullgrid(meuse.grid) <- TRUE # Now a SpatialGridDataFrame mg_owin2 <- as(meuse.grid, "owin") ## SpatialPolygons region with a hole ho_sp <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0), c(0,0,1,1,0))), Polygon(cbind(c(0.6,0.4,0.4,0.6,0.6), c(0.2,0.2,0.4,0.4,0.2)), hole=TRUE)), ID="ho"))) plot(ho_sp, col="red", pbg="pink") ## SpatialPolygons -> owin ho <- as(ho_sp, "owin") plot(ho) ## Define owin directly and check they are identical ho_orig <- owin(poly=list(list(x=c(0,1,1,0), y=c(0,0,1,1)), list(x=c(0.6,0.4,0.4,0.6), y=c(0.2,0.2,0.4,0.4)))) identical(ho, ho_orig) ## owin -> SpatialPolygons ho_sp1 <- as(ho, "SpatialPolygons") all.equal(ho_sp, ho_sp1, check.attributes=FALSE)## SpatialPixelsDataFrame -> owin data(meuse.grid) # A data.frame gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame mg_owin <- as(meuse.grid, "owin") mg_owin ## SpatialGridDataFrame -> owin fullgrid(meuse.grid) <- TRUE # Now a SpatialGridDataFrame mg_owin2 <- as(meuse.grid, "owin") ## SpatialPolygons region with a hole ho_sp <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0), c(0,0,1,1,0))), Polygon(cbind(c(0.6,0.4,0.4,0.6,0.6), c(0.2,0.2,0.4,0.4,0.2)), hole=TRUE)), ID="ho"))) plot(ho_sp, col="red", pbg="pink") ## SpatialPolygons -> owin ho <- as(ho_sp, "owin") plot(ho) ## Define owin directly and check they are identical ho_orig <- owin(poly=list(list(x=c(0,1,1,0), y=c(0,0,1,1)), list(x=c(0.6,0.4,0.4,0.6), y=c(0.2,0.2,0.4,0.4)))) identical(ho, ho_orig) ## owin -> SpatialPolygons ho_sp1 <- as(ho, "SpatialPolygons") all.equal(ho_sp, ho_sp1, check.attributes=FALSE)
Given a dataset of spatial points in the sf package, convert it to a spatial point pattern object in the spatstat package.
## S3 method for class 'sf' as.ppp(X, ..., fatal=TRUE, warn=TRUE)## S3 method for class 'sf' as.ppp(X, ..., fatal=TRUE, warn=TRUE)
X |
An object of class |
... |
Ignored. |
fatal |
Logical value specifying whether an error should be caused if the conversion cannot be performed. |
warn |
Logical value specifying whether a warning should be issued if the conversion cannot be performed. |
The function as.ppp is generic.
This file describes the method for as.ppp
for objects of class sf in the sf package.
The object X is converted to a spatial point pattern
of class "ppp"
recognised by the spatstat family of packages.
A spatial point pattern (object of class "ppp")
recognised by the spatstat family of packages.
Ege Rubak [email protected], Adrian Baddeley [email protected] and Edzer Pebesma [email protected].
qt <- TRUE if(require(sf, quietly=qt)) { ## point pattern dataset in 'sp' format data(meuse) coordinates(meuse) = ~x+y ## convert to 'sf' format maas <- st_as_sf(meuse) ## convert to 'spatstat' format z <- as.ppp(maas) }qt <- TRUE if(require(sf, quietly=qt)) { ## point pattern dataset in 'sp' format data(meuse) coordinates(meuse) = ~x+y ## convert to 'sf' format maas <- st_as_sf(meuse) ## convert to 'spatstat' format z <- as.ppp(maas) }
Functions to convert between spatstats planar point pattern (ppp)
format and sps SpatialPoints and SpatialPointsDataFrame as
well as one-way conversion from SpatialGridDataFrame to ppp.
S4-style as() coercion can be used as well.
## S3 method for class 'SpatialPoints' as.ppp(X, W=NULL, ..., fatal=TRUE) ## S3 method for class 'SpatialPointsDataFrame' as.ppp(X, W=NULL, ..., fatal=TRUE) as.SpatialPoints.ppp(from) as.SpatialPointsDataFrame.ppp(from) as.SpatialGridDataFrame.ppp(from)## S3 method for class 'SpatialPoints' as.ppp(X, W=NULL, ..., fatal=TRUE) ## S3 method for class 'SpatialPointsDataFrame' as.ppp(X, W=NULL, ..., fatal=TRUE) as.SpatialPoints.ppp(from) as.SpatialPointsDataFrame.ppp(from) as.SpatialGridDataFrame.ppp(from)
from, X
|
object to coerce from |
W |
Window (object of class |
... |
Ignored. |
fatal |
Logical value specifying whether a conversion error should result in a fatal error. |
The main conversion is between sps
SpatialPoints/SpatialPointsDataFrame and spatstats
ppp.
Conversion between SpatialGridDataFrame and ppp should
rarely be used; using as.owin.SpatialGridDataFrame
is more transparent.
A spatial point pattern
(object of class "ppp" as defined in the spatstat
package)
or an object of class "SpatialPoints",
"SpatialPointsDataFrame" or "SpatialGridDataFrame"
as defined in the package sp.
signature(from = "SpatialPoints", to = "ppp")
signature(from = "SpatialPointsDataFrame", to = "ppp")
signature(from = "ppp", to = "SpatialGridDataFrame")
signature(from = "ppp", to = "SpatialPointsDataFrame")
signature(from = "ppp", to = "SpatialPoints")
In spatstat all spatial objects are assumed to be planar. This means
that spatstat is not designed to work directly with geographic
(longitude and latitude) coordinates. If a sp object is declared to
have geographic (unprojected) coordinates maptools refuses to convert
directly to spatstat format. Rather, these should be projected first
using e.g. spTransform. If you know what you are doing, and
really want to force coercion, you can overwrite the proj4string of the
sp object with NA, proj4string(x) <- CRS(NA), which
will fool the system to think that the data is in local planar coordinates.
This is probably not a good idea!
The ppp format requires an observation window which is the sampling
region. The sp formats contain no such information and by default the
bounding box of the points is simply used. This is almost never the correct
thing to do! Rather, information about the sampling region should be converted
into spatstats owin format and assigned as the
observation window. Usually conversion from ppp to sp format simply
discards the owin. However, as.SpatialGridDataFrame.ppp actually
first discards the points(!), second checks that the corresponding owin
is in a grid format (matrix of TRUE/FALSE for inside/outside sampling region),
and finally converts the TRUE/FALSE grid to a SpatialGridDataFrame.
Edzer Pebesma [email protected] and Roger Bivand
## Convert SpatialPointsDataFrame into a marked ppp data(meuse) coordinates(meuse) = ~x+y meuse_ppp <- as(meuse, "ppp") meuse_ppp # Window is the bounding rectangle plot(meuse_ppp, which.marks = "zinc") ## Convert SpatialPoints into an unmarked ppp meuse2 <- as(meuse, "SpatialPoints") as(meuse2, "ppp") ## Get sampling region in grid format and assign it as observation window data(meuse.grid) gridded(meuse.grid) <- ~x+y mg_owin <- as(meuse.grid, "owin") Window(meuse_ppp) <- mg_owin meuse_ppp # Window is now a binary image mask (TRUE/FALSE grid) plot(meuse_ppp, which.marks = "zinc") ## Convert marked ppp back to SpatialPointsDataFrame rev_ppp_SPDF <- as.SpatialPointsDataFrame.ppp(meuse_ppp) summary(rev_ppp_SPDF) ## Convert marked ppp back to SpatialPoints (discarding marks) rev_ppp_SP <- as.SpatialPoints.ppp(meuse_ppp) summary(rev_ppp_SP) ## Convert marked ppp back to SpatialGridDataFrame (extracting window grid) rev_ppp_SGDF <- as.SpatialGridDataFrame.ppp(meuse_ppp) summary(rev_ppp_SGDF)## Convert SpatialPointsDataFrame into a marked ppp data(meuse) coordinates(meuse) = ~x+y meuse_ppp <- as(meuse, "ppp") meuse_ppp # Window is the bounding rectangle plot(meuse_ppp, which.marks = "zinc") ## Convert SpatialPoints into an unmarked ppp meuse2 <- as(meuse, "SpatialPoints") as(meuse2, "ppp") ## Get sampling region in grid format and assign it as observation window data(meuse.grid) gridded(meuse.grid) <- ~x+y mg_owin <- as(meuse.grid, "owin") Window(meuse_ppp) <- mg_owin meuse_ppp # Window is now a binary image mask (TRUE/FALSE grid) plot(meuse_ppp, which.marks = "zinc") ## Convert marked ppp back to SpatialPointsDataFrame rev_ppp_SPDF <- as.SpatialPointsDataFrame.ppp(meuse_ppp) summary(rev_ppp_SPDF) ## Convert marked ppp back to SpatialPoints (discarding marks) rev_ppp_SP <- as.SpatialPoints.ppp(meuse_ppp) summary(rev_ppp_SP) ## Convert marked ppp back to SpatialGridDataFrame (extracting window grid) rev_ppp_SGDF <- as.SpatialGridDataFrame.ppp(meuse_ppp) summary(rev_ppp_SGDF)
Functions to convert between spatstats planar segment pattern (psp)
format and various sp line formats. S4-style as() coercion can be
used as well.
## S3 method for class 'Line' as.psp(x, ..., window=NULL, marks=NULL, fatal) ## S3 method for class 'Lines' as.psp(x, ..., window=NULL, marks=NULL, fatal) ## S3 method for class 'SpatialLines' as.psp(x, ..., window=NULL, marks=NULL, characterMarks=FALSE, fatal) ## S3 method for class 'SpatialLinesDataFrame' as.psp(x, ..., window=NULL, marks=NULL, fatal) as.SpatialLines.psp(from)## S3 method for class 'Line' as.psp(x, ..., window=NULL, marks=NULL, fatal) ## S3 method for class 'Lines' as.psp(x, ..., window=NULL, marks=NULL, fatal) ## S3 method for class 'SpatialLines' as.psp(x, ..., window=NULL, marks=NULL, characterMarks=FALSE, fatal) ## S3 method for class 'SpatialLinesDataFrame' as.psp(x, ..., window=NULL, marks=NULL, fatal) as.SpatialLines.psp(from)
x, from
|
object to coerce from |
... |
ignored |
window |
window of class |
marks |
marks as defined in the spatstat package |
characterMarks |
default FALSE, if TRUE, do not convert NULL marks to factor from character |
fatal |
formal coercion argument; ignored |
A spatial pattern of line segments
(object of class "psp" as defined in the spatstat
package)
or a collection of lines (object of class "SpatialLines"
or "SpatialLinesDataFrame" in the package sp).
signature(from = "Line", to = "psp")
signature(from = "Lines", to = "psp")
signature(from = "SpatialLines", to = "psp")
signature(from = "SpatialLinesDataFrame", to = "psp")
signature(from = "psp", to = "SpatialLines")
In spatstat all spatial objects are assumed to be planar. This means
that spatstat is not designed to work directly with geographic
(longitude and latitude) coordinates. If a sp object is declared to
have geographic (unprojected) coordinates maptools refuses to convert
directly to spatstat format. Rather, these should be projected first
using e.g. spTransform. If you know what you are doing, and
really want to force coercion, you can overwrite the proj4string of the
sp object with NA, proj4string(x) <- CRS(NA), which
will fool the system to think that the data is in local planar coordinates.
This is probably not a good idea!
Edzer Pebesma [email protected] and Roger Bivand
data(meuse.riv) mr <- Line(meuse.riv) mr_psp <- as(mr, "psp") mr_psp plot(mr_psp) xx_back <- as(mr_psp, "SpatialLines") plot(xx_back)data(meuse.riv) mr <- Line(meuse.riv) mr_psp <- as(mr, "psp") mr_psp plot(mr_psp) xx_back <- as(mr_psp, "SpatialLines") plot(xx_back)
This function coerces spatstats tessellation objects of class tess
to sps SpatialPolygons class. S4-style as() coercion works as well.
as.SpatialPolygons.tess(from)as.SpatialPolygons.tess(from)
from |
spatstat object of class |
An object of class "SpatialPolygons"
as defined in the package sp.
signature(from = "tess", to = "SpatialPolygons")
Edzer Pebesma [email protected] and Roger Bivand
A <- dirichlet(runifpoint(6)) A_sp <- as(A, "SpatialPolygons") plot(A_sp) text(coordinates(A_sp), labels=row.names(A_sp), cex=0.6)A <- dirichlet(runifpoint(6)) A_sp <- as(A, "SpatialPolygons") plot(A_sp) text(coordinates(A_sp), labels=row.names(A_sp), cex=0.6)
Virtual S4 class definition for S3 classes in the spatstat package to allow S4-style coercion to these classes
A virtual Class: No objects may be created from it.
Edzer Pebesma [email protected]