将 shapefile(多边形)data.frame 转换为 R 中的 S4
Converting a shapefile (polygon) data.frame to S4 in R
我在弄清楚如何将我的 shapefile 从 data.frame
class 转换为 R 中的 S4
时遇到了一些麻烦。我已经阅读了一些关于 setClass
函数,但不理解语法或者这是否是正确的转换方式。我有三个 classes(1、2 和 3)。我需要将它们重命名为洞、植被和地面。我需要转换此 shapefile,因为我正在为 classification 脚本使用 SpatialPolygonsDataFrame
和 superClass
函数。这是我正在关注的 tutorial。有人可以为我提供一个易于理解的示例来说明如何执行此操作吗?
caret: 6.0-86
randomForest: *4.6-14
e1071: 1.7-2
raster: 3.0-12
sf: *0.8-0
RStoolbox: *0.2.6
R version: 3.5.2
shp <- SpatialPolygonsDataFrame(shp)
Error in SpatialPolygonsDataFrame(shp) :
trying to get slot "polygons" from an object (class "sf") that is not an S4 object
structure(list(class = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), geometry = structure(list(
structure(list(structure(c(-123.116217862721, -123.116217607334,
-123.116217604312, -123.116217864233, -123.116217862721,
44.6731080270105, 44.6731080254993, 44.6731078819384, 44.6731078789161,
44.6731080270105), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116092277155, -123.116092027057,
-123.11609202479, -123.116092296044, -123.116092277155, 44.6731077806902,
44.6731077776679, 44.6731075880164, 44.6731075880164, 44.6731077806902
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116012247241, -123.116012144481,
-123.116012145237, -123.116012203417, -123.116012199639,
-123.116012413469, -123.11601241498, -123.116012245729, -123.116012247241,
44.6730423562089, 44.6730423562089, 44.6730421952696, 44.6730421952696,
44.6730421386008, 44.6730421393564, 44.6730422942511, 44.6730422927399,
44.6730423562089), .Dim = c(9L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116233949854, -123.116233951365,
-123.116233699, -123.116233699, -123.116233581129, -123.116233584151,
-123.116233949854, 44.6731567848266, 44.6731570387027, 44.6731570356804,
44.6731569283875, 44.6731569314098, 44.6731567833154, 44.6731567848266
), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116570359895, -123.116570362918,
-123.116570183089, -123.116570175533, -123.11657011962, -123.116570116597,
-123.116570359895, 44.6730916882686, 44.6730918696087, 44.6730918771645,
44.6730917683605, 44.673091763827, 44.6730916882686, 44.6730916882686
), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116285577377, -123.116285574355,
-123.116285657469, -123.116285655958, -123.116285719427,
-123.11628572396, -123.116285577377, 44.6730524205838, 44.6730521651965,
44.67305216973, 44.673052278534, 44.6730522800452, 44.6730524220949,
44.6730524205838), .Dim = c(7L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116336270999, -123.116334530134,
-123.116336343535, -123.116338241561, -123.116336270999,
44.6730674566993, 44.6730622341048, 44.6730620769434, 44.6730670940192,
44.6730674566993), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116428041172, -123.116425744198,
-123.116427436705, -123.11642973368, -123.116428041172, 44.6730837168605,
44.6730813473501, 44.6730804285603, 44.6730826046413, 44.6730837168605
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116575917969, -123.116577828085,
-123.116581551601, -123.116580125059, -123.116575917969,
44.6730884800602, 44.6730875370917, 44.673090293461, 44.6730912606082,
44.6730884800602), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116663517321, -123.116664653719,
-123.116668328878, -123.116666781442, -123.116663517321,
44.6730755202886, 44.6730744806054, 44.6730758587901, 44.6730774545828,
44.6730755202886), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116731918802, -123.116733490416,
-123.116736270964, -123.116734868601, -123.116731918802,
44.6731688983444, 44.673168100448, 44.6731704216011, 44.6731716547137,
44.6731688983444), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116625508438, -123.116627104231,
-123.11663077939, -123.116629425384, -123.116625508438, 44.6731609919166,
44.6731606775937, 44.6731646912543, 44.6731653440787, 44.6731609919166
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116469072389, -123.116470535199,
-123.116474403788, -123.116472940978, -123.116469072389,
44.6731589246396, 44.673158404798, 44.6731614754901, 44.6731627690494,
44.6731589246396), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116322549599, -123.116316819252,
-123.116320083373, -123.116323951962, -123.116322549599,
44.6731661661537, 44.6731618139916, 44.6731603149136, 44.6731639175367,
44.6731661661537), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116110986163, -123.116113017172,
-123.116115217432, -123.116113742532, -123.116110986163,
44.6731484915398, 44.6731482497531, 44.6731515864107, 44.6731522150563,
44.6731484915398), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116025091408, -123.116025647518,
-123.11602633661, -123.116025466178, -123.116025091408, 44.673099880307,
44.6730997594136, 44.6731001583618, 44.6731004364166, 44.673099880307
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.11602799285, -123.116026832273,
-123.116028669853, -123.116029274319, -123.11602799285, 44.6730422625165,
44.6730393489857, 44.673039252271, 44.673041960283, 44.6730422625165
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116141016081, -123.116142466802,
-123.116142974554, -123.116141680995, -123.116141016081,
44.6730368948499, 44.6730368344032, 44.6730390346629, 44.6730392885391,
44.6730368948499), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116321775881, -123.116319865765,
-123.116320433964, -123.116322416616, -123.116321775881,
44.6730813836181, 44.6730795218598, 44.6730790020182, 44.6730808395978,
44.6730813836181), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116385668038, -123.11638728801,
-123.116388472765, -123.116387239652, -123.116385668038,
44.6731356405723, 44.6731338150821, 44.6731347580505, 44.6731362450392,
44.6731356405723), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116530848913, -123.116528527759,
-123.116530075195, -123.116531840238, -123.116530848913,
44.6730954797875, 44.6730931707238, 44.6730927838649, 44.6730951291967,
44.6730954797875), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116504518332, -123.116505376675,
-123.11650766156, -123.116506682324, -123.116504518332, 44.6730272233786,
44.673026026534, 44.6730269815918, 44.6730282509724, 44.6730272233786
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.11661304433, -123.1166152325,
-123.116616816203, -123.116613926851, -123.11661304433, 44.6730421053551,
44.6730359397921, 44.6730362541149, 44.6730423471419, 44.6730421053551
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116775271172, -123.116776903233,
-123.116776999948, -123.116775392065, -123.116775271172,
44.6730782887472, 44.6730781074072, 44.6730789657502, 44.6730792800731,
44.6730782887472), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116539033395, -123.116539831292,
-123.116541729318, -123.11654114903, -123.116539033395, 44.6731478266262,
44.6731475243927, 44.6731485519865, 44.6731494465976, 44.6731478266262
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116419379161, -123.116420001762,
-123.116421942101, -123.116421313455, -123.116419379161,
44.6731141336379, 44.6731133478308, 44.6731149617576, 44.6731155843586,
44.6731141336379), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg"))), n_empty = 0L, crs = structure(list(epsg = 4326L,
proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = -123.116776999948,
ymin = 44.673026026534, xmax = -123.116012144481, ymax = 44.6731716547137
), class = "bbox"))), row.names = c(NA, -26L), class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(class = NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")))
使用上面的 dput(data),并附上 sf_0.8-1
和 sp_1.4-1
,虽然我认为你只需要 sf
因为它的功能 as_Spatial
提升:
> summary(sf_data)
class geometry
Min. :1.000 POLYGON :26
1st Qu.:2.000 epsg:4326 : 0
Median :2.000 +proj=long...: 0
Mean :2.154
3rd Qu.:3.000
Max. :3.000
所以我们有 sf
,现在我们想要 sp
(S4)。
> sf_as_sp <- as_Spatial(sf_data)
> summary(sf_as_sp)
Object of class SpatialPolygonsDataFrame
Coordinates:
x -123.11678 -123.11601
y 44.67303 44.67317
Is projected: FALSE
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
Data attributes:
class
Min. :1.000
1st Qu.:2.000
Median :2.000
Mean :2.154
3rd Qu.:3.000
Max. :3.000
sf
和 sp
开发人员的意图是尽可能或可行地在实例之间轻松转换它们。 HTH
我在弄清楚如何将我的 shapefile 从 data.frame
class 转换为 R 中的 S4
时遇到了一些麻烦。我已经阅读了一些关于 setClass
函数,但不理解语法或者这是否是正确的转换方式。我有三个 classes(1、2 和 3)。我需要将它们重命名为洞、植被和地面。我需要转换此 shapefile,因为我正在为 classification 脚本使用 SpatialPolygonsDataFrame
和 superClass
函数。这是我正在关注的 tutorial。有人可以为我提供一个易于理解的示例来说明如何执行此操作吗?
caret: 6.0-86
randomForest: *4.6-14
e1071: 1.7-2
raster: 3.0-12
sf: *0.8-0
RStoolbox: *0.2.6
R version: 3.5.2
shp <- SpatialPolygonsDataFrame(shp)
Error in SpatialPolygonsDataFrame(shp) :
trying to get slot "polygons" from an object (class "sf") that is not an S4 object
structure(list(class = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), geometry = structure(list(
structure(list(structure(c(-123.116217862721, -123.116217607334,
-123.116217604312, -123.116217864233, -123.116217862721,
44.6731080270105, 44.6731080254993, 44.6731078819384, 44.6731078789161,
44.6731080270105), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116092277155, -123.116092027057,
-123.11609202479, -123.116092296044, -123.116092277155, 44.6731077806902,
44.6731077776679, 44.6731075880164, 44.6731075880164, 44.6731077806902
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116012247241, -123.116012144481,
-123.116012145237, -123.116012203417, -123.116012199639,
-123.116012413469, -123.11601241498, -123.116012245729, -123.116012247241,
44.6730423562089, 44.6730423562089, 44.6730421952696, 44.6730421952696,
44.6730421386008, 44.6730421393564, 44.6730422942511, 44.6730422927399,
44.6730423562089), .Dim = c(9L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116233949854, -123.116233951365,
-123.116233699, -123.116233699, -123.116233581129, -123.116233584151,
-123.116233949854, 44.6731567848266, 44.6731570387027, 44.6731570356804,
44.6731569283875, 44.6731569314098, 44.6731567833154, 44.6731567848266
), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116570359895, -123.116570362918,
-123.116570183089, -123.116570175533, -123.11657011962, -123.116570116597,
-123.116570359895, 44.6730916882686, 44.6730918696087, 44.6730918771645,
44.6730917683605, 44.673091763827, 44.6730916882686, 44.6730916882686
), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116285577377, -123.116285574355,
-123.116285657469, -123.116285655958, -123.116285719427,
-123.11628572396, -123.116285577377, 44.6730524205838, 44.6730521651965,
44.67305216973, 44.673052278534, 44.6730522800452, 44.6730524220949,
44.6730524205838), .Dim = c(7L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116336270999, -123.116334530134,
-123.116336343535, -123.116338241561, -123.116336270999,
44.6730674566993, 44.6730622341048, 44.6730620769434, 44.6730670940192,
44.6730674566993), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116428041172, -123.116425744198,
-123.116427436705, -123.11642973368, -123.116428041172, 44.6730837168605,
44.6730813473501, 44.6730804285603, 44.6730826046413, 44.6730837168605
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116575917969, -123.116577828085,
-123.116581551601, -123.116580125059, -123.116575917969,
44.6730884800602, 44.6730875370917, 44.673090293461, 44.6730912606082,
44.6730884800602), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116663517321, -123.116664653719,
-123.116668328878, -123.116666781442, -123.116663517321,
44.6730755202886, 44.6730744806054, 44.6730758587901, 44.6730774545828,
44.6730755202886), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116731918802, -123.116733490416,
-123.116736270964, -123.116734868601, -123.116731918802,
44.6731688983444, 44.673168100448, 44.6731704216011, 44.6731716547137,
44.6731688983444), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116625508438, -123.116627104231,
-123.11663077939, -123.116629425384, -123.116625508438, 44.6731609919166,
44.6731606775937, 44.6731646912543, 44.6731653440787, 44.6731609919166
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116469072389, -123.116470535199,
-123.116474403788, -123.116472940978, -123.116469072389,
44.6731589246396, 44.673158404798, 44.6731614754901, 44.6731627690494,
44.6731589246396), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116322549599, -123.116316819252,
-123.116320083373, -123.116323951962, -123.116322549599,
44.6731661661537, 44.6731618139916, 44.6731603149136, 44.6731639175367,
44.6731661661537), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116110986163, -123.116113017172,
-123.116115217432, -123.116113742532, -123.116110986163,
44.6731484915398, 44.6731482497531, 44.6731515864107, 44.6731522150563,
44.6731484915398), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116025091408, -123.116025647518,
-123.11602633661, -123.116025466178, -123.116025091408, 44.673099880307,
44.6730997594136, 44.6731001583618, 44.6731004364166, 44.673099880307
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.11602799285, -123.116026832273,
-123.116028669853, -123.116029274319, -123.11602799285, 44.6730422625165,
44.6730393489857, 44.673039252271, 44.673041960283, 44.6730422625165
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116141016081, -123.116142466802,
-123.116142974554, -123.116141680995, -123.116141016081,
44.6730368948499, 44.6730368344032, 44.6730390346629, 44.6730392885391,
44.6730368948499), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116321775881, -123.116319865765,
-123.116320433964, -123.116322416616, -123.116321775881,
44.6730813836181, 44.6730795218598, 44.6730790020182, 44.6730808395978,
44.6730813836181), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116385668038, -123.11638728801,
-123.116388472765, -123.116387239652, -123.116385668038,
44.6731356405723, 44.6731338150821, 44.6731347580505, 44.6731362450392,
44.6731356405723), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116530848913, -123.116528527759,
-123.116530075195, -123.116531840238, -123.116530848913,
44.6730954797875, 44.6730931707238, 44.6730927838649, 44.6730951291967,
44.6730954797875), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116504518332, -123.116505376675,
-123.11650766156, -123.116506682324, -123.116504518332, 44.6730272233786,
44.673026026534, 44.6730269815918, 44.6730282509724, 44.6730272233786
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.11661304433, -123.1166152325,
-123.116616816203, -123.116613926851, -123.11661304433, 44.6730421053551,
44.6730359397921, 44.6730362541149, 44.6730423471419, 44.6730421053551
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116775271172, -123.116776903233,
-123.116776999948, -123.116775392065, -123.116775271172,
44.6730782887472, 44.6730781074072, 44.6730789657502, 44.6730792800731,
44.6730782887472), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(-123.116539033395, -123.116539831292,
-123.116541729318, -123.11654114903, -123.116539033395, 44.6731478266262,
44.6731475243927, 44.6731485519865, 44.6731494465976, 44.6731478266262
), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg")),
structure(list(structure(c(-123.116419379161, -123.116420001762,
-123.116421942101, -123.116421313455, -123.116419379161,
44.6731141336379, 44.6731133478308, 44.6731149617576, 44.6731155843586,
44.6731141336379), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg"))), n_empty = 0L, crs = structure(list(epsg = 4326L,
proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = -123.116776999948,
ymin = 44.673026026534, xmax = -123.116012144481, ymax = 44.6731716547137
), class = "bbox"))), row.names = c(NA, -26L), class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(class = NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")))
使用上面的 dput(data),并附上 sf_0.8-1
和 sp_1.4-1
,虽然我认为你只需要 sf
因为它的功能 as_Spatial
提升:
> summary(sf_data)
class geometry
Min. :1.000 POLYGON :26
1st Qu.:2.000 epsg:4326 : 0
Median :2.000 +proj=long...: 0
Mean :2.154
3rd Qu.:3.000
Max. :3.000
所以我们有 sf
,现在我们想要 sp
(S4)。
> sf_as_sp <- as_Spatial(sf_data)
> summary(sf_as_sp)
Object of class SpatialPolygonsDataFrame
Coordinates:
x -123.11678 -123.11601
y 44.67303 44.67317
Is projected: FALSE
proj4string :
[+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0]
Data attributes:
class
Min. :1.000
1st Qu.:2.000
Median :2.000
Mean :2.154
3rd Qu.:3.000
Max. :3.000
sf
和 sp
开发人员的意图是尽可能或可行地在实例之间轻松转换它们。 HTH