[R-sig-Geo] Subset polygon(s) from a country from wrld_simpl
Lars Dalby
lars.dalby at gmail.com
Fri Apr 6 11:25:11 CEST 2012
Dear list
I am trying to make a subset of wrld_simpl to use as mask for a raster
layer.
I have a list of countries and I can do the subset quite easily. However I
would like to have one (or several) of the polygons from Russia in the list
also - Kaliningrad on the coast of the Baltic. This is where I run into
trouble.
How do I cut these out and subsequently subset wrld_simpl accordingly?
require('sp');require('maptools')
cntr <- c('Denmark', 'Germany', 'Norway', 'Ireland', 'United Kingdom',
'France', 'Italy', 'Sweden', 'Finland', 'Spain', 'Portugal', 'Latvia',
'Estonia', 'Slovenia', 'Belgium', 'Netherlands', 'Austria', 'Poland',
'Switzerland', 'Morocco', 'Algeria', 'Tunisia', 'Slovakia', 'Lithuania',
'Croatia', 'Czech Republic')
data(wrld_simpl)
NW <- wrld_simpl[which(wrld_simpl at data$NAME %in% cntr),]
#I tried to use the overlay function, but that didn't quite do the trick:
rus <- wrld_simpl[which(wrld_simpl at data$NAME == 'Russia'),]
x <- 21.335449; y <- 54.788017
P <- data.frame('x' = x, 'y' = y)
coordinates(P) <- ~x+y
ov <- overlay(P, rus)
ov # 1
# Now if I move the point further east and repeat:
x <- 90.335449; y <- 54.788017
P <- data.frame('x' = x, 'y' = y)
coordinates(P) <- ~x+y
ov <- overlay(P, rus)
ov # still 1- so I guess this refers to the single object in the polygons
slot of rus, which is not quite what I'm after.
#sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dismo_0.7-17 maptools_0.8-14 lattice_0.20-6 foreign_0.8-49
[5] raster_1.9-70 sp_0.9-97 ncdf_1.6.6
loaded via a namespace (and not attached):
[1] grid_2.14.1 tools_2.14.1
Any suggestions on how to solve this would be highly appreciated.
Best regards and Happy Easter!
Lars
Lars Dalby
Msc. PhD student
Ecoinformatics & Biodiversity
Department of Bioscience, Aarhus University
Denmark
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Subset-polygon-s-from-a-country-from-wrld-simpl-tp7442490p7442490.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list