[R] [spatstat] Convert shapefile to pixel image

Lluis.Hurtado at uv.es Lluis.Hurtado at uv.es
Thu Jun 1 13:58:20 CEST 2017


Dear all,

I am currently working with the spatstat package, using windows and pixel images.

First:

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?

Second:

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.



More information about the R-help mailing list