[R] [spatstat] Convert shapefile to pixel image

MacQueen, Don macqueen1 at llnl.gov
Thu Jun 1 21:10:23 CEST 2017

This looks like to would be better to ask on R-sig-geo, instead of R-help.


Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 6/1/17, 6:17 AM, "R-help on behalf of Lluis.Hurtado at uv.es" <r-help-bounces at r-project.org on behalf of Lluis.Hurtado at uv.es> wrote:

    Dear all,
    I am currently working with the spatstat package, using windows and pixel images.
    My aim is to transform a shapefile (see attached) into a pixel image. 
    My idea is to start transforming the shapefile into a Spatial Polygon file:
    x <- readShapeSpatial("200001441.shp")
    y <- as(x, "SpatialPolygons")
    z <- as.owin(y)
    Given z, I want to identify each polygon with a single constant value. This is like adding marks to the SpatialPolygons file. Then I want to convert these polygons into an image, such that the value of each pixel corresponds to the value associated to the polygon where the pixel lies. 
    I have been able to do this individually, polygon by polygon, but then I cannot merge the resulting images into a single one. Any idea?
    I would also need a single window containing all the smallest polygons (the boundary). I have tried:
    w <- union.owin(z)
    But the resulting window w still shows internal polygons. As read in spatstas FAQ page:
    "First, convert each of the regions into a separate owin object. Then apply union.owin to combine them."
    So I try,
    regions <- slot(y, "polygons")
    regions <- lapply(regions, function(x) { SpatialPolygons(list(x)) })
    windows <- lapply(regions, as.owin) 
    But windows is a list of 4307 polygons. How can introduce all of them as a single argument?
    > M <- union.owin(windows)
    Warning messages:
    1: In union.owin(windows) : Some arguments were not windows
    2: In union.owin(windows) : No windows were given
    Thank you very much for you help.
    Lluís Hurtado.
    R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list