[spatstat] Convert shapefile to pixel image
Try fasterize, converting to spatstat from raster is straightforward, happy to help. https://github.com/ecohealthalliance/fasterize Cheers, Mije
On Fri, 2 Jun 2017, 07:45 Rolf Turner, <r.turner at auckland.ac.nz> wrote:
On 02/06/17 01:17, Lluis.Hurtado at uv.es wrote:
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.
As Don has said, this question would be better asked on R-sig-geo.
A *reproducible* example would be nice, e.g. perhaps you could tell us
how to get the shapefile in question.
In respect of your last point: Rather than "M <- union.owin(windows)"
you should use:
M <- do.call(union.owin, windows)
But that's rather off the track. What I think you should do (it's hard
to be certain without a reproducible example) is something like:
tw <- tess(tiles=windows)
iw <- as.im(funxy(as.function(tw,values=vvv),W=Window(tw)),dimyx=128)
where "vvv" is a vector of the values that you wish to associate with
the individual polygons. There may be less cryptic ways of
accomplishing the same thing, but this is the sexiest way, I think.
The value chosen for "dimyx", i.e. 128, is just by way of example. You
can use whatever value suits you. The larger it is, the smoother the
individual polygons will look, in pixellated form, but the longer things
will take. With 4307 polygons, that might be a rather long time!
HTH
cheers,
Rolf Turner
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dr. Michael Sumner Software and Database Engineer Australian Antarctic Division 203 Channel Highway Kingston Tasmania 7050 Australia [[alternative HTML version deleted]]