I have data frame that contains variable with set of coordinates, which supposed to be a polygon. I’d like to try to convert it into sf geometry polygon, but I have little idea how to achieve this goal. Data looks like this:
a <- c("[30.523311, 50.40919], [30.523111, 50.409093], [30.522475, 50.408781], [30.522484, 50.408771], [30.523591, 50.407804], [30.524049, 50.407403], [30.526558, 50.406062], [30.526791, 50.405939], [30.527487, 50.4057], [30.527787, 50.405564], [30.528793, 50.405209], [30.528718, 50.404554], [30.530223, 50.404552], [30.530133, 50.404363], [30.529104, 50.404185], [30.529018, 50.403965], [30.528933, 50.403337], [30.529986, 50.403227], [30.531422, 50.403077], [30.531336, 50.402585], [30.531743, 50.402489], [30.531612, 50.401577], [30.531505, 50.401447], [30.531483, 50.401211], [30.531489, 50.40103], [30.531873, 50.400999], [30.531932, 50.400429], [30.531961, 50.400257], [30.531965, 50.400233], [30.532439, 50.400174], [30.533329, 50.400062], [30.533369, 50.399868], [30.533435, 50.399542], [30.533478, 50.39954], [30.534231, 50.399498], [30.534229, 50.399863], [30.5354583263, 50.4005476511], [30.5361664295, 50.4004929412], [30.5361825227, 50.4008211999], [30.5366814136, 50.4008656514], [30.537257, 50.400902], [30.537703, 50.400882], [30.538053, 50.400829], [30.538949, 50.40062], [30.539305, 50.400619], [30.539605, 50.40051], [30.540023, 50.40049], [30.54056, 50.400838], [30.540828, 50.400443], [30.541166, 50.400788], [30.541501, 50.401137], [30.542203, 50.40115], [30.54236, 50.40171], [30.542792, 50.401676], [30.543223, 50.401641], [30.548297, 50.400637], [30.558722, 50.398574], [30.558969, 50.398794], [30.559252, 50.399041], [30.559338, 50.399931], [30.55938, 50.400163], [30.559447, 50.400539], [30.559532, 50.400916], [30.559051, 50.400983], [30.555229, 50.401518], [30.555258, 50.402649], [30.554082, 50.402791], [30.553204, 50.402945], [30.552119, 50.40318], [30.551907, 50.403224], [30.551414, 50.403334], [30.55056, 50.403525], [30.550566, 50.403544], [30.547881, 50.404161], [30.547819, 50.404175], [30.546416, 50.404497], [30.544978, 50.404827], [30.54398, 50.404963], [30.543771, 50.404992], [30.54248, 50.405173], [30.540812, 50.405404], [30.538729, 50.405488], [30.537509, 50.405536], [30.536298, 50.405576], [30.534761, 50.405593], [30.53247, 50.405613], [30.530065, 50.40586], [30.528, 50.406381], [30.527158, 50.406595], [30.525696, 50.407249], [30.52562, 50.407313], [30.525481, 50.40743], [30.525423, 50.407478], [30.525274, 50.4076], [30.524885, 50.407919], [30.524221, 50.408445], [30.523308, 50.409165], [30.52333, 50.409175], [30.523311, 50.40919]")
b <- c("[30.517824, 50.405568], [30.517689, 50.40543], [30.517727, 50.405406], [30.517798, 50.405361], [30.517947, 50.405265], [30.518736, 50.405337], [30.519452, 50.405434], [30.520105, 50.405521], [30.52047, 50.405706], [30.520844, 50.405896], [30.521225, 50.405894], [30.521758, 50.405744], [30.524134, 50.404085], [30.524483, 50.403844], [30.524582, 50.403776], [30.524901, 50.403571], [30.524915, 50.403562], [30.524967, 50.403529], [30.525058, 50.40347], [30.525317, 50.403308], [30.525573, 50.403147], [30.526089, 50.402824], [30.526607, 50.4025], [30.527259, 50.402092], [30.527528, 50.401796], [30.528232, 50.40102], [30.528482, 50.400748], [30.528687, 50.400524], [30.528731, 50.400476], [30.528953, 50.400242], [30.529032, 50.400158], [30.529059, 50.400054], [30.529836, 50.399809], [30.530864, 50.399677], [30.530971, 50.39987], [30.53074, 50.400041], [30.53095, 50.400096], [30.53118, 50.401053], [30.531489, 50.40103], [30.531483, 50.401211], [30.531505, 50.401447], [30.531612, 50.401577], [30.531743, 50.402489], [30.531336, 50.402585], [30.531422, 50.403077], [30.529986, 50.403227], [30.528933, 50.403337], [30.529018, 50.403965], [30.529104, 50.404185], [30.530133, 50.404363], [30.530223, 50.404552], [30.528718, 50.404554], [30.528793, 50.405209], [30.527787, 50.405564], [30.527487, 50.4057], [30.526791, 50.405939], [30.526558, 50.406062], [30.524049, 50.407403], [30.523591, 50.407804], [30.522484, 50.408771], [30.519427, 50.407208], [30.519404, 50.407185], [30.519397, 50.407177], [30.518728, 50.406494], [30.518557, 50.406319], [30.517824, 50.405568]")
polygons<- as.data.frame(c(a,b), ncol=1, nrow=2)%>%
rename(polygon=1)
Your solution works perfectly on the example I provided. However, when I try to use your function on real data I get an error ("'MtrxSet(x, dim, type = "POLYGON", needClosed = TRUE)': polygons not (all) closed ". Perhaps my coordinates are flawed? Sorry for that, but I’m pretty new to R.