[R-sig-Geo] Changing coords in SpatialPolgyon

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu Sep 13 13:25:39 CEST 2012


Thanks, Nick.

what works, and probably does what you want, is this:

slot(all_emerged_islands_at_this_height_break at polygons[[1]]@Polygons[[1]],
 "coords", check=FALSE) = slot(tmppoly_shrunken, "coords")

I replaced here

all_emerged_islands_at_this_height_break[j]

with

all_emerged_islands_at_this_height_break

selection-assignment replaces whole objects, not just its first element.
As you address @polygons[[1]] you do make sure the replacement concerns
the first element only.

Your (somewhat understandable) assumption was that SpatialPolygons
objects have a [<- method, but they don't (none of the Spatial* objects do).

The error message "object of type 'S4' is not subsettable" is not very
enlightening. I'll think of adding a [<- method that only gives an error
message that is more helpful.

Best regards,
--
Edzer

On 09/13/2012 02:13 AM, Nick Matzke wrote:
> Hi all,
> 
> Thanks very much for the comments.  Here is a "reproduce the problem
> from scratch" which will hopefully be helpful.
> 
> (Sorry this took a bit, I had to figure out some dput hacks to even get
> the S4 objects to store/retrieve with dput/dget.)
> 
> Any help VERY welcome...
> 
> 
> 
> 
> #######################################################
> # Re-creation of problem:
> # start by retrieving the polygon objects I was using
> #######################################################
> library(sp)
> library(maptools)    # for e.g. elide
> sessionInfo()
> # R version 2.14.1 (2011-12-22)
> # Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
> #
> # locale:
> # [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> #
> # attached base packages:
> # [1] stats     grDevices utils     datasets  graphics methods   base
> #
> # other attached packages:
> # [1] maptools_0.8-12 lattice_0.20-0  foreign_0.8-48 sp_0.9-93
> #
> # loaded via a namespace (and not attached):
> # [1] grid_2.14.1
> 
> 
> all_emerged_islands_at_this_height_break = new("SpatialPolygons"
>     , polygons = list(
> new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(89.7176352165596, 25.2725528991387)
>     , area = 0.102289000000002
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(89.595, 89.655, 89.699, 89.759, 89.819,
> 89.864, 89.864,
> 89.894, 89.924, 89.909, 89.894, 89.879, 89.864, 89.774, 89.684,
> 89.655, 89.565, 89.49, 89.505, 89.535, 89.595, 25.38, 25.407,
> 25.435, 25.448, 25.421, 25.366, 25.325, 25.366, 25.366, 25.298,
> 25.257, 25.161, 25.092, 25.106, 25.12, 25.175, 25.188, 25.202,
> 25.312, 25.38, 25.38), .Dim = c(21L, 2L), .Dimnames = list(NULL,
>     c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(89.7176352165596, 25.2725528991387)
>     , ID = "86"
>     , area = 0.102289000000002
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(91.4445748369887, 25.5542392708951)
>     , area = 0.253050000000001
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(91.273, 91.348, 91.408, 91.498, 91.528,
> 91.573, 91.633,
> 91.723, 91.768, 91.768, 91.768, 91.708, 91.648, 91.558, 91.468,
> 91.378, 91.318, 91.228, 91.123, 91.108, 91.123, 91.198, 91.258,
> 91.273, 25.804, 25.817, 25.844, 25.831, 25.763, 25.694, 25.681,
> 25.681, 25.626, 25.544, 25.448, 25.38, 25.312, 25.298, 25.298,
> 25.325, 25.38, 25.421, 25.435, 25.558, 25.64, 25.708, 25.776,
> 25.804), .Dim = c(24L, 2L), .Dimnames = list(NULL, c("x", "y"
> )))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(91.4445748369887, 25.5542392708951)
>     , ID = "89"
>     , area = 0.253050000000001
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(92.0053498822521, 25.1236440055376)
>     , area = 1.07977850000001
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(91.618, 91.633, 91.723, 91.813, 91.918,
> 92.008, 92.112,
> 92.202, 92.262, 92.322, 92.382, 92.367, 92.352, 92.367, 92.382,
> 92.397, 92.397, 92.337, 92.322, 92.337, 92.367, 92.367, 92.337,
> 92.337, 92.277, 92.232, 92.142, 92.038, 91.918, 91.813, 91.738,
> 91.648, 91.588, 91.528, 91.483, 91.483, 91.513, 91.543, 91.618,
> 25.503, 25.599, 25.667, 25.681, 25.708, 25.749, 25.831, 25.94,
> 26.049, 26.062, 26.008, 25.94, 25.858, 25.776, 25.667, 25.476,
> 25.257, 25.037, 24.818, 24.694, 24.584, 24.515, 24.487, 24.418,
> 24.349, 24.335, 24.28, 24.322, 24.377, 24.446, 24.57, 24.735,
> 24.9, 25.079, 25.202, 25.284, 25.353, 25.448, 25.503), .Dim = c(39L,
> 2L), .Dimnames = list(NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(92.0053498822521, 25.1236440055376)
>     , ID = "90"
>     , area = 1.07977850000001
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(92.7514440592915, 24.8855703894446)
>     , area = 0.227182499999996
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(92.922, 92.937, 92.907, 92.802, 92.757,
> 92.667, 92.592,
> 92.592, 92.577, 92.577, 92.607, 92.652, 92.712, 92.712, 92.712,
> 92.787, 92.817, 92.877, 92.892, 92.892, 92.877, 92.877, 92.862,
> 92.817, 92.847, 92.877, 92.922, 24.694, 24.652, 24.584, 24.515,
> 24.501, 24.418, 24.404, 24.542, 24.611, 24.735, 24.873, 25.024,
> 25.188, 25.312, 25.394, 25.407, 25.435, 25.435, 25.325, 25.229,
> 25.065, 24.941, 24.914, 24.845, 24.79, 24.749, 24.694), .Dim = c(27L,
> 2L), .Dimnames = list(NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(92.7514440592915, 24.8855703894446)
>     , ID = "91"
>     , area = 0.227182499999996
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(92.9357709620477, 24.4263626802001)
>     , area = 0.203939999999999
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(92.772, 92.802, 92.892, 92.967, 92.997,
> 93.102, 93.162,
> 93.237, 93.267, 93.222, 93.177, 93.102, 93.027, 92.952, 92.877,
> 92.772, 92.682, 92.622, 92.607, 92.652, 92.697, 92.772, 24.584,
> 24.625, 24.639, 24.68, 24.625, 24.597, 24.529, 24.487, 24.432,
> 24.363, 24.294, 24.266, 24.17, 24.17, 24.225, 24.28, 24.363,
> 24.391, 24.46, 24.57, 24.57, 24.584), .Dim = c(22L, 2L), .Dimnames = list(
>     NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(92.9357709620477, 24.4263626802001)
>     , ID = "92"
>     , area = 0.203939999999999
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(93.3346580365155, 24.13562118743)
>     , area = 0.209865
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(93.072, 93.117, 93.162, 93.267, 93.311,
> 93.356, 93.386,
> 93.446, 93.506, 93.566, 93.626, 93.641, 93.641, 93.641, 93.641,
> 93.611, 93.581, 93.491, 93.431, 93.341, 93.296, 93.222, 93.177,
> 93.147, 93.087, 93.042, 93.027, 93.072, 24.322, 24.377, 24.404,
> 24.404, 24.391, 24.335, 24.28, 24.253, 24.239, 24.184, 24.17,
> 24.142, 24.114, 24.073, 24.018, 23.962, 23.893, 23.865, 23.907,
> 23.921, 23.948, 23.962, 24.004, 24.059, 24.087, 24.128, 24.239,
> 24.322), .Dim = c(28L, 2L), .Dimnames = list(NULL, c("x", "y"
> )))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(93.3346580365155, 24.13562118743)
>     , ID = "93"
>     , area = 0.209865
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(93.720755372218, 23.8856480770871)
>     , area = 0.11727
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(93.551, 93.581, 93.671, 93.776, 93.866,
> 93.941, 93.971,
> 93.941, 93.896, 93.866, 93.836, 93.821, 93.806, 93.791, 93.731,
> 93.656, 93.626, 93.551, 93.491, 93.506, 93.551, 23.935, 24.018,
> 24.059, 24.059, 24.045, 24.031, 24.018, 23.99, 23.907, 23.851,
> 23.768, 23.699, 23.671, 23.685, 23.685, 23.671, 23.727, 23.81,
> 23.838, 23.921, 23.935), .Dim = c(21L, 2L), .Dimnames = list(
>     NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(93.720755372218, 23.8856480770871)
>     , ID = "94"
>     , area = 0.11727
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(93.9485141144793, 23.827383855931)
>     , area = 0.0863474999999999
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(93.806, 93.851, 93.911, 93.941, 93.986,
> 94.031, 94.061,
> 94.091, 94.121, 94.151, 94.106, 94.076, 93.986, 93.896, 93.851,
> 93.791, 93.761, 93.761, 93.806, 23.824, 23.907, 23.935, 24.018,
> 24.018, 23.99, 24.018, 23.948, 23.907, 23.851, 23.796, 23.768,
> 23.713, 23.671, 23.63, 23.657, 23.727, 23.782, 23.824), .Dim = c(19L,
> 2L), .Dimnames = list(NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(93.9485141144793, 23.827383855931)
>     , ID = "96"
>     , area = 0.0863474999999999
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(94.2478423555231, 23.9836118751524)
>     , area = 0.102525
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(94.061, 94.046, 94.046, 94.091, 94.121,
> 94.166, 94.226,
> 94.286, 94.331, 94.391, 94.421, 94.451, 94.451, 94.421, 94.331,
> 94.256, 94.196, 94.136, 94.061, 94.031, 94.061, 23.879, 23.948,
> 23.99, 24.031, 24.087, 24.128, 24.184, 24.17, 24.128, 24.073,
> 24.059, 23.976, 23.907, 23.865, 23.851, 23.851, 23.838, 23.81,
> 23.879, 23.921, 23.879), .Dim = c(21L, 2L), .Dimnames = list(
>     NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(94.2478423555231, 23.9836118751524)
>     , ID = "97"
>     , area = 0.102525
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(95.4490116409178, 23.7243413032006)
>     , area = 0.223278500000003
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(95.2, 95.23, 95.26, 95.305, 95.35, 95.395,
> 95.44,
> 95.485, 95.575, 95.635, 95.635, 95.635, 95.665, 95.709, 95.305,
> 95.245, 95.185, 95.17, 95.724, 95.739, 95.68, 95.605, 95.485,
> 95.35, 95.2, 23.671, 23.754, 23.81, 23.893, 23.935, 24.031, 24.073,
> 24.031, 23.976, 23.948, 23.865, 23.796, 23.81, 23.685, 23.519,
> 23.532, 23.588, 23.657, 23.63, 23.574, 23.546, 23.519, 23.505,
> 23.519, 23.671), .Dim = c(25L, 2L), .Dimnames = list(NULL, c("x",
> "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(95.4490116409178, 23.7243413032006)
>     , ID = "100"
>     , area = 0.223278500000003
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(95.5720136547939, 23.476735339631)
>     , area = 0.185465999999999
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(95.784, 95.829, 95.844, 95.814, 95.799,
> 95.724, 95.68,
> 95.59, 95.47, 95.38, 95.29, 95.275, 95.275, 95.32, 95.395, 95.485,
> 95.56, 95.635, 95.709, 95.739, 95.769, 95.784, 23.56, 23.463,
> 23.421, 23.338, 23.282, 23.254, 23.24, 23.282, 23.338, 23.338,
> 23.38, 23.463, 23.532, 23.602, 23.63, 23.657, 23.685, 23.699,
> 23.713, 23.657, 23.616, 23.56), .Dim = c(22L, 2L), .Dimnames = list(
>     NULL, c("x", "y")))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(95.5720136547939, 23.476735339631)
>     , ID = "101"
>     , area = 0.185465999999999
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(97.7712398308958, 23.1559739868982)
>     , area = 0.0656399999999989
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(97.748, 97.823, 97.883, 97.928, 97.958,
> 97.958, 97.928,
> 97.898, 97.838, 97.823, 97.763, 97.673, 97.628, 97.583, 97.643,
> 97.748, 23.282, 23.254, 23.254, 23.24, 23.199, 23.157, 23.143,
> 23.087, 23.059, 23.031, 23.018, 23.059, 23.087, 23.157, 23.24,
> 23.282), .Dim = c(16L, 2L), .Dimnames = list(NULL, c("x", "y"
> )))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(97.7712398308958, 23.1559739868982)
>     , ID = "106"
>     , area = 0.0656399999999989
> ),new("Polygons"
>     , Polygons = list(
> new("Polygon"
>     , labpt = c(98.1040520957036, 23.1987578502582)
>     , area = 0.0543135000000015
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(97.928, 97.913, 97.958, 98.018, 98.107,
> 98.152, 98.197,
> 98.227, 98.242, 98.242, 98.242, 98.182, 98.122, 98.063, 97.988,
> 97.928, 23.171, 23.226, 23.24, 23.254, 23.296, 23.338, 23.324,
> 23.282, 23.254, 23.171, 23.129, 23.087, 23.101, 23.101, 23.115,
> 23.171), .Dim = c(16L, 2L), .Dimnames = list(NULL, c("x", "y"
> )))
> )
> )
>     , plotOrder = 1L
>     , labpt = c(98.1040520957036, 23.1987578502582)
>     , ID = "107"
>     , area = 0.0543135000000015
> )
> )
>     , plotOrder = c(3L, 2L, 4L, 10L, 6L, 5L, 11L, 7L, 9L, 1L, 8L, 12L, 13L)
>     , bbox = structure(c(89.49, 23.018, 98.242, 26.062), .Dim = c(2L,
> 2L), .Dimnames = list(
>     c("x", "y"), c("min", "max")))
>     , proj4string = new("CRS"
>     , projargs = " +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
> )
> )
> 
> 
> 
> tmppoly_shrunken = new("Polygon"
>     , labpt = c(89.7176352165596, 25.2725528991387)
>     , area = 10.2274188632081
>     , hole = FALSE
>     , ringDir = 1L
>     , coords = structure(c(88.4912830509635, 89.0912830509635,
> 89.5312830509635,
> 90.1312830509635, 90.7312830509635, 91.1812830509635, 91.1812830509635,
> 91.4812830509635, 91.7812830509635, 91.6312830509635, 91.4812830509635,
> 91.3312830509635, 91.1812830509635, 90.2812830509635, 89.3812830509634,
> 89.0912830509635, 88.1912830509634, 87.4412830509634, 87.5912830509634,
> 87.8912830509634, 88.4912830509635, 26.3470239077515, 26.6170239077515,
> 26.8970239077515, 27.0270239077515, 26.7570239077515, 26.2070239077515,
> 25.7970239077515, 26.2070239077515, 26.2070239077515, 25.5270239077515,
> 25.1170239077515, 24.1570239077515, 23.4670239077515, 23.6070239077515,
> 23.7470239077515, 24.2970239077515, 24.4270239077515, 24.5670239077515,
> 25.6670239077515, 26.3470239077515, 26.3470239077515), .Dim = c(21L,
> 2L), .Dimnames = list(NULL, c("x", "y")))
> )
> 
> 
> 
> 
> #######################################################
> # ATTEMPT TO REPLACE COORDINATES;
> # FAILS WITH:
> # Error in
> slot(all_emerged_islands_at_this_height_break[j]@polygons[[1]]@Polygons[[1]],
>  :
> #  object of type 'S4' is not subsettable
> #######################################################
> 
> j = 1
> volcano_ID = 86
> 
> slot(all_emerged_islands_at_this_height_break[j]@polygons[[1]]@Polygons[[1]],
> 
> "coords", check=FALSE) = slot(tmppoly_shrunken, "coords")
> 
> # Error in
> slot(all_emerged_islands_at_this_height_break[j]@polygons[[1]]@Polygons[[1]],
>  :
> #   object of type 'S4' is not subsettable
> 
> #######################################################
> # ATTEMPT TO REPLACE WHOLE POLYGONS OBJECT;
> # FAILS WITH:
> # Error in
> slot(all_emerged_islands_at_this_height_break[j]@polygons[[1]]@Polygons[[1]],
>  :
> #  object of type 'S4' is not subsettable
> #######################################################
> 
> all_emerged_islands_at_this_height_break[j]@polygons =
> list(Polygons(list(tmppoly_shrunken), ID=volcano_ID))
> 
> # Error in all_emerged_islands_at_this_height_break[j]@polygons =
> list(Polygons(list(tmppoly_shrunken),  :
> #   object of type 'S4' is not subsettable
> #
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
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