Reputation: 61
newdata
V1 V2
1 -2.8701091 8273.6321
2 4.8298909 8273.6321
3 21.3298909 8279.1321
4 25.7298909 8281.3321
5 32.3298909 8285.7321
6 46.6298909 8298.9321
V1 is x and V2 is y and I want to know if there was a way to convert this into an sf multipolygon file. Newdata has 941 polygons and each polygon has roughly 19 rows each. Thank you! Here is a reproducible dataset for the first two polygons:
> dput(head(newdata, 38))
structure(list(.id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), V1 = c(-2.8701090548579,
4.8298909451421, 21.3298909451421, 25.7298909451421, 32.3298909451421,
46.6298909451421, 47.7298909451421, 55.4298909451421, 64.2298909451421,
67.5298909451421, 67.5298909451421, 65.3298909451421, 54.3298909451421,
52.1298909451421, 48.8298909451421, 46.6298909451421, 34.5298909451421,
-2.8701090548579, -2.8701090548579, -1.79600509987251, 9.2039949001275,
20.2039949001275, 25.7039949001275, 37.8039949001275, 41.1039949001275,
44.4039949001275, 44.4039949001275, 38.9039949001275, 35.6039949001275,
33.4039949001275, 32.3039949001275, 29.0039949001275, 20.2039949001275,
16.9039949001275, 11.4039949001275, 0.403994900127497, -1.79600509987251,
-1.79600509987251), V2 = c(8273.63213813615, 8273.63213813615,
8279.13213813615, 8281.33213813615, 8285.73213813615, 8298.93213813615,
8300.03213813615, 8313.23213813615, 8333.03213813615, 8356.13213813615,
8381.43213813615, 8385.83213813615, 8401.23213813615, 8403.43213813615,
8405.63213813615, 8406.73213813615, 8406.73213813615, 8395.73213813615,
8273.63213813615, 7198.72511687208, 7198.72511687208, 7199.82511687208,
7202.02511687208, 7213.02511687208, 7216.32511687208, 7222.92511687208,
7236.12511687208, 7251.52511687208, 7258.12511687208, 7261.42511687208,
7262.52511687208, 7264.72511687208, 7269.12511687208, 7270.22511687208,
7271.32511687208, 7271.32511687208, 7270.22511687208, 7198.72511687208
)), row.names = c(NA, 38L), class = "data.frame")
Upvotes: 2
Views: 2088
Reputation: 26248
Converting R objects to {sf} objects is exactly the use-case for {sfheaders}
If your .id
column specifies each POLYGON within a single MULTIPOLYGON, then use the argument polygon_id = ".id"
library(sf)
library(sfheaders)
sfheaders::sf_multipolygon(
obj = newData
, polygon_id = ".id"
, x = "V1"
, y = "V2"
)
Simple feature collection with 1 feature and 1 field
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -2.870109 ymin: 7198.725 xmax: 67.52989 ymax: 8406.732
CRS: NA
id geometry
1 1 MULTIPOLYGON (((-2.870109 8...
If your .id
column represents each MULTIPOLYGON, use the argument multipolygon_id = ".id"
sfheaders::sf_multipolygon(
obj = newData
, multipolygon_id = ".id"
, x = "V1"
, y = "V2"
)
Simple feature collection with 2 features and 1 field
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -2.870109 ymin: 7198.725 xmax: 67.52989 ymax: 8406.732
CRS: NA
.id geometry
1 1 MULTIPOLYGON (((-2.870109 8...
2 2 MULTIPOLYGON (((-1.796005 7...
Upvotes: 3