[R-sig-Geo] sp package/ spatial points operation works in R <= 2.8, fails for R >= 2.9 - resend with file download link
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Fri May 21 08:29:18 CEST 2010
Hi Rick, thanks for the clean and reproducable example.
instead of
SampPointsOneDeg <- as(SampGridOneDeg, "SpatialPoints")
you now need to do a
SampPointsOneDeg <- as(as(SampGridOneDeg,
"SpatialPixels"),"SpatialPoints")
to make your example work.
As these coercion functions were written a long time ago I believe that
the reason it stopped working are changes in the methods package rather
than in sp. I'd agree that the first one should work as well, and I'll
have a look at modifying sp such that it will, again, in the future.
--
Edzer
On 05/21/2010 02:10 AM, rick reeves wrote:
> Hello List:
>
> The following script creates a point grid at user-supplied spatial
> resolution;
> this grid (SpatialPoints) is then used as an input to sp/overlay() to
> extract
> points at regular intervals from a SpatialPolygonsDataFrame object.
>
> Downloat the code and data for this example at:
> http://www.nceas.ucsb.edu/files/scicomp/Dloads/CreatePointGridSP.zip
>
> CreatePointGrid <- function()
> {
> library(rgdal) # loads sp package
>
> # Read ESRI Shape File into SpatialPolygonsDataFrame
>
> EcoRegion <-
> readOGR("BaileysEcoRegionUsaGP.shp","BaileysEcoRegionUsaGP")
> theProjection <- proj4string(EcoRegion)
>
> vals <- EcoRegion at bbox
> deltaLong <- as.integer((vals[1,2] - vals[1,1]) + 1.5)
> deltaLat <- as.integer((vals[2,2] - vals[2,1]) + 1.5)
>
> # grid resolution units: decimal degress.
>
> gridRes <- 1.0
> gridSizeX <- deltaLong / gridRes
> gridSizeY <- deltaLat / gridRes
>
> # GridTopology object is basis for sampling grid
>
> GridTopoOneDeg <- GridTopology(vals[,1],
> c(gridRes,gridRes),
> c(gridSizeX,gridSizeY))
>
> # Create the SpatialGrid object....
>
> SampGridOneDeg <- SpatialGrid(GridTopoOneDeg,proj4string =
> CRS(theProjection))
> SampPointsOneDeg <- as(SampGridOneDeg,"SpatialPoints")
> message("hit key to see first 20 points of One Degree Grid Points...")
> browser()
> print(SampPointsOneDeg[1:20,])
> message("done")
> }
> The issue: the line:
>
> SampPointsOneDeg <- as(SampGridOneDeg,"SpatialPoints")
>
> ...generates the required list of points when the script is run under
> R version 2.8 and earlier.
> However, if the script is run under R 2.9 or greater, the point list
> contains only 2 coordinate pairs.
>
> Question: What has changed in R 2.9 and later? Is this technique
> deprecated, and is there a better
> way of creating such a point list?
>
> Thanks,
> Rick R
>
> < Attached zip file contains the script and sample data set>
>
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics e.pebesma at wwu.de
More information about the R-sig-Geo
mailing list