Skip to content

Problem with extract {raster} function

1 message · Kalomenopoulos, Manos

#
Thanks a lot, Robert!
Could you please tell me how you cleaned the shapefile. I will need to clean my whole dataset, as this was just a small extract of it.
Thanks in advance,
Manos

-------------------------------------------------------
Manos Kalomenopoulos
Lab. of Geo-Information Science and Remote Sensing, Center for Geo-Information - Wageningen University
Department for Biogeochemical Integration, Max-Planck-Institute for Biogeochemistry, Jena
-------------------------------------------------------


-----Original Message-----
From: Robert J. Hijmans [mailto:r.hijmans at gmail.com] 
Sent: dinsdag 28 mei 2013 3:15
To: Kalomenopoulos, Manos
Cc: r-sig-geo at r-project.org
Subject: Re: [R-sig-Geo] Problem with extract {raster} function

Manos,

I think this happens because s1pol is invalid (self-intersecting).
s2pol also has that problem but it works anyway. I have cleaned your shapefile and will send it to you via a separate email. With the cleaned shp file the problem does not occur. The script below shows the test for validity and the difference between the two data sources.

Robert

library(raster)
s1pol <- shapefile('1site.shp')
s2 <- shapefile('cln.shp')
library(rgeos)
gIsValid(s1pol)
# [1] FALSE
# Warning message:
# In RGEOSUnaryPredFunc(spgeom, byid, "rgeos_isvalid") :
#   Ring Self-intersection at or near point -4997474.25179449 -227339.61631866
gIsValid(s2)
#  [1] TRUE

# see the difference

r <- raster('1site.tif')
df1 <- extract(r,s1pol, df=TRUE, weights=TRUE, cellnumbers=TRUE)
names(df1) <- c('ID','cell', 'r', 'weight') # Extraction for test area a <- tapply(df1$weight, df1$cell, sum)
r1 <- raster(r)
r1[as.integer(names(a))] = a

df2 <- extract(r, s2, df=TRUE, weights=TRUE, cellnumbers=TRUE)
names(df2) <- c('ID','cell', 'r', 'weight') # Extraction for test area b <- tapply(df2$weight, df2$cell, sum)
r2 <- raster(r)
r2[as.integer(names(b))] <- b

s <- stack(r1, r2)
names(s) <- c('original', 'cleaned')
plot(s)
On Mon, May 20, 2013 at 11:25 AM, Kalomenopoulos, Manos <manos.kalomenopoulos at wur.nl> wrote: