Shae11
Shae11

Reputation: 49

How do I convert the geometry of multipolygons to lat and long using r

Hi I am trying to convert the geometry field into lat and long. I need to use the data to create a map in leaflet for a shiny app.

I have tried the following code:

  1. rl_coord <- rle %>% mutate(lat = unlist(map(rle$geometry, 1)), long = unlist(map(rle$geometry, 2)))

output: Error in stopifnot(): ! Problem while computing lat = unlist(map(rle$geometry, 1)). x lat must be size 45152 or 1, not 19470112. Run rlang::last_error() to see where the error occurred.

sample of data (first 2 rows):

(column names)
oi, name, rle_status, percentage, geometry

1, Aggeneys Gravel Vygieveld, Least Concern, 99, list(c(18.1286933451867, 18.1288873157412, 18.1285209659984, 18.1253701585106, 18.1256087808649, 18.125834301081, 18.1261614439592, 18.1263051557795, 18.1264605244489, 18.1266239843156, 18.1269636452865, 18.12730379009, 18.1274679032905, 18.1276242720226, 18.1277694335806, 18.128129283741, 18.1285605618558, 18.1286933451867, -29.2730674003889, -29.2731573351778, -29.273107503584, -29.273040061747, -29.2729443517532, -29.2728706490378, -29.2727512792674, -29.2727164322246, -29.2726934896327, -29.2726798052816,  -29.2726735519181, -29.2726912387301, -29.2727098853755, -29.2727368207384, -29.272774262715, -29.2728993661673, -29.2730186720589, -29.2730674003889)) 

2,Aggeneys Gravel Vygieveld, Least Concern, 99, list(c(18.1537914281886, 18.1575870517462, 18.1606979380474, 18.1636028297453, 18.1655769358587, 18.16918945379, 18.1735477460181, 18.1759052286574, 18.1772384655054, 18.1773223880217, 18.1776390084706, 18.1759643559649, 18.1735973363743, 18.169952393675, 18.1688938146476, 18.1682070250338, 18.1683513617348, 18.1685030450031, 18.1688311043061, 18.1693585935878, 18.1702652749817, 18.1706158522373, 18.1709379223772, 18.1710792665481, 18.1712000947379, 18.1712925878997, 18.1713437767173, 18.1713693252922,  18.1713707328073, 18.1713479804211, 18.1713015143846, 18.1712382418641, 18.1709475315411, 18.1704635387361, 18.1684209356022, 18.1680682165722, 18.1678999593179, 18.1677395265964, 18.1675893883433, 18.1674634611086, 18.16651344333, 18.1637821201569, 18.1598033908529, 18.1569099426752, 18.1529178620954, 18.14891243025, 18.1454734808983, 18.141641617306, 18.1389141094622, 18.1363582617806, 18.1337413799508, 18.1329259143444, 18.1327123178834, 18.1321225427447, 18.1316307840128, 18.1308981359519, 18.130405823682,  18.1296760299562, 18.1295423948817, 18.1293256727083, 18.1291879582129, 18.1287509532673, 18.1283905796694, 18.1282495214403, 18.1280975604639, 18.1277725515827, 18.1267805933984, 18.1265024871873, 18.1263039910056, 18.1262050407897, 18.1261056638745, 18.1257711483693, 18.1254680798452, 18.1251343803712, 18.1237035930329, 18.1233520704461, 18.1230207288408, 18.1228686758013, 18.122730590917, 18.122428801227, 18.1222578827598, 18.1221962726986, 18.1221588093525, 18.1221453668644, 18.1221553763524,  18.1221897086251, 18.1222487087564, 18.1223573579371, 18.121940612864, 18.119729996386, 18.1201267241008, 18.1202602393336, 18.1232814790033, 18.1262264249677, 18.1309795388961, 18.1359252938222, 18.1414241797188, 18.1456203463775, 18.1505165109378, 18.1537914281886, -29.2714061489315, -29.2729282131844, -29.27283475266, -29.2735366562541, -29.2748603575312, -29.2763805135057, -29.2774333702504, -29.2781257379722, -29.2796611540815, -29.279758428048, -29.2815265405493, -29.282613729886, -29.2822437040154,  -29.2818450674144, -29.2814769494464, -29.2812386697835, -29.2812023081652, -29.2811798583234, -29.2811567953417, -29.2811519774458, -29.2811626612424, -29.2811582844914, -29.2811379726675, -29.2811178709109, -29.2810880943803, -29.2810455977044, -29.2809975064909, -29.2809446550115, -29.2808944231771, -29.2808543392358, -29.2808307426598, -29.2808240597577, -29.2808606799739, -29.2808810489644, -29.2808696770233, -29.2808822434794, -29.280895647919, -29.280916088344, -29.2809458790151, -29.2809806868257,  -29.2806510672722, -29.280271505169, -29.2787456264884, -29.2775611622045, -29.2765159357731, -29.2759494531304, -29.2747535453343, -29.2745113125779, -29.2739715327436, -29.2739181265728, -29.2737464650722, -29.2736922023781, -29.2736427340937, -29.2735310196154, -29.2733754030725, -29.2732322407442, -29.2730770705516, -29.2729311019628, -29.2728915355121, -29.2728078851028, -29.2727640339101, -29.2726519877553, -29.2725284230301, -29.2724913733567, -29.2724634517196, -29.2724256261332, -29.2723486672961,  -29.2723047868547, -29.2722601758286, -29.2722516909261, -29.2722563540645, -29.2723118485858, -29.2723331410326, -29.2723400722685, -29.272324458358, -29.2723303416353, -29.2723548747257, -29.2723790183498, -29.272414889976, -29.2725469760291, -29.2726412327055, -29.2726981781648, -29.2727653543012, -29.2728386599826, -29.2729125214399, -29.2729813861663, -29.2730411602367, -29.2731112309018, -29.2731265774833, -29.2737197621271, -29.2726058704699, -29.272533391812, -29.2709121456387, -29.2701740008779,  -29.2701167811391, -29.2697410330702, -29.2692184199776, -29.2694682832248, -29.2708549256421, -29.2714061489315))

Upvotes: 0

Views: 3114

Answers (2)

Ma&#235;l
Ma&#235;l

Reputation: 52399

You can use sf::st_coordinates (with reproducible data set). You'll get a list of X and Y coordinates for each multipolygon. You can then use unnest to get one row for each X Y pair.

library(spData)
library(sf)
data(nz)

nz %>% 
  transmute(lon = list(st_coordinates(.)[, 1]),
            lat = list(st_coordinates(.)[, 2])) %>% 
  unnest(lon, lat) %>% 
  st_drop_geometry()

output

# A tibble: 19,056 × 2
        lon      lat
 *    <dbl>    <dbl>
 1 1745493. 6001802.
 2 1740539. 5995066.
 3 1733165. 5989714.
 4 1720197. 5980078.
 5 1709110. 5986672.
 6 1701512. 5996205.
 7 1694072. 5996670.
 8 1698473. 5988332.
 9 1703769. 5984809.
10 1706031. 5975136.
# … with 19,046 more rows

Or use the wrapper sfheaders::sf_to_df (pointed out by @tospig) and select the x and y columns:

sfheaders::sf_to_df(nz)[c("x", "y")]

Upvotes: 5

Lucca Nielsen
Lucca Nielsen

Reputation: 1894

Try this

rl_coord <- 
  rle %>%
  sf::st_coordinates() %>%
  as_tibble() %>%
  select("long" = X, "lat" = Y)

rl_coord 

Upvotes: 1

Related Questions